Employers - Post Free Jobs
Forum guidelines
  • Home
  • »
  • C# Language
  • »
  • populating data to dropdown list and text field from sql table
AuthorQuestion
populating data to dropdown list and text field from sql table
Posted on: 16 Aug 2012
Hi,

       Can you help me for populating data to dropdown list from sql server db table and when selecting the dropdown list the text box field must be loaded accordingly? 

example: If I select employee id (E0000001,.....) in ddl, the other textbox fields have to be loaded(name,department,designation,...)

AuthorReply
Naresh Avari
  • 0
  • 0
accepted
Re: populating data to dropdown list and text field from sql table
Posted on: 17 Aug 2012   Accepted Answer
Sorry Karthika,


You have to set AutoPostBack property to true for the ddlEmp.

<asp:DropDownList ID="ddlEmp" runat="server" 
                        onselectedindexchanged="ddlEmp_SelectedIndexChanged" 
            AutoPostBack="True"></asp:DropDownList>


Then it will work.

Plz make sure to like my answer if your problem solved....

Thanks and Regards Naresh Avari
Re: populating data to dropdown list and text field from sql table
Posted on: 16 Aug 2012  
Load the combobox from sql server on pageload like this

private void Form1_Load(object sender, EventArgs e)
{
SqlDataAdapter ad = new SqlDataAdapter("select *from Reg",con);
 DataSet ds = new DataSet();
 ad.Fill(ds, "Reg");
 comboBox1.DataSource = ds.Tables["Reg"].DefaultView;
comboBox1.DisplayMember = "name";
 comboBox1.ValueMember = "name";
}

Then in the combobox SelectedIndexChanged event,

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { SqlDataAdapter ad = new SqlDataAdapter("select *from Reg where name='" + comboBox1.SelectedItem.ToString()+ "'", con);
 DataSet ds = new DataSet();
 ad.Fill(ds, "Reg");
textBox1.Text = ds.Tables["Reg"].Rows[0]["Name"].ToString();
textBox2.Text = ds.Tables["Reg"].Rows[0]["Department"].ToString();
textBox3.Text = ds.Tables["Reg"].Rows[0]["Designation"].ToString();
  }


Thanks & Regards,
Santhosh

Please mark this answer as accepted answer if it resolves your problem.
Re: populating data to dropdown list and text field from sql table
Posted on: 16 Aug 2012  
Hi,

Let me give a sample:

Dropdownlist with OnSelectedIndexChanged event and a TextBox:

<asp:DropDownList ID="ddlEmp" runat="server" 
                        onselectedindexchanged="ddlEmp_SelectedIndexChanged"></asp:DropDownList><br>
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>

Bind ddlEmp in Page_Load like this:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           string connStr = Declare Your connection string here
        using (SqlConnection con = new SqlConnection(connStr))
        {
            con.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT EmployeeId FROM Employees", con))
            {
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        ListItem li = new ListItem();
                        li.Value = dr["EmployeeId"].ToString();
                        li.Text = dr[" EmployeeId "].ToString();
                        ddlEmp.Items.Add(li);
                    }
                }
            }
        }
        }
    }


and in ddlEmp_selectedindexchanged:

protected void ddlEmp_SelectedIndexChanged(object sender, EventArgs e)
    {
         using (SqlConnection con = new SqlConnection(connStr))
        {
            con.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM Employees WHERE employeeid='" + ddlEmp.SelectedValue + "'", con))
            {
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                   dr.Read();
                   if(dr.HasRows)
                   {
                      txt1.Text = dr["EmployeeName"].ToString();
                   }
                }
            }
        }

    }


Thanks and Regards Naresh Avari
Re: populating data to dropdown list and text field from sql table
Posted on: 16 Aug 2012  
Hi, I can only see the data populated to dropdown list and the textbox is not getting loaded when I clicked the dropdown.please reply me ASAP
Re: populating data to dropdown list and text field from sql table
Posted on: 17 Aug 2012  
Hi Karthika,


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Dropdownlist_click_data_in_textbox._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server"
            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList><br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </div>
   
    </form>
</body>
</html>



using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Dropdownlist_click_data_in_textbox
{
    public partial class _Default : System.Web.UI.Page
    {
        string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        string str;
        SqlCommand com;
        protected void Page_Load(object sender, EventArgs e)
        {
            DropDownList1.AutoPostBack = true;
            SqlConnection con = new SqlConnection(strConnString);

            if (!IsPostBack)
            {
                DropDownList1.Items.Add("Choose id");
                con.Open();
                str = "select * from  test";
                com = new SqlCommand(str, con);
                SqlDataReader reader = com.ExecuteReader();
                while (reader.Read())
                {
                    DropDownList1.Items.Add(reader["id"].ToString());
                }
                reader.Close();
                con.Close();
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(strConnString);
            con.Open();
            str = "select * from test where id='" + DropDownList1.SelectedItem.Text + "'";
            com = new SqlCommand(str, con);
            SqlDataReader reader = com.ExecuteReader();
            while (reader.Read())
            {
                TextBox1.Text = reader["name"].ToString();
               
            }
            reader.Close();
            con.Close();
        }
    }
}



Thanks

If this post helps you mark it as answer
Thanks

Re: populating data to dropdown list and text field from sql table
Posted on: 07 Jul 2013  

Hi ,

I have modified the code as below...but now it is saying :  There is no row at position 0.

Dim sqlcmd As String = ("select * From DP_Table Where P_id = '" & DropDownList1.SelectedItem.Value & "'")
Dim ad As New SqlDataAdapter(sqlcmd, cn)

Dim ds As New DataSet
ad.Fill(ds, "DP_Table")

TB1.Text = ds.Tables("DP_Table").Rows(0)("P_id").ToString
TB2.Text = ds.Tables("DP_Table").Rows(0)("P_Name").ToString


SPONSORED BY

Offshore Software and Database Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.