1

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.

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.

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.

How can I fix this problem?

This is the auto generated code VS2010:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="CustID" 
DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
ShowSelectButton="True" />
<asp:BoundField DataField="CustID" HeaderText="CustID" InsertVisible="False" 
ReadOnly="True" SortExpression="CustID" />
<asp:TemplateField HeaderText="CompanyID" SortExpression="CompanyID">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
DataSourceID="SqlDataSource1" DataTextField="CompanyName" 
DataValueField="CompanyID">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ICT_IdealComputerConnectionString %>" 
SelectCommand="SELECT [CompanyID], [CompanyName] FROM [Company]">
</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("CompanyID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" 
SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" 
SortExpression="LastName" />
<asp:BoundField DataField="Address" HeaderText="Address" 
SortExpression="Address" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
</Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
 ConnectionString="<%$ ConnectionStrings:sas %>" 

 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">

 <DeleteParameters>
 <asp:Parameter Name="CustID" Type="Int32" />
 </DeleteParameters>

 <InsertParameters>
 <asp:Parameter Name="CompanyID" Type="Int32" />
 <asp:Parameter Name="FirstName" Type="String" />
 <asp:Parameter Name="LastName" Type="String" />
 <asp:Parameter Name="Address" Type="String" />
 <asp:Parameter Name="Email" Type="String" />
 </InsertParameters>

 <UpdateParameters>
 <asp:Parameter Name="CompanyID" Type="Int32" />
 <asp:Parameter Name="FirstName" Type="String" />
 <asp:Parameter Name="LastName" Type="String" />
 <asp:Parameter Name="Address" Type="String" />
 <asp:Parameter Name="Email" Type="String" />
 <asp:Parameter Name="CustID" Type="Int32" />
 </UpdateParameters>

 </asp:SqlDataSource>
2
  • Can you post the markup for the grid from .aspx and any code behind you have? Commented Jan 19, 2012 at 17:31
  • I posted the auto generated code, Please see. Commented Jan 19, 2012 at 17:45

2 Answers 2

1

Change:

<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
DataSourceID="SqlDataSource1" DataTextField="CompanyName" 
DataValueField="CompanyID">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ICT_IdealComputerConnectionString %>" 
SelectCommand="SELECT [CompanyID], [CompanyName] FROM [Company]">
</asp:SqlDataSource>
</EditItemTemplate>

To:

<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" 
DataSourceID="SqlDataSource1" DataTextField="CompanyName" 
DataValueField="CompanyID" SelectedValue='<%# Bind("CompanyID") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ICT_IdealComputerConnectionString %>" 
SelectCommand="SELECT [CompanyID], [CompanyName] FROM [Company]">
</asp:SqlDataSource>
</EditItemTemplate>

Here is a link to a walkthrough of using a dropdown list to edit

Sign up to request clarification or add additional context in comments.

Comments

1

In the UpdateParameters try replacing

<asp:Parameter Name="CompanyID" Type="Int32" />

with

<asp:ControlParameter Name="CompanyID" 
     ControlID="DropDownList1" PropertyName="SelectedValue" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.