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

    I tried several times in combobox data is not retireved from the database

    My code as follows

    In pageload i written the below code as follows

    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 database records as follows (Table name Designations )

    ID Name Designation
    1 Raj Course Officer
    2 Ramesh Authorised signatoury

    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.
  • #761571
    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();


    If you need bind using datatable for binding ddl means using this code


    DataTable dt = new DataTable();
    SqlDataAdapter sc1 = new SqlDataAdapter("select DesgId,DesgNam from head", conn);
    sc1.Fill(dt);
    ddldesignation.DataSource = dt;
    ddldesignation.DataTextField = "DesgNam";
    ddldesignation.DataValueField = "DesgId";
    ddldesignation.DataBind();



    Both code working check your old post
    why post the duplicate again and again

    check this url

    http://www.dotnetspider.com/forum/342677-in-combo-box-data-is-not-retrieved-from-the-database-in-web-application.aspx


    I have attached snapshot given below check them

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

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

    Delete Attachment

  • #761584
    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);
    DropDownList1.DataSource = dt;
    DropDownList1.DataTextField = "Designation";
    DropDownList1.DataValueField = "Designation";
    DropDownList1.DataBind();
    DropDownList1.SelectedIndex = 0;
    }
    catch (Exception ex)
    {
    Response.Write(ex.Message);
    }
    finally
    {
    con.Close();
    }

    See the attached image as an output of this code.

    Hope this will help you.
    Mark the answer if it helped you.

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

    Delete Attachment

  • #761586
    Hi rao,

    Yesterday also you raise the same question did you tried yesterday posts, refer below sample may be this will helpful to you.


    Protected void Page_Load(object sender, EventArgs e)
    {
    SqlConnection conn = new SqlConnection("Data Source=DOCTOR\SQLEXPRESS;Initial Catalog=HIMT_Testing;User ID=sa;Password=himt123");
    try
    {
    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();
    }
    Catch(Exception ex)
    {
    }
    finally
    {
    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/


Sign In to post your comments