Show SelectedItem in Dropdownlist based on DataRow

i try to show the item in drop down list if the datarow returns let's say 2,
the ddl will select 2. but cannot
if (Request.QueryString["SLID"] != null && Request.QueryString["SLItemDelete"] == null)
            {
                DataSet1TableAdapters.SetLineTableAdapter adpt01 = new DataSet1TableAdapters.SetLineTableAdapter();
                DataTable dt = adpt01.GetSetLineData(Convert.ToInt64(Request.QueryString["SLID"]));

                ddlLevelID.SelectedItem.Text = dt.Rows[0]["LevelID"].ToString();
                ddlClassID.SelectedItem.Text = dt.Rows[0]["ClassID"].ToString();
                ddlSubjectID.SelectedItem.Text = dt.Rows[0]["SubjectID"].ToString();
                ddlTopicID.SelectedItem.Text = dt.Rows[0]["TopicID"].ToString();
                
            }

Open in new window

LVL 1
doramail05Asked:
Who is Participating?
 
robastaConnect With a Mentor Commented:
You need to bind the Dropdowns(if you havent).

Make Sure that the DataValueField for 'ddlLevelID' is set to 'LevelID' and do the same for the other ddls.

Also, If the fields are nullable, then you need to add a 'null' value on the Dropdowns (i.e. if Dropdown has values (1,2,3,4) and the item selected has "Null" or empty string, you will get that error.
0
 
krunal_shahCommented:
try like  this,
ddlLevelID.SelectedValue= dt.Rows[0]["LevelID"].ToString();
0
 
doramail05Author Commented:
had this

'ddlClassID' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
0
 
krunal_shahCommented:
>> 'ddlClassID' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value


This error shows that you are trying to set the value of dropdown which does not exists in the dropdown.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.