Georgios
Georgios

Reputation: 1483

C# DropDownList listing number value

I have a dropdownlist that fills like:

cmd.CommandText = "select * from dbo.pelischool";
                        SqlDataAdapter da = new SqlDataAdapter(cmd);
                        DataSet ds = new DataSet();
                        da.Fill(ds);
                        DropDownList1.DataTextField = ds.Tables[0].Columns["schoolName"].ToString();
                        DropDownList1.DataSource = ds.Tables[0];
                        DropDownList1.DataBind();

Now I want to read the listing number of the selected item(in order to place it in a query). So, while my dropdownlist has values like "Oxford University", "MIT University", "Simon Fraser University" etc, I want clicking an item from the list, store the value of this item in the list. To be more specific, if clicking the first item, then int number = 1. if clicking the second item, then int number = 2.

Is it possible to be done with something like:

DropDownList1.SelectedItem.????;

Thank you for your time!

Upvotes: 2

Views: 955

Answers (4)

Kinjal Patel
Kinjal Patel

Reputation: 420

You didn't bind item's value to dropdownlist, so you need to bind it.

like this

DropDownList1.DataValueField = ds.Tables[0].Columns["Column name that contain the value of Item"].ToString();

and then you can use to get it's value like as follow

string item_value = DropDownList1.SelectedValue;

Upvotes: 3

Saeed ur Rehman
Saeed ur Rehman

Reputation: 737

    cmd.CommandText = "select * from dbo.pelischool";
                            SqlDataAdapter da = new SqlDataAdapter(cmd);
                            DataSet ds = new DataSet();
                            da.Fill(ds);
                            DropDownList1.DataTextField = ds.Tables[0].Columns["schoolName"].ToString();
                            DropDownList1.DataValueField = ds.Tables[0].Columns["SomeValueColumn or id column"];
                            DropDownList1.DataSource = ds.Tables[0];
                            DropDownList1.DataBind();

and then you can use...

string id = DropDownList1.SelectedValue.ToString();

Try this.

 cmd.CommandText = "select id,schoolName from dbo.pelischool";
 SqlDataAdapter da = new SqlDataAdapter(cmd);
 DataTable dt = new DataTable();
 da.Fill(dt );
 if (dt != null)
        {
            if (dt.Rows.Count > 0)
            {
                ddlPosition.DataSource = dt;
                ddlPosition.DataTextField = "schoolName";
                ddlPosition.DataValueField = "Id";
                ddlPosition.DataBind();
            }
        }

Upvotes: 0

Sami
Sami

Reputation: 3800

In addition to Nitin Kumar answer, selected item text can be obtained by:

var selectedText = DropDownList1.SelectedItem.Text;

Selected Value

var selectedValue = DropDownList1.SelectedValue;

Upvotes: 0

Nitin Kumar
Nitin Kumar

Reputation: 898

If you want selected Value then use

DropDownList1.SelectedValue

and if you want selected item then use

DropDownList1.SelectedItem

Upvotes: 2

Related Questions