Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
How to write code to fetch data from database in dropdownlist. Textboxes are working fine.
 

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 3-Jan-13 1:47am
Edited 3-Jan-13 1:50am
digimanus30.5K
v2
Comments
digimanus at 3-Jan-13 6:52am
   
just google: and find
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Suppose you have adropdownlist "drpCities" you want to show city list using this dropdownlist. Here is the sample code:
 
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();
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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(); 
  Permalink  
Comments
Member 9693583 at 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();
}
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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();
  Permalink  
v2
Comments
Turbo_23 at 3-Jan-13 7:22am
   
hi aarti,
 
what is wrong in solution 2 ?
Member 9693583 at 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
vaibhavmarne at 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"])))
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

hi,
 
int i = 0;
        while (reader.Read())
        {
            ddl.Items.Add(new ListItem(Convert.ToString(reader["Cust_FName"]), i.ToString()));
            i++;
        }
  Permalink  

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

  Print Answers RSS
0 BillWoodruff 260
1 Afzaal Ahmad Zeeshan 252
2 OriginalGriff 251
3 Mathew Soji 234
4 Maciej Los 230
0 OriginalGriff 6,419
1 Sergey Alexandrovich Kryukov 6,008
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,070


Advertise | Privacy | Mobile
Web04 | 2.8.1411019.1 | Last Updated 3 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100