I am trying to list multiple companies in a combobox. These companies populate from database table tbl_companies. Combobox value should be id of table(Which is primary key of the table). Text should be the name of company. Like id=1 and name = "XYZ". Code is
string connStr = "My Connection string";
SqlConnection conn = new SqlConnection(connStr);
string strQuery = "SELECT * FROM tbl_Companies ORDER BY id ASC";
SqlCommand cmd = new SqlCommand(strQuery, conn);
conn.Open();
SqlDataReader companyRead = cmd.ExecuteReader();
if (companyRead.HasRows)
{
while (companyRead.Read())
{
int cId = (Convert.ToInt16(companyRead["id"].ToString()));
cmbCompany.Items.Insert(cId, companyRead["name"].ToString());
}
}
conn.Close();
When I am executing the code, getting the following error. System.ArgumentOutOfRangeException: 'InvalidArgument=Value of '1' is not valid for 'index'. Parameter name: index'
For reference, I am also sharing the [enter image description here]screenshot of code with error1
I need guidance, how to fix the problem.