Durga Velusamy

Durga Velusamy

  • NA
  • 318
  • 114.4k

find the error

Apr 15 2015 3:37 AM
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)