Reputation: 2503
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>
Upvotes: 1
Views: 1423
Reputation: 794
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
Upvotes: 1
Reputation: 8897
In the UpdateParameters
try replacing
<asp:Parameter Name="CompanyID" Type="Int32" />
with
<asp:ControlParameter Name="CompanyID"
ControlID="DropDownList1" PropertyName="SelectedValue" />
Upvotes: 1