title: C# ASP.NET - Cannot update data through GridView
body: <p>I have 2 tables called company and customer. Company is the base table, and it has two column: CompanyID and Companyname. And the customer table has comapnyID as it's foreign key. </p>
  
  <p>I use gird view to display all the customerID, CustName, CustAddress and companyID. To update   companyID field, In the grid view, I use a drop down list which displays all the available companyID from where user can select one available companyID.</p>
  
  <p>But the problem is I try to update a particular customer with a new companyID by selecting one companyID from drop down list and click update. Everything works fine, however, in the updated customer record, for the companyID field it shows NULL value not the newly assigned companyID value. </p>
  
  <p>How can I fix this problem?</p>
  
  <p>This is the auto generated code VS2010:</p>
  
  <pre><code>&lt;asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
  AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="CustID" 
  DataSourceID="SqlDataSource1"&gt;
  &lt;Columns&gt;
  &lt;asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
  ShowSelectButton="True" /&gt;
  &lt;asp:BoundField DataField="CustID" HeaderText="CustID" InsertVisible="False" 
  ReadOnly="True" SortExpression="CustID" /&gt;
  &lt;asp:TemplateField HeaderText="CompanyID" SortExpression="CompanyID"&gt;
  &lt;EditItemTemplate&gt;
  &lt;asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
  DataSourceID="SqlDataSource1" DataTextField="CompanyName" 
  DataValueField="CompanyID"&gt;
  &lt;/asp:DropDownList&gt;
  &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" 
  ConnectionString="&lt;%$ ConnectionStrings:ICT_IdealComputerConnectionString %&gt;" 
  SelectCommand="SELECT [CompanyID], [CompanyName] FROM [Company]"&gt;
  &lt;/asp:SqlDataSource&gt;
  &lt;/EditItemTemplate&gt;
  &lt;ItemTemplate&gt;
  &lt;asp:Label ID="Label1" runat="server" Text='&lt;%# Bind("CompanyID") %&gt;'&gt;&lt;/asp:Label&gt;
  &lt;/ItemTemplate&gt;
  &lt;/asp:TemplateField&gt;
  &lt;asp:BoundField DataField="FirstName" HeaderText="FirstName" 
  SortExpression="FirstName" /&gt;
  &lt;asp:BoundField DataField="LastName" HeaderText="LastName" 
  SortExpression="LastName" /&gt;
  &lt;asp:BoundField DataField="Address" HeaderText="Address" 
  SortExpression="Address" /&gt;
  &lt;asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" /&gt;
  &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  
  &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" 
   ConnectionString="&lt;%$ ConnectionStrings:sas %&gt;" 
  
   DeleteCommand="DELETE FROM [Customer] WHERE [CustID] = @CustID" 
   InsertCommand="INSERT INTO [Customer] ([CompanyID], [FirstName], [LastName],      
   [Address], [Email]) VALUES (@CompanyID, @FirstName, @LastName, @Address, @Email)" 
  
   SelectCommand="SELECT [CustID], [CompanyID], [FirstName], [LastName], [Address], 
   [Email] FROM [Customer]" 
  
   UpdateCommand="UPDATE [Customer] SET [CompanyID] = @CompanyID, [FirstName] =    
   @FirstName, [LastName] = @LastName, [Address] = @Address, [Email] = @Email WHERE 
   [CustID] = @CustID"&gt;
  
   &lt;DeleteParameters&gt;
   &lt;asp:Parameter Name="CustID" Type="Int32" /&gt;
   &lt;/DeleteParameters&gt;
  
   &lt;InsertParameters&gt;
   &lt;asp:Parameter Name="CompanyID" Type="Int32" /&gt;
   &lt;asp:Parameter Name="FirstName" Type="String" /&gt;
   &lt;asp:Parameter Name="LastName" Type="String" /&gt;
   &lt;asp:Parameter Name="Address" Type="String" /&gt;
   &lt;asp:Parameter Name="Email" Type="String" /&gt;
   &lt;/InsertParameters&gt;
  
   &lt;UpdateParameters&gt;
   &lt;asp:Parameter Name="CompanyID" Type="Int32" /&gt;
   &lt;asp:Parameter Name="FirstName" Type="String" /&gt;
   &lt;asp:Parameter Name="LastName" Type="String" /&gt;
   &lt;asp:Parameter Name="Address" Type="String" /&gt;
   &lt;asp:Parameter Name="Email" Type="String" /&gt;
   &lt;asp:Parameter Name="CustID" Type="Int32" /&gt;
   &lt;/UpdateParameters&gt;
  
   &lt;/asp:SqlDataSource&gt;
  </code></pre>