title: How to update one column using a DropDownList inside a GridView?
body: <p>I am a new ASP.NET developer and I am struggling with updating a column in the GridView with a DropDownList. In details, I have a GridView that I am using it to add, delete and update/edit the employees information. This information is employee Username, Name, BadgeNo, JobTitle, and the DivisionShortcut. The Divisions will be listed in a DropDownList, but still I cannot update this column and I don't know why.</p>
  
  <p>I have the following database design:</p>
  
  <pre><code>Employee Table: Username, Name, JobTitle, BadgeNo, IsActive, DivisionCode
  Divisions Table: SapCode, DivisionShortcut
  </code></pre>
  
  <p>I wrote the code and I don't find any error during debugging the code. However, when I run the code and selected one of the values from the dropdownlist, the column doesn't update and I don't know why. So how to get this column updated inside the GridView? </p>
  
  <p>ASP.NET Code:</p>
  
  <pre><code>&lt;asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
              AutoGenerateColumns="False" DataKeyNames="Username" 
              DataSourceID="SqlDataSource1" BorderWidth="1px" BackColor="#DEBA84" 
               CellPadding="3" CellSpacing="2" BorderStyle="None" 
               BorderColor="#DEBA84" OnRowEditing="GridView1_RowEditing" 
               OnRowCancelingEdit="GridView1_RowCancelingEdit" 
               OnRowUpdating="GridView1_RowUpdating"&gt;
              &lt;FooterStyle ForeColor="#8C4510" 
                BackColor="#F7DFB5"&gt;&lt;/FooterStyle&gt;
              &lt;PagerStyle ForeColor="#8C4510" 
                HorizontalAlign="Center"&gt;&lt;/PagerStyle&gt;
              &lt;HeaderStyle ForeColor="White" Font-Bold="True" 
                BackColor="#A55129"&gt;&lt;/HeaderStyle&gt;
              &lt;Columns&gt;
                  &lt;asp:CommandField ButtonType="Image" ShowEditButton="true" ShowCancelButton="true"
                                  EditImageUrl="Images/icons/edit24.png" UpdateImageUrl="Images/icons/update24.png" 
                                  CancelImageUrl="Images/icons/cancel324.png" /&gt;
  
                  &lt;asp:TemplateField HeaderText="Division"&gt;
                      &lt;ItemTemplate&gt;
                          &lt;%# Eval("DivisionShortcut")%&gt;
                      &lt;/ItemTemplate&gt;
                      &lt;EditItemTemplate&gt;
                          &lt;asp:DropDownList ID="DivisionsList" runat="server" DataSourceID="DivisionsListDataSource"
                                            AutoPostBack="true" DataTextField="DivisionShortcut" DataValueField="SapCode"
                                            selectedValue='&lt;%# Bind("DivisionShortcut") %&gt;'&gt;
                          &lt;/asp:DropDownList&gt;
                      &lt;/EditItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
  
                  &lt;asp:BoundField DataField="Username" HeaderText="Network ID" ReadOnly="True" 
                      SortExpression="Username" /&gt;
  
                  &lt;asp:TemplateField HeaderText="Name"&gt;
                      &lt;ItemTemplate&gt;
                          &lt;%# Eval("Name")%&gt;
                      &lt;/ItemTemplate&gt;
                      &lt;EditItemTemplate&gt;
                          &lt;asp:TextBox ID="txtEmployeeName" runat="server" Text='&lt;%# Bind("Name")%&gt;' /&gt;
                      &lt;/EditItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
  
                  &lt;asp:TemplateField HeaderText="Job Title"&gt;
                      &lt;ItemTemplate&gt;
                          &lt;%# Eval("JobTitle")%&gt;
                      &lt;/ItemTemplate&gt;
                      &lt;EditItemTemplate&gt;
                          &lt;asp:TextBox ID="txtJobTitle" runat="server" Text='&lt;%# Bind("JobTitle")%&gt;' /&gt;
                      &lt;/EditItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
  
                  &lt;asp:TemplateField HeaderText="Badge No."&gt;
                      &lt;ItemTemplate&gt;
                          &lt;%# Eval("BadgeNo")%&gt;
                      &lt;/ItemTemplate&gt;
                      &lt;EditItemTemplate&gt;
                          &lt;asp:TextBox ID="txtBadgeNo" runat="server" Text='&lt;%# Bind("BadgeNo")%&gt;' /&gt;
                      &lt;/EditItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
  
                  &lt;asp:TemplateField HeaderText="Is Active?"&gt;
                      &lt;ItemTemplate&gt;
                          &lt;%# Eval("IsActive")%&gt;
                      &lt;/ItemTemplate&gt;
                      &lt;EditItemTemplate&gt;
                          &lt;asp:CheckBox ID="isActive" runat="server" 
                                        Checked='&lt;%# Eval("IsActive").ToString().Equals("True") %&gt;'/&gt;
                      &lt;/EditItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
  
                  &lt;asp:TemplateField HeaderText="Delete?"&gt;
                      &lt;ItemTemplate&gt;
                          &lt;span onclick="return confirm('Are you sure to Delete the record?')"&gt;
                              &lt;asp:ImageButton ID="lnkB" runat="server" ImageUrl="Images/icons/delete24.png" CommandName="Delete" /&gt;
                          &lt;/span&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
              &lt;/Columns&gt;
          &lt;/asp:GridView&gt;
          &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" 
              ConnectionString="&lt;%$ ConnectionStrings:UsersInfoDBConnectionString %&gt;" 
  
              SelectCommand="SELECT     dbo.Divisions.DivisionShortcut,dbo.employee.DivisionCode dbo.employee.Username, dbo.employee.Name, dbo.employee.JobTitle, dbo.employee.BadgeNo, dbo.employee.IsActive
                  FROM         dbo.Divisions INNER JOIN
                              dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode"
              UpdateCommand="UPDATE [employee] SET [Name] = @Name, [JobTitle] = @JobTitle, 
                                                  [BadgeNo] = @BadgeNo WHERE [Username] = @Username"
              DeleteCommand="DELETE FROM [employee] WHERE [Username] = @Username"&gt;
              &lt;UpdateParameters&gt;
                  &lt;asp:Parameter Name="Name" Type="String"  /&gt;
                  &lt;asp:Parameter Name="JobTitle" Type="String" /&gt;
                  &lt;asp:Parameter Name="BadgeNo" Type="String" /&gt;
                  &lt;asp:Parameter Name="Username" Type="String" /&gt;
              &lt;/UpdateParameters&gt;
              &lt;DeleteParameters&gt;
                  &lt;asp:Parameter Name="Username" Type="String" /&gt;
              &lt;/DeleteParameters&gt;
          &lt;/asp:SqlDataSource&gt;
  </code></pre>
  
  <p><strong>And outside the GridView, I have the SqlDataSource for the DropDownList:</strong></p>
  
  <pre><code>&lt;asp:SqlDataSource ID="DivisionsListDataSource" runat="server" 
      ConnectionString="&lt;%$ ConnectionStrings:UsersInfoDBConnectionString %&gt;" 
      SelectCommand="SELECT * FROM Divisions"
      InsertCommand="INSERT INTO [Divisions] ([SapCode], [DivisionShortcut]) VALUES (@SapCode, @DivisionShortcut)"
      UpdateCommand="UPDATE [Divisions] SET [DivisionShortcut] = @DivisionShortcut WHERE [SapCode] = @SapCode"&gt;
      &lt;DeleteParameters&gt;
          &lt;asp:Parameter Name="SapCode" Type="Double" /&gt;
      &lt;/DeleteParameters&gt;
      &lt;InsertParameters&gt;
          &lt;asp:Parameter Name="SapCode" Type="Double" /&gt;
          &lt;asp:Parameter Name="DivisionShortcut" Type="String" /&gt;
      &lt;/InsertParameters&gt;
      &lt;UpdateParameters&gt;
          &lt;asp:Parameter Name="DivisionShortcut" Type="String" /&gt;
          &lt;asp:Parameter Name="SapCode" Type="Double" /&gt;
      &lt;/UpdateParameters&gt;
  &lt;/asp:SqlDataSource&gt;
  </code></pre>
  
  <p>Code-Behind:</p>
  
  <pre><code>//For editing any row in the GridView
      protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) 
      {
          GridView1.EditIndex = e.NewEditIndex;
      }
  
      //For canceling any editng in any row in the GridView
      protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
      {
          e.Cancel = true;
          GridView1.EditIndex = -1;
      }
  
      //For updating the information in any row in the GridView
      protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
      {
          GridViewRow gvrow = GridView1.Rows[e.RowIndex];
  
          DropDownList DivisionsList = (DropDownList)gvrow.FindControl("DivisionsList"); 
  
          TextBox txtEmployeeName = (TextBox)gvrow.FindControl("txtEmployeeName");
          TextBox txtJobTitle = (TextBox)gvrow.FindControl("txtJobTitle");
          TextBox txtBadgeNo = (TextBox)gvrow.FindControl("txtBadgeNo");
  
          CheckBox isActive = (CheckBox)gvrow.FindControl("isActive");
  
          //For getting the ID (primary key) of that row
          string username = GridView1.DataKeys[e.RowIndex].Value.ToString();
  
          string name = txtEmployeeName.Text;
          string jobTitle = txtJobTitle.Text;
          string badgeNo = txtBadgeNo.Text;
          string division = DivisionsList.Text;
          //string divisioncode = DivisionsList.SelectedValue;
  
          UpdateEmployeeInfo(username, name, jobTitle, badgeNo, division);
      }
  
  
      private void UpdateEmployeeInfo(string username, string name, string jobTitle, string badgeNo, string division)
      {
          string connString = ConfigurationManager.ConnectionStrings["UsersInfoDBConnectionString"].ConnectionString;
          SqlConnection conn = new SqlConnection(connString);
          string update = @"UPDATE Employee SET Name = @Name, JobTitle = @JobTitle, 
                              BadgeNo = @BadgeNo WHERE Username = @Username";
  
          SqlCommand cmd = new SqlCommand(update, conn);
  
          cmd.Parameters.AddWithValue("@Name", name);
          cmd.Parameters.AddWithValue("@JobTitle", jobTitle);
          cmd.Parameters.AddWithValue("@BadgeNo", badgeNo);
          cmd.Parameters.AddWithValue("@division", division);
          //cmd.Parameters.AddWithValue("@SapCode", divisioncode);
          cmd.Parameters.AddWithValue("@Username", username);
          //cmd.Parameters.AddWithValue("@IsActive", isActive.checked);
  
          try
          {
              conn.Open();
              cmd.ExecuteNonQuery();
              conn.Close();
  
              GridView1.EditIndex = -1;
          }
          catch (Exception ex)
          {
              throw ex;
          }
          finally
          {
              cmd.Dispose();
              conn.Close();
              conn.Dispose();
          }
          GridView1.DataBind();
      }
  
      //For deleting a row in the GridView
      protected void DeleteRecord(object sender, GridViewDeleteEventArgs e) 
      {
          string networkID = GridView1.DataKeys[e.RowIndex].Value.ToString();
  
          string connString = ConfigurationManager.ConnectionStrings["UsersInfoDBConnectionString"].ConnectionString;
          SqlConnection conn = new SqlConnection(connString);
          string deleteCommand = "DELETE employee where dbo.employee.Username = " + networkID;
          SqlCommand cmd = new SqlCommand(deleteCommand, conn);
          cmd.Parameters.AddWithValue("@username", networkID);
          try
          {
              conn.Open();
              cmd.ExecuteNonQuery();
              conn.Close();
          }
          catch (SqlException se)
          {
              throw se;
          }
          finally
          {
              cmd.Dispose();
              conn.Close();
              conn.Dispose();
          }
          GridView1.DataBind();
      }
  </code></pre>