Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi,
I m trying below code to display data from database in dropdownlist as well as textbox
 
but Textboses are working fine but Dropdownlist is not showing data of database.
 
Pls Help..
 
 
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())
        {
            txtRegdt.Text = Convert.ToString(reader["Reg_dt"]);
            txtComplnno.Text = Convert.ToString(reader["Complaint_no"]);
            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();
 
Posted 3-Jan-13 4:10am
Edited 3-Jan-13 4:17am
Raje_7.4K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

First, on your page load event fill the list of states:
protected void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
fillStates();
}
}
//function to fill state list
protected void fillStates()
{
SqlCommand cmd = default(SqlCommand);
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Callregister_connectionstring"].ConnectionString);
con.Open();
cmd = new SqlCommand("Select distinct stateID,stateName from YourStateTable", con);
SqlDataReader reader = cmd.ExecuteReader();
 
while (reader.Read())
{
ListItem li = new ListItem();
li.Text = Convert.ToString(reader["stateName"]);
li.Value = Convert.ToString(reader["stateID"]);
ddlstat.Items.Add(li);
}
con.close();
}
 
Now on you button click event write
protected void btnDetails_Click(object sender, System.EventArgs e)
{
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();
txtRegdt.Text = Convert.ToString(reader["Reg_dt"]);
txtComplnno.Text = Convert.ToString(reader["Complaint_no"]);
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.SelectedValue = Convert.ToString(reader["state"]);
/*the above line willl set the value to the stored value if you are storing id 
of the state in the database. If you are storing state as text in the database replace the line with following line:
ddlstat.SelectedIndex =
ddlstat.Items.IndexOf(ddlstat.Items.FindByValue(Convert.ToString(reader["state"])))
*/
con.close();
}
  Permalink  
v2
Comments
Member 9693583 at 3-Jan-13 9:41am
   
Yes Zafan, I m passing complaint no. & filling the data from query on button click.... Ohhhoo.... this code is not working on button click.... pls help me...
Zafar Sultan at 3-Jan-13 9:45am
   
OK, I will update my reply in a while.
Zafar Sultan at 3-Jan-13 10:10am
   
Please check the updated reply.
Member 9693583 at 3-Jan-13 10:53am
   
Yuuuuuuhuuuuu..... Done... Thanks Zafan... U dont understand wht u have done.... thaku thanku so much...
Zafar Sultan at 4-Jan-13 2:56am
   
It's all right :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Try this: For dropdown list only
private void dropdownlistbind()
    {
       
        SqlCommand cmd = new SqlCommand("Your Command using appropriate clause","Your Connection");
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DropDownList1.DataSource = ds;
        // FieldName of Table in DataBase
        DropDownList1.DataValueField = "FieldName";
        DropDownList1.DataBind();   
      }	
  Permalink  
Comments
Member 9693583 at 3-Jan-13 9:56am
   
hi prasad, tried below code & call it on button click.... exception error
 
"Invalid operation exception was unhandled by user code" pls help..
 

private void ddlstatebind()
{
SqlCommand cmd = new SqlCommand("Select state from Call_Reg ", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ddlstat.DataSource = ds;
ddlstat.DataValueField = "state";
ddlstat.DataBind();
}
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

First - Bind the dropdown on page_load event.
 
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())
{
txtRegdt.Text = Convert.ToString(reader["Reg_dt"]);
txtComplnno.Text = Convert.ToString(reader["Complaint_no"]);
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.SelectedIndex =
    ddlstat.Items.IndexOf(ddlstat.Items.FindByValue(Convert.ToString(reader["state"])))
 

i++;
}
con.close();
  Permalink  
v2
Comments
Member 9693583 at 3-Jan-13 9:30am
   
hi vaibhav,Thanks 4 reply... I tried that code but its not working i want the code on button click event

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

  Print Answers RSS
0 OriginalGriff 290
1 Jochen Arndt 150
2 Kornfeld Eliyahu Peter 149
3 PIEBALDconsult 110
4 Richard MacCutchan 105
0 OriginalGriff 6,080
1 DamithSL 4,648
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,624
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web03 | 2.8.141220.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