Hi ,
i have a combo box which loads from the database. Now i want default text on the combo box ("--Please Select--") to appear on top of the list, it must show the text whithout the user even opened the combo box. Also when the user clear the combo box, the default text will be "--Please Select--"
this is how i bindeed it ;
private void LoadCombo()
{
try
{
oConnection = new SqlConnection(_connectionString);
oCommand = new SqlCommand("select * from tbldpt", oConnection);
oAdapter = new SqlDataAdapter(oCommand);
oConnection.Open();
oDataset = new System.Data.DataSet();
SqlDataReader oReader = oCommand.ExecuteReader();
while (oReader.Read())
{
string _Combobox = oReader["Departmentname"].ToString();
cboDepartment.Items.Add(_Combobox);
}
oReader.Close();
oConnection.Close();
}
catch(Exception ex)
{
}
}

VulpesRank #55 · 29.2K reputationPosted Dec 4, 2014, 1:41 AM EST
Manish Kumar ChoudharyRank #339 · 7.4K reputationPosted Dec 3, 2014, 11:23 PM EST
this will work fine
private void LoadCombo()
{
try
{
oConnection = new SqlConnection(_connectionString);
oCommand = new SqlCommand("select * from tbldpt", oConnection);
oAdapter = new SqlDataAdapter(oCommand);
oConnection.Open();
oDataset = new System.Data.DataSet();
SqlDataReader oReader = oCommand.ExecuteReader();
while (oReader.Read())
{
string _Combobox = oReader["Departmentname"].ToString();
cboDepartment.Items.Add(_Combobox);
}
cboDepartment.Items.Insert(0, "--Please Select--");
oReader.Close();
oConnection.Close();
}
catch (Exception ex)
{
}
}
Enos128 reputationPosted Dec 3, 2014, 11:30 PM EST
Manish Kumar ChoudharyRank #339 · 7.4K reputationPosted Dec 3, 2014, 11:57 PM EST
private void LoadCombo()
{
try
{
oConnection = new SqlConnection(_connectionString);
oCommand = new SqlCommand("select * from tbldpt", oConnection);
oAdapter = new SqlDataAdapter(oCommand);
oConnection.Open();
oDataset = new System.Data.DataSet();
SqlDataReader oReader = oCommand.ExecuteReader();
DataRow row = oDataset.Tables [0].NewRow();
row["Category"] = "--Please Select--";
oDataset.Tables[0].Rows.InsertAt(row, 0);
while (oReader.Read())
{
string _Combobox = oReader["Departmentname"].ToString();
cboDepartment.Items.Add(_Combobox);
}
oReader.Close();
oConnection.Close();
}
catch (Exception ex)
{
}
}
Enos128 reputationPosted Dec 4, 2014, 12:25 AM EST
DataRow row = oDataset.Tables[0].NewRow();
row["Category"] = "--Please Select--";
oDataset.Tables[0].Rows.InsertAt(row, 0);