SQLDataReder Select dropdownlist value in GridView

Hi,

I am having issues with selecting a value in a dropdownlist that is located in a gridview.  My GridView allows for editing and I am trying to have it display the current value that is in my database. Below is my code:

Littile of the HTML portion:

<asp:GridView ID="Users_GridView1" AutoGenerateColumns="False" onrowediting="Users_GridView1_RowEditing"
          onrowcancelingedit="Users_GridView1_RowCancelingEdit"  OnRowUpdating="Users_GridView1_RowUpdating" OnRowDeleting="Users_Gridview1_RowDeleting"  
            runat="server" onselectedindexchanged="Users_GridView1_SelectedIndexChanged"  >    
        <Columns>  
         <asp:TemplateField HeaderText="Edit" ShowHeader="False" HeaderStyle-HorizontalAlign="Left">
                <EditItemTemplate>
                    <asp:LinkButton ID="lbkUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
                    <asp:LinkButton ID="lnkCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                    <asp:LinkButton ID="lnkDel" CausesValidation="False" CommandName="Delete" Text="Delete" runat="server"></asp:LinkButton>
                </EditItemTemplate>            
               
                <ItemTemplate>
                   <asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
                </ItemTemplate>
                <HeaderStyle HorizontalAlign="Left" />
     </asp:TemplateField>  


<asp:TemplateField HeaderText="Access" HeaderStyle-HorizontalAlign="Left">
                <EditItemTemplate>
                    <asp:DropDownList ID="Acess" runat="server" >
                    <asp:ListItem Text="Single Site" Value="1" />
                    <asp:ListItem Text="Multi Site" Value="2" />
                    <asp:ListItem Text="Admin" Value="3" />
                    </asp:DropDownList>  
                </EditItemTemplate>              
                <ItemTemplate>
                    <asp:Label ID="Access_lbl" runat="server" Text='<%# Bind("Level") %>'></asp:Label>
                </ItemTemplate>
                <HeaderStyle HorizontalAlign="Left" />
       </asp:TemplateField>

Code portion

 protected void Users_GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {

            Users_GridView1.EditIndex = e.NewEditIndex;
            lblError.Text = "";
            //showStuff();
            try
            {
                lblError.Text = "";
                System.Data.SqlClient.SqlConnection SqlCon1 = new System.Data.SqlClient.SqlConnection();
                string sConnSTr1 = null;
                sConnSTr1 = WebConfigurationManager.ConnectionStrings["DentalEncDB"].ConnectionString;
                SqlCon1.ConnectionString = sConnSTr1;
                SqlCon1.Open();
                System.Data.SqlClient.SqlCommand sqlCommand0 = new System.Data.SqlClient.SqlCommand();
                sqlCommand0.CommandType = System.Data.CommandType.StoredProcedure;
                sqlCommand0.CommandText = "Select_Members";
                sqlCommand0.Connection = SqlCon1;
                SqlDataReader reader = sqlCommand0.ExecuteReader(CommandBehavior.CloseConnection);
                Users_GridView1.DataSource = reader;
                if (reader.Read())
                {
                    GridViewRow row = (GridViewRow)Users_GridView1.Rows[e.NewEditIndex];
                    DropDownList ddl = (DropDownList)row.FindControl("Acess");
                    string test1 = reader[5].ToString();
                   
                    ddl.Items.FindByValue(test1).Selected = true;
                    //ddl.SelectedValue = reader[5].ToString();

                }
               
                Users_GridView1.DataBind();
                SqlCon1.Close();
                Users_GridView1.HeaderRow.Cells[1].Visible = false;
                foreach (GridViewRow gvr in Users_GridView1.Rows)
                {
                    gvr.Cells[1].Visible = false;
                }
            }
            catch (Exception ex)
            {
                Users_GridView1.Visible = false;
                lblError.Text = ex.Message;
               
            }
        }
thomasm1948Asked:
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.

Martman100Commented:
Are the items in the DDL of type string?

Also confirm your DDL is named Acess and not Access. I notice one of your labels has an id of Access_lbl.
0
thomasm1948Author Commented:
I did catch that and they are pointing to the correct ids.
0
thomasm1948Author Commented:
I figured it out

I just needed to added the following to the dropdownlist:

SelectedValue='<%# Bind("Level") %>'
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
thomasm1948Author Commented:
I figured it out but I still want to post it so that others do not run into the same issue
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
C#

From novice to tech pro — start learning today.