title: Update GridView with Dropdownlist control
body: <p>I am using VS2005 C#.</p>
  
  <p>Currently I have a GridView, and I have changed one of my GridView control to my column name <code>Gender</code>, from the default <code>TextBox</code> to a <code>DropDownList</code>, which I gave the <code>ID</code> of the control to <code>GenderList</code>, and it contains 2 values, <em>M</em> and <em>F</em>.</p>
  
  <p>I have a default update statement which is able to update the GridView after <code>edit</code>, which is the following:</p>
  
  <pre><code>&lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString=
  "&lt;%$ ConnectionStrings:SODConnectionString %&gt;" UpdateCommand="UPDATE 
  [UserMasterData] SET [Name] = @Name, [Age] = @Age, [ContactNo]=@ContactNo,
   [Address]=@Address, [Gender]=@Gender"/&gt;
  </code></pre>
  
  <p>The above UPDATE query works perfectly, and now I have changed my <code>Gender</code> <em>textbox</em> to a <em>dropdownlist</em>, the <code>UPDATE</code> query gave me an error which says:</p>
  
  <p><code>Must declare the scalar variable "@Gender".</code></p>
  
  <p>I assume the UPDATE query couldn't find the value from my <code>Gender</code> column.</p>
  
  <p>I tried to modify the UPDATE query to <code>@GenderList</code>, but it did not work as well.</p>
  
  <p>Anyone knows what I should do do the UPDATE query so that my UPDATE query can find the value from my <code>GenderList</code> dropdownlist in my <code>Gender</code> column?</p>
  
  <p>Thank you.</p>
  
  <hr>
  
  <p>Below is my previous <code>Gender</code> column with a textbox control:</p>
  
  <pre><code>                &lt;asp:BoundField  HeaderText="Gender" 
                  DataField="Gender" 
                  SortExpression="Gender"&gt;&lt;/asp:BoundField&gt;
  </code></pre>
  
  <p>Below is my <code>Gender</code> with the dropdownlist control:</p>
  
  <pre><code>    &lt;asp:TemplateField HeaderText="Gender" SortExpression="Gender" &gt;
          &lt;EditItemTemplate&gt;
              &lt;asp:DropDownList ID="GenderList" runat="server" Width="50px" &gt;
                  &lt;asp:ListItem&gt;M&lt;/asp:ListItem&gt;
                  &lt;asp:ListItem&gt;F&lt;/asp:ListItem&gt;
              &lt;/asp:DropDownList&gt;
          &lt;/EditItemTemplate&gt;
          &lt;ItemTemplate&gt;
  </code></pre>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>Tried implementing  <code>RowDatBound</code> and <code>onRowUpdating</code>:</p>
  
  <hr>
  
  <p><code>RowDatBound</code></p>
  
  <hr>
  
  <pre><code>protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
      DataRowView dRowView = (DataRowView)e.Row.DataItem;
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
          if ((e.Row.RowState &amp; DataControlRowState.Edit) &gt; 0)
          {
              DropDownList genderList= (DropDownList)e.Row.FindControl("GenderList");
              genderList.SelectedValue = dRowView[2].ToString();
          }
      }
  }
  </code></pre>
  
  <hr>
  
  <p><code>RowUpdating</code>.aspx.cs</p>
  
  <hr>
  
  <pre><code>protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {
  
  DropDownList genderSelect =(DropDownList)GridView1.Rows[e.RowIndex].FindControl("GenderList");
  
  SqlDataSource1.UpdateParameters["Gender"].DefaultValue = 
  genderSelect.SelectedValue; --&gt; error says not set to an instance of an object
  
  }
  </code></pre>