无法通过GridView更新数据

我有两张名为公司和客户的表格。 Company是基表,它有两列:CompanyID和Companyname。 客户表具有comapnyID,因为它是外键。

我使用网格视图来显示所有customerID,CustName,CustAddress和companyID。 要更新companyID字段,请在网格视图中使用下拉列表,该列表显示用户可以从中选择一个可用公司ID的所有可用公司ID。

但问题是我尝试通过从下拉列表中选择一个公司ID并单击更新来更新具有新公司ID的特定客户。 一切正常,但是,在更新的客户记录中,对于companyID字段,它显示NULL值而不是新分配的公司ID值。

我该如何解决这个问题?

这是自动生成的代码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>

更改:

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

至:

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

以下是使用下拉列表进行编辑的演练的链接


UpdateParameters尝试替换

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

<asp:ControlParameter Name="CompanyID" 
     ControlID="DropDownList1" PropertyName="SelectedValue" />
链接地址: http://www.djcxy.com/p/86285.html

上一篇: Cannot update data through GridView

下一篇: How to add 'ON DELETE CASCADE' in ALTER TABLE statement