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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
robastaCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.