I want to declare Page scope array and fill from end user
protected void Button2_Click(object sender, EventArgs e)
{
String Knowledge_iloes = TextBox5.Text;
Knowlege.Add(Knowledge_iloes);
for (int i = 0; i < Knowlege.Count; i++)
{
Response.Write(Knowlege[i]);
}
}
![ http://i.stack.imgur.com/H3HlU.jpg][1]
Related
I'm trying to add textbox and numericUpandDown values to an array, but it doesn't seem to be working.
Carro []carros = new Carro[1];
private Carro carro;
public Form1()
{
..
}
private void Form1_Load(object sender, EventArgs e)
{
..
}
private void AdicionarCarro()
{
this.carro = new Carro(textboxCor.Text, textboxMarca.Text, textboxModelo.Text,
(int.Parse(numUpDownCilindrada.Text)), (int.Parse(numUpDownVelocidade.Text)));
}
private Carro[] AdicionarArray(Carro carro, Carro[] array)
{
AdicionarCarro();
int novoTamanho = array.Length + 1;
Carro[] carros = new Carro[novoTamanho];
for (int i = 0; i < array.Length; i++)
{
carros[i] = array[i];
}
carros[novoTamanho] = carro;
return carros;
}
private void buttonGravar_Click(object sender, EventArgs e)
{
AdicionarArray(carro, carros);
}
When I type the values and click on the "buttonGravar", it gives me this
Error:
I'd be much delighted to get some tips/help on it.
Using System.Collection.Generic.List<T> would be much simpler, since it doesn't have a fixed size:
List<Carro> carros = new List<Carro>();
carros.AddRange(array);
carros.Add(carro);
return carros;
Better way:
private List<Carro> Carros;
public Form1()
{
Carros = new List<Carro>();
..
}
private void Form1_Load(object sender, EventArgs e)
{
..
}
private void AdicionarCarro()
{
var carro = new Carro(textboxCor.Text, textboxMarca.Text, textboxModelo.Text,
(int.Parse(numUpDownCilindrada.Text)), (int.Parse(numUpDownVelocidade.Text)));
Carros.Add(carro);
}
private void buttonGravar_Click(object sender, EventArgs e)
{
AdicionarCarro();
}
To help you understand your code:
carros[novoTamanho] = carro;
should be
carros[novoTamanho - 2] = carro;
Reason:
Array index starts from 0. novoTamanh represents new length (starting at 1, not 0 unlike index), which is outside array.
It's an index out of range exception because your array Carro is of size tmanho:
Carro[] carros = new Carro[novoTamanho];
and carros can contain exactly "novoTamanho" items indexed from "0" to "novoTamanho -1"
You can simply solve this by defining:
int novoTamanho = array.Length + 2;
Or if you do not want to manage indexes, use Lists:
List<Carro> listCarro = new List<Carro>;
listCarro.AddRAnge(array);
listCarro.Add(carro);
return listCarro.ToArray();
How to access array from another method in this example? I'm new in c# and i really appreciate if you will help me. Thanks in advance!
private void button1_Click(object sender, EventArgs e) {
int[] array1 = new int[5];
for (int i = 0; i < 5; i++) {
array1[i] = i;
}
}
private void button2_Click(object sender, EventArgs e) {
int[] array2 = new int[5];
for (int i = 0; i < 5; i++) {
array2[i] = array1[i];
}
}
As Jon Skeet mentioned in his comment, local variables are by definition local to the method which defines them. If you want something to be visible globally, you need to make it an instance variable, or return it from the method.
Assuming that only array1 needs to be visible, it suffices to declare that one outside.
// Declare the array globally.
int[] array1 = new int[5];
private void button1_Click(object sender, EventArgs e)
{
// Initially the array inside this method.
for(int i=0;i<5;i++)
array1[i]=i;
}
private void button2_Click(object sender, EventArgs e)
{
int[] array2 = new int[5];
// Copy from the global array
for(int i=0;i<5;i++)
{
array2[i]=array1[1];
}
}
Declare both arrays outside to enable access.
int[] array1, array2;
private void button1_Click(object sender, EventArgs e) {
array1 = new int[5];
for (int i = 0; i < 5; i++) {
array1[i] = i;
}
}
private void button2_Click(object sender, EventArgs e) {
array2 = new int[5];
for (int i = 0; i < 5; i++) {
array2[i] = array1[i];
}
}
subj. I get the ID, and it will fill the table. But in this example, only the last entry is filled.
If you announce in page_load "pro = number" - that is filled correctly.
What to do please tell me.
public string idul;
public int pro;
public string nz;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
pro = 5;
}
protected void Button2_Click(object sender, EventArgs e)
{
for (int i = pro; i > -1; i--)
{
wisp();
}
}
public void wisp()
{
SqlConnection con_tsp = new SqlConnection(WebConfigurationManager.ConnectionStrings["JJ"].ConnectionString);
string tsp1 = "UPDATE TSP SET IDUL=#IDUL WHERE NZ=#NZ";
SqlCommand upd_tsp = new SqlCommand(tsp1, con_tsp);
upd_tsp.Parameters.AddWithValue("#NZ", pro);
upd_tsp.Parameters.AddWithValue("#IDUL", "12");
upd_tsp.Connection.Open();
upd_tsp.ExecuteNonQuery();
upd_tsp.Connection.Close();
pro--;
}
You have set Pro as a global variable, on Button2_Click the value will be set to 0 (int's default value)
and then the button click handler will be called hence u r getting only 0 as the input value
I have a folder browser button and a combobox which shows selected path. I want it keep previously selected paths. I wrote this code and it works fine. But I wanna know if there is more efficient way to do this.
private void Form1_Load_1(object sender, EventArgs e)
string hurrem, hurrem2, hurrem3, hurrem4, hurrem5;
hurrem = Settings.Default["com1"].ToString();
hurrem2 = Settings.Default["com2"].ToString();
hurrem3 = Settings.Default["com3"].ToString();
hurrem4 = Settings.Default["com4"].ToString();
hurrem5 = Settings.Default["com5"].ToString();
comboBox2.Items.Add(hurrem);
comboBox2.Items.Add(hurrem2);
comboBox2.Items.Add(hurrem3);
comboBox2.Items.Add(hurrem4);
comboBox2.Items.Add(hurrem5);
comboBox2.SelectedIndex = 0;
and
private void button1_Click(object sender, EventArgs e)
{
//..code
Settings.Default["com5"] = Settings.Default["com4"];
Settings.Default["com4"] = Settings.Default["com3"];
Settings.Default["com3"] = Settings.Default["com2"];
Settings.Default["com2"] = Settings.Default["com1"];
Settings.Default["com1"] = path1.ToString();
Settings.Default.Save();
You can use the LimitedStack from here: Limit size of Queue<T> in .NET?. You could then add methods for serializing/deserializing the data using Settings.Default.
public void Save()
{
int i = 0;
foreach (var item in _stack)
{
Settings.Default["com" + i++] = item;
}
Settings.Default.Save();
}
public void Load()
{
for (int i = 0; i < Limit; i++)
{
_stack.Add((T)Settings.Default["com" + i++]);
}
}
Storing the items (assuming you have the limited stack saved in a variable called myStack):
myStack.Push(path1.ToString());
myStack.Save();
Adding the items to the combobox:
for (int i = 0; i < myStack.Limit; i++)
{
comboBox2.Items.Add(myStack.Pop());
}
In the below C# code even though I am choosing my own year but still the value is passed as 1920 only. I can see all the values being displayed in the dropdown box but when I choose a value and submit it only 1920 is being passed to the database.
protected void Page_Load(object sender, EventArgs e)
{
DropDownList3.Items.Clear();
for (int i = 1920; i <= 2000; i++)
{
DropDownList3.Items.Add(i.ToString());
}
}
protected void Button1_Click(object sender, EventArgs e)
{
sbtc.dex(DropDownList3.SelectedItem);
}
Can anyone tell me where did I do mistake?
Because when you click on the button it is again executing the code in your Page_Load event. Solution is to set the dropdown values only once. You can use Page.IsPostBack property to check whether this is the initial load or a postback.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DropDownList3.Items.Clear();
for (int i = 1920; i <= 2000; i++)
{
DropDownList3.Items.Add(i.ToString());
}
}
}
Try like this;
if(!IsPostBack)
[
for (int i = 1920; i <= 2000; i++)
{
DropDownList3.Items.Add(i.ToString());
}
}
Try also setting a value for the selected items like this, make sure to check for postbacks
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DropDownList3.Items.Clear();
for (int i = 1920; i <= 2000; i++)
{
DropDownList3.Items.Add( new ListItem(i.ToString(), i.ToString()));
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
sbtc.dex(DropDownList3.SelectedItem);
}