我有2个名为公司和客户的表。公司是基表,它有两列:CompanyID和Companyname。并且customer表具有comapnyID作为其外键。
我使用gird视图显示所有customerID,CustName,CustAddress和companyID。要更新companyID字段,请在网格视图中使用下拉列表,该列表显示所有可用的companyID,用户可从中选择一个可用的companyID。
但问题是我尝试通过从下拉列表中选择一个companyID并单击更新来使用新的companyID更新特定客户。但是,在更新的客户记录中,一切正常,对于companyID字段,它显示NULL值而不是新分配的companyID值。
如何解决此问题?
这是自动生成的代码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>
答案 0 :(得分:1)
在UpdateParameters
尝试替换
<asp:Parameter Name="CompanyID" Type="Int32" />
与
<asp:ControlParameter Name="CompanyID"
ControlID="DropDownList1" PropertyName="SelectedValue" />
答案 1 :(得分:1)
变化:
<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>
的链接