Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How to write code to fetch data from database in dropdownlist. Textboxes are working fine.


C#
SqlCommand cmd = default(SqlCommand);
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Callregister_connectionstring"].ConnectionString);
        con.Open();
        cmd = new SqlCommand("Select * from Call_Reg where Complaint_no='" + ddlupCompl.SelectedItem.ToString() + "'", con);
        SqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            txtFname.Text = Convert.ToString(reader["Cust_FName"]);
            txtLname.Text = Convert.ToString(reader["Cust_LName"]);
            txtPh.Text = Convert.ToString(reader["ph_no"]);
            txtMob.Text = Convert.ToString(reader["mob_no"]);
            txtemail.Text = Convert.ToString(reader["email"]);
            txtadd.Text = Convert.ToString(reader["address"]);
            txtcity.Text = Convert.ToString(reader["city"]);
            txtdist.Text = Convert.ToString(reader["dist"]);
            
        }
            con.Close();
Posted
Updated 3-Jan-13 0:50am
v2
Comments
Herman<T>.Instance 3-Jan-13 6:52am    
just google: and find

Suppose you have adropdownlist "drpCities" you want to show city list using this dropdownlist. Here is the sample code:

C#
SqlCommand cmd = default(SqlCommand);
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Callregister_connectionstring"].ConnectionString);
con.Open();
cmd = new SqlCommand("Select cityId,cityName from City order by cityName", con);
cmd.CommandType = CommandType.Text;
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(dt);
drpCities.DataSource = dt.DefaultView;
drpCities.DataTextField = "cityName";
drpCities.DataValueField = "cityId";
drpCities.DataBind();
 
Share this answer
 
here is the simple example:
cmd.CommandText = "Select field1, field2 from yourTable";
cmd.Connection = conn;
conn.Open();
DataTable dt  = new DataTable();
dt.Load(cmd.ExecuteReader());
conn.Close();
DDownList1.DataSource = dt;
DDownList1.DataTextField = "field1";
DDownList1.DataValueField = "field2";
DDownList1.DataBind(); 
 
Share this answer
 
Comments
Member 9693583 3-Jan-13 7:26am    
I tried below code but there is no change in dropdownlist.... Dropdownlist is not showing the item.



SqlCommand cmd = default(SqlCommand);
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Callregister_connectionstring"].ConnectionString);
con.Open();
cmd = new SqlCommand("Select * from Call_Reg where Complaint_no='" + ddlupCompl.SelectedItem.ToString() + "'", con);
SqlDataReader reader = cmd.ExecuteReader();
int i=0;
while (reader.Read())
{
txtFname.Text = Convert.ToString(reader["Cust_FName"]);
txtLname.Text = Convert.ToString(reader["Cust_LName"]);
txtPh.Text = Convert.ToString(reader["ph_no"]);
txtMob.Text = Convert.ToString(reader["mob_no"]);
txtemail.Text = Convert.ToString(reader["email"]);
txtadd.Text = Convert.ToString(reader["address"]);
txtcity.Text = Convert.ToString(reader["city"]);
txtdist.Text = Convert.ToString(reader["dist"]);
ddlstat.Items.Add(new ListItem(Convert.ToString(reader["state"]),i.ToString()));
i++;
}
con.Close();
}
C#
cmd.CommandText = " Select * From GetUsersID";
cmd.Connection = conn;
conn.Open();
DataTable dt  = new DataTable();
dt.Load(cmd.ExecuteReader());
conn.Close();
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "ID";
DropDownList1.DataBind();
 
Share this answer
 
v2
Comments
[no name] 3-Jan-13 7:22am    
hi aarti,

what is wrong in solution 2 ?
Member 9693583 3-Jan-13 7:32am    
Hi, Vaibhavmane, we have to insert list one by one or only one line code read all data for dropdownlist from database??? pls help.... thanks & regards,
kshitija
$*Developer - Vaibhav*$ 3-Jan-13 9:27am    
Hi kshitija,
In your code senario first you bind the drop down on page load event

cmd.CommandText = " Select StateName, id_State From State";
cmd.Connection = conn;
conn.Open();
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
conn.Close();
ddlstat.DataSource = dt;
ddlstat.DataTextField = "StateName";
ddlstat.DataValueField = "id_State";
ddlstat.DataBind();

then you write the below code to display the correct record.

ddlstat.SelectedIndex =
ddlstat.Items.IndexOf(ddlstat.Items.FindByValue(Convert.ToString(reader["state"])))
hi,

C#
int i = 0;
        while (reader.Read())
        {
            ddl.Items.Add(new ListItem(Convert.ToString(reader["Cust_FName"]), i.ToString()));
            i++;
        }
 
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