2
Answers

find the error

Photo of Durga Velusamy

Durga Velusamy

10y
405
1
The below code does not work. Its shows an error as item collections cannot be modified.
Last 5th line error. Why its shows an error?
I have to get the count value of last id +1 on combobox1.when execute the application it create the id but I did not able to get it in combobox1. Please find.
private void button2_Click(object sender, EventArgs e)
{

int CustomerId=0;
int i=0;

SqlConnection con = new SqlConnection(@"server=CSE-PC; integrated security=true; Database=Bas");
con.Open();
SqlCommand com = new SqlCommand("select max(CustomerId) FROM devi", con);
if (com.ExecuteScalar().ToString() == "")
{
CustomerId = 0;
}
else
{
CustomerId = int.Parse(com.ExecuteScalar().ToString());
}
if (CustomerId > 0)
{
CustomerId = CustomerId + 1;
}
else
{
CustomerId = 001;
}
if (i>0)
{

CustomerId = CustomerId + 1;
}
else
{
CustomerId = 001;

}

comboBox1.Items.Add("CustomerId");
/* collections cannot be modified */
i = i + 1;
MessageBox.Show("IdCreated");
con.Close();

}

Answers (2)

0
Photo of Manoj Bhoir
NA 7.6k 294.2k 10y
Why you are adding "CustomerId" string into combobox1?
It will add "CustomerId" not 001 or your id.
Write
combobox1.Items.Add(CustomerId);
Not
combobox1.Items.Add("CustomerId");
Accepted
0
Photo of Durga Velusamy
NA 297 46.7k 10y
ok Mr.Manoj Bhoir sir "combobox1.Items.Add(CustomerId); " is this will get an id and it will displayed on combobox1  able to visible to user and then able to added in the table  is it possible. i will try . thank you sir.