Hello Team,
Please in my window form application, am trying to load Category on the product entry form and save and my issues is category is not loading.

CLASS
public static void CBFill(string qry, ComboBox cb)
{
SqlCommand cmd = new SqlCommand(qry, con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
cb.DataSource = dt;
cb.SelectedIndex = -1;
}
PRODUCT ENTRY FORM PAGE
private void frmProductAdd_Load(object sender, EventArgs e)
{
string qry = @"Select CategoryId 'id', CategoryName 'name' from tblCategory";
MainClass.CBFill(qry, cmbCategory);
if (id > 0)
{
cmbCategory.SelectedValue = catID;
LoadImage();
}
}

Amit MohantyRank #112 · 20K reputationPosted Oct 18, 2023, 9:57 AM EDT
You have to pass both
displayMember, valueMember. Try like this:string qry = @"Select CategoryId 'id', CategoryName 'name' from tblCategory"; MainClass.CBFill(qry, cmbCategory, "name", "id");Amit MohantyRank #112 · 20K reputationPosted Oct 18, 2023, 7:59 AM EDT
Try like this:
public static void CBFill(string sqlQuery, ComboBox comboBox, string displayMember, string valueMember) { SqlCommand sqlCommand = new SqlCommand(sqlQuery, con); sqlCommand.CommandType = CommandType.Text; SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand); DataTable dataTable = new DataTable(); sqlDataAdapter.Fill(dataTable); comboBox.Datasource = dataTable; comboBox.DisplayMember = displayMember; comboBox.ValueMember = valueMember; comboBox.SelectedIndex = -1; } private void frmProductAdd_Load(object sender, EventArgs e) { string qry = @"Select CategoryId 'id', CategoryName 'name' from tblCategory"; MainClass.CBFill(qry, cmbCategory, "name", "id"); // your other code }Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Oct 18, 2023, 9:27 AM EDT
Hello Amit, thank but, when I applied the changes the combobox started selecting only the category ID and when I remove the "id" leaving the name, the category name started coming MainClass.CBFill(qry,cmbCategory, " ", "name")" but when I tried to save the data into the database the category id in the product table become zero
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Oct 18, 2023, 11:44 AM EDT
Thank you for your help, it is working now