ASP.Net/C# - Dropdownlist Question selected value

Hello all.  I have a dropdown list that gets bound to a dataset I have.  In that dataset there is a field called Primary and its either Y or N.  This field is in the dataset but not my DataValueField or DataTextField.  What I want to do is after the bind select the value that has the Primary = Y.  Any idea how I can do that after the bind?  thanks all
sbornstein2Asked:
Who is Participating?
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.

strickddCommented:
dropdownObj.SelectedValue = "VALUE OF PRIMARY"

OR

for(int i=0; i<ROWCOUNT; i++)
{
    ListItem item = new ListItem(reader[TEXTFIELD].ToString(), reader[VALUEFIELD].ToString());
   
    dropdownObj.Items.Add(item);

    if(Convert.ToBoolean(reader[PRIMARY]))
    {
        dropdownObj.SelectedValue = reader[TEXTFIELD].ToString();
    }

    reader.MoveNext();
}
0
sbornstein2Author Commented:
dropdownObj.SelectedValue = "VALUE OF PRIMARY" would that be something like

dropdownObj.SelectedValue = "Primary = Y" or something?   Again the Primary is not actually the value field or the text field in the drop.
0
strickddCommented:
If you want to do it all with one SQL call you will have to loop through the DataReader and create each list item and check if it is primary or not. If you want to just use "dropdownObj.SelectedValue = "VALUE OF PRIMARY"" then you will need to do another SQL call to get the primary: SELECT TEXTFIELDCOLUMN FROM TABLE WHERE PRIMARY = 'Y'
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

spdudeCommented:
Hi sbornstein2

First loop through each row to find which row has primary value = y
DataSet ds = new DataSet();
        int index = 0;
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            index++;

            if (dr["primary"] == "Y")
            {
                break;
            }
        }


After finding the index set dropdown selected index to found index
        DropDownList1.SelectedIndex = index;
   

regards
snehal
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
sbornstein2Author Commented:
actually I have a dataview is it the same code for the dataset?  thanks guys
0
strickddCommented:
I believe they are similar. Either way you should be able to loop through the set and find the Primary = Y and set the .SelectedValue equal to that rows value.
0
sbornstein2Author Commented:
thank you
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.

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.