You must Sign In to post a response.
  • Category: ASP.NET

    In combo box data is not retrieved from the database in web application

    my code as follows

    SqlConnection conn = new SqlConnection("Data Source=DOCTOR\SQLEXPRESS;Initial Catalog=HIMT_Testing;User ID=sa;Password=himt123");

    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);

    ComboBox1.DataTextField = "Designation";
    ComboBox1.DataValueField = "Designation";

    ComboBox1.DataSource = dt;
    conn.Close();

    in combo box data is not retrieved from the database.

    what is the problem in my code. it is web application.
  • #761551
    add ComboBox1.DataBind() after the line ComboBox1.DataSource = dt;

    As follows

    ComboBox1.DataTextField = "Designation";
    ComboBox1.DataValueField = "Designation";

    ComboBox1.DataSource = dt;
    ComboBox1.DataBind()

    By Nathan
    Direction is important than speed

  • #761552
    Hello
    Rao

    I and Nirav so many time asking listen you have post more then question we are take time solve your issue if the answer useful to you select as best answer


    thats DNS grow and useful to some members

    Donot forgot

    follow this code


    Create table Designation
    (
    DesgId int,
    DesgNam varchar(40)
    )

    Insert into Designation VALUES(1,'SW')
    Insert into Designation VALUES(2,'HW')
    Insert into Designation VALUES(3,'SA')

    protected void bt1_Click(object sender, EventArgs e)
    {
    DataTable dt = new DataTable();
    SqlConnection conn = new SqlConnection(GetConnectionString());
    SqlDataAdapter sc = new SqlDataAdapter("select DesgId,DesgNam from head", conn);
    sc.Fill(dt);
    ddldesignation.DataSource = dt;
    ddldesignation.DataTextField = "DesgNam";
    ddldesignation.DataValueField = "DesgId";
    ddldesignation.DataBind();
    }

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #761554
    Hai Rao,
    The code which you are sing is incorrect. You can try like below if you want to the use the DataReader:

    var conn = new SqlConnection("Data Source=DOCTOR\SQLEXPRESS;Initial Catalog=HIMT_Testing;User ID=sa;Password=himt123");
    var sc = new SqlCommand("select Designation from Designations where active <>'d'", conn);
    SqlDataReader reader;
    conn.Open();
    reader = sc.ExecuteReader();
    if(dr.HasRows)
    {
    while(dr.Read())
    {
    ComboBox1.Items.Add(dr[0].ToString());
    }
    }
    conn.Close();

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #761557
    Hi,

    As you said your application is web based, in web application we are using dropdownlist, combobox we are using in windows application. If you want to bind data to that use below sample.

    SqlConnection conn = new SqlConnection("Data Source=DOCTOR\SQLEXPRESS;Initial Catalog=HIMT_Testing;User ID=sa;Password=himt123");

    conn.Open();

    SqlCommand cmd = new SqlCommand("select Designation from Designations where active <>'d'", conn);

    SqlDataAdapter da=new SqlDataAdapter(cmd);
    DataSet ds=new DataSet();
    da.Fill(ds);

    ddl.DataSource();
    ddl.DataTextField = "Designation";
    ddl.DataValueField = "ID";
    ddl.DataBind();

    conn.Close();


    Hope this will helpful to you...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #761559
    Hi Rao.

    You have given the data source as data table but didn't called the bind method.
    DataTable dt = new DataTable();
    ComboBox1.DataSource = dt;
    ComboBox1.DataBind();//this will give you data.

    Regards
    Thota Sridhar.
    If you learn from defeat..
    You haven't really lost..

    Sridhar Thota.
    Editor: DNS Forum.

  • #761564
    Hi,

    You are missing below line,

    ComboBox1.DataBind();


    So your last portion of the code looks like below,

    ComboBox1.DataSource = dt;
    ComboBox1.DataBind();
    conn.Close();


    Regards,
    Asheej T K
    Microsoft MVP[ASP.NET/IIS]
    DotNetSpider MVM

  • #761572
    Hi

    If you need Bind the Records using Datareader using this code i mention below



    SqlConnection conn = new SqlConnection(GetConnectionString());
    var sc = new SqlCommand("select Designation from Designations where active <>'d'", conn);
    SqlDataReader reader;
    conn.Open();
    reader = sc.ExecuteReader();
    if (reader.HasRows)
    {
    while (reader.Read())
    {
    ComboBox1.Items.Add(reader[0].ToString());
    }
    }
    conn.Close();


    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #761587
    Hello Rao,

    Refer the below code :

    SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Admin\Desktop\WebApplication1\WebApplication1\App_Data\Database1.mdf;Integrated Security=True;User Instance=True");
    try
    {
    DataTable dt = new DataTable();
    con.Open();
    SqlDataAdapter sda = new SqlDataAdapter("select Designation from Designations where active <> 'd'",con);
    sda.Fill(dt);
    ComboBox1.DataSource = dt;
    ComboBox1.DataTextField = "Designation";
    ComboBox1.DataValueField = "Designation";
    ComboBox1.DataBind();
    }
    catch (Exception ex)
    {
    Response.Write(ex.Message);
    }
    finally
    {
    con.Close();
    }

    Hope this will help you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"


Sign In to post your comments