Link to home
Start Free TrialLog in
Avatar of thomasm1948
thomasm1948Flag for United States of America

asked on

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;
               
            }
        }
Avatar of Martman100
Martman100

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.
Avatar of thomasm1948

ASKER

I did catch that and they are pointing to the correct ids.
ASKER CERTIFIED SOLUTION
Avatar of thomasm1948
thomasm1948
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I figured it out but I still want to post it so that others do not run into the same issue