0

I am displaying values from a database into a dropdownlist. but I am unable to get the corresponding values into the DDL.

if (dt.Rows.Count > 0)
            {
                DataTable dt1 = new DataTable();
                dt1 = bll.getnewid(TextBox1.Text);

                    if (dt1.Rows.Count > 0)
                    {

                        Session["pid"] = dt1.Rows[0]["NewidColumn"].ToString();
                        Session["email"] = dt1.Rows[0]["EmailID"].ToString();
                        Session["gender"] = dt1.Rows[0]["Gender"].ToString();
                        Session["mobile"] = dt1.Rows[0]["MobileNo"].ToString();
                        Session["country"] = dt1.Rows[0]["Country"].ToString();
                        Session["state"] = dt1.Rows[0]["State"].ToString();
                      }

and I am displaying like this

   DropDownList1.Text = Session["country"].ToString();
            DropDownList2.Text = Session["state"].ToString();

I am able to get the country and state values in the datatable. but I am unable to display them in the DDL.

6 Answers 6

1
DropDownList1.Items.Add(new ListItem(Session["country"].ToString()); 
DropDownList2.Items.Add(new ListItem(Session["state"].ToString());
Dropdownlist2.databind();
Dropdownlist1.databind();
Sign up to request clarification or add additional context in comments.

6 Comments

When i do like this i am getting the values but in country ddl it is showing -select a country- instead the country name,how to display country name directly.
DropDownList2.Items.Add(new ListItem(Session["ID"].ToString(),Session["countryname"].ToString());
What is session["ID"] here?? I have not assigned anything to it probably Object reference not set to an instance of an object error will occur.
it means you have to add counrty id to session ..before use
Add this statement DropDownList2.SelectedIndex = 0;
|
1
DropDownList1.Items.Add(new ListItem(Session["country"].ToString()); 
DropDownList2.Items.Add(new ListItem(Session["state"].ToString());

1 Comment

Argument '1': cannot convert from 'System.Web.UI.WebControls.ListItem' to 'System.Web.UI.Control' I am getting an error like this.
0

You need to set the SelectedValue or SelectedIndex property of your dropdownlist.

Comments

0

try like this

   DropDownList1.Items.Add("yourvalue");
   DropDownList1.Items.Add(Session["country"].ToString());

Comments

0

Try This Code :

DropDownList1.Items.Insert(0, new ListItem(Session["country"].ToString(), "0"));
DropDownList1.DataBind();

DropDownList2.Items.Insert(0, new ListItem(Session["state"].ToString(), "0"));
DropDownList2.DataBind();

Comments

0

What is the need of using sessions ..? Instead try this..hope it works. After this line of code (dt1 = bll.getnewid(TextBox1.Text);) use this two lines instead of sessions.

DropDownList1.DataSource=dt1;
DropDownList1.DataTextField="Country";
DropDownList1.DataValueField="Country";
DropDownList1.DataBind();
DropDownList2.DataSource=dt1;
DropDownList2.DataValueField="Country";
DropDownList2.DataValueField="Country";
DropDownList2.DataBind();

6 Comments

here country and state are the names of the columns in the data table dt1. make sure column names match to the dt1.
I am catching the country and state values and displaying in the another page so i am using session.
do u mean to say the selected country and state or the entire list of countries and states..?
I am populating the values from DB which are selected by the use and displaying them in the DDL not the list.
From the code you have written you will get only the first row of the dt in sessions .Instead can't u store the dt in session and typecast it to datatable while accessing the values ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.