Iam currently working on a book management project and I'm using SQL Server from Visual Studio. I have a Book Category table in the database and I'm trying to place it in a combobox.
This the code - I don't see anything wrong with it but the categories are taking so long to be visible in the combobox.
Also the list is repetitive, is it maybe because of the while
Loop? If so is there any way to fix it?
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
con.ConnectionString = (@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\malek\source\repos\BookStore\BookStore\BOOKDB.mdf;Integrated Security=True");
scmd.Connection = con;
con.Open();
scmd.CommandText = "SELECT CATEGORY FROM BOOKCAT";
var rd = scmd.ExecuteReader();
while (rd.Read())
{
List.Add(Convert.ToString(rd[0]));
}
int i = 0;
while (i < List.LongCount())
{
comboBox1.Items.Add(List[i]);
i = i + 1;
}
}
catch (Exception EX)
{
MessageBox.Show(EX.Message);
}
finally
{
con.Close();
}
}
What did I miss?
NOTE: I am not getting any errors!!