Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My code as follows

In pageload i written the below code as follows

C#
SqlConnection conn = new SqlConnection(@"Data Source=DOCTOR\SQLEXPRESS;Initial Catalog=Testing;User ID=sa;Password=a");

conn.Open();
SqlCommand sc = new SqlCommand("select Designation from Designations where active <> 'd'", conn);
 SqlDataReader reader;
 reader = sc.ExecuteReader();
 DataTable dt = new DataTable();
 dt.Columns.Add("Designation", typeof(string));
 dt.Load(reader);
 dt.Load(reader);
 ComboBox1.DataTextField = "Designation";
 ComboBox1.DataValueField = "Designation";
 ComboBox1.DataSource = dt;
 ComboBox1.DataBind();
 conn.Close();


In run mode as follows
Designation Combobox

But in combobox designation is not displayed. I tried several time but in combobox designation is not shown.

Please help me what is the problem in my above code.
Posted
Updated 13-Aug-15 17:34pm
v3
Comments
DamithSL 14-Aug-15 0:13am    
any exceptions? debug and check reader has records or not

There are quite a few possible problems, like:

  • The condition of the query causes no rows to be returned
  • The table is empty
  • The ComboBox1 isn't actually the correct object and so on

Also you seem to call the data table's Load method twice. Never tested, what happens if the load is called the second time with the same reader....

Anyhow You should us the debugger to see what actually happens. Place a breakpoint on the first line and go through the code step-by-step. Using the debugger, investigate what is the content of different variables, does the query return rows and so on.

But actually before doing even that, I would suggest the following modifications:

  • Instead of using the reader and loading the data table, I'd use SqlDataAdapter, define the SelectCommand[^] and use the SqlDataAdapter.Fill[^] to populate the data table.
  • I suggest renaming the objects to something meaningful instead of the default names like ComboBox1. This helps to understand the code and also to spot errors.
 
Share this answer
 
v2
Comments
aarif moh shaikh 14-Aug-15 0:29am    
agree with you.
Read Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^] and try to debug your application and check the values you get at runtime and also which objects not behave as you expected.
you can fill Datatable using SqlDataAdapter[^] and set the datatable as DataSource, sample code:
C#
using(SqlConnection con = new SqlConnection(ConnectionString))
using(SqlCommand cmd = new SqlCommand("select Designation from Designations where active <> 'd'", con))
using(SqlDataAdapter da = new SqlDataAdapter(cmd))
{
     DataTable dt=new DataTable();
     da.Fill(dt);
     ComboBox1.DataTextField = "Designation";
     ComboBox1.DataValueField = "Designation";
     ComboBox1.DataSource = dt;
     ComboBox1.DataBind();
}

run the sql statement in sql management studio and make sure you have data for the sql statement. debug and check above code and check the datatable rows. update the question with exception details if any.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900