一次插入2条记录asp.net

时间:2018-04-14 04:04:11

标签: c# asp.net

我正在尝试一次将记录添加到两个表中,第一个记录的ID是第二个记录中的外键,第二个表中的一个列也在quirt字符串中。事情是我无法添加记录,我不知道为什么

表一是:Customer

customer ID
customer name
customer email
customer addres
customer phone

表二是:Order

Order id
order date
order details< contain Product id
customer id

这是aspx标记:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT [ProductID], [Prod_name], [Prod_price], [prod_description], [prod_photo] FROM [Products] WHERE ([ProductID] = @ProductID)">
    <SelectParameters>
        <asp:QueryStringParameter Name="ProductID" QueryStringField="ProdPass" 
            Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>
<asp:FormView HorizontalAlign="Center" ID="FormView1" runat="server" DataKeyNames="ProductID" 
DataSourceID="SqlDataSource1" 
onpageindexchanging="FormView1_PageIndexChanging">
    <ItemTemplate>
        <div><table align="center" style="width:100%;">
            <tr>
                <td>
                    <asp:Image ID="Image1" runat="server" Height="348px" 
                        ImageUrl='<%# Bind("prod_photo","~\ppp\{0}") %>' style="margin-left: 0px" 
                        Width="290px" />
                </td>
                <td>
                    <asp:Label ID="Label3" runat="server" 
                        style="font-weight: 700; font-family: 'Century Gothic'; font-size: large" 
                        Text='<%# Eval("Prod_name") %>'></asp:Label>
                    <br />
                    <asp:Label ID="Label1" runat="server" CssClass="style30" 
                        Text='<%# Eval("Prod_name") %>'></asp:Label>
                    <br class="style30" />
                    <asp:Label ID="Label2" runat="server" CssClass="style30" 
                        Text='<%# Eval("Prod_price") %>'></asp:Label>
                    <br />
                    <asp:DropDownList ID="ddlQuantity" runat="server" Width="50px">
                        <asp:ListItem Value="1" Text="1" Selected="True"></asp:ListItem>
                        <asp:ListItem Value="2" Text="2" ></asp:ListItem>
                        <asp:ListItem Value="3" Text="3" ></asp:ListItem>
                        <asp:ListItem Value="4" Text="4" ></asp:ListItem>
                        <asp:ListItem Value="5" Text="5" ></asp:ListItem>
                        <asp:ListItem Value="6" Text="6" ></asp:ListItem>
                        <asp:ListItem Value="7" Text="7"></asp:ListItem>
                        <asp:ListItem Value="8" Text="8"></asp:ListItem>
                        <asp:ListItem Value="9" Text="9"></asp:ListItem>
                        <asp:ListItem Value="10" Text="10"></asp:ListItem>
                    </asp:DropDownList>
                    <br />
                    <asp:LinkButton ID="LinkButton1" runat="server" Font-Names="Century Gothic" 
                        Font-Underline="False" ForeColor="Gray" onclick="LinkButton1_Click">Order</asp:LinkButton>
                </td>
            </tr>
        </table></div>
    </ItemTemplate>
</asp:FormView>
<br />
<asp:Label ID="lblMessage" runat="server"></asp:Label>
<br />
<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
    ConnectionString="<%$ ConnectionStrings:SHOPUREConnectionString %>" 

    SelectCommand="SELECT Order_product.Quantity, Order_product.ProductID, [Order].OrderID, [Order].OrderDate, [Order].Orderdetails, [Order].CustomerID AS Expr1, Customer.* FROM Order_product INNER JOIN [Order] ON Order_product.OrderID = [Order].OrderID INNER JOIN Customer ON [Order].CustomerID = Customer.CustomerID WHERE ProductID=@ProductID " InsertCommand="INSERT INTO [Customer] ([CusomerName],[C_address],[C_phone],[C_email]) VALUES (@CusomerName, @C_address, @C_phone, @C_email);
 INSERT INTO [Order] ([OrderDetails],[CustomerID]) VALUES (@OrderDetails,@@Identity);
" oninserted="SqlDataSource2_Inserted">
    <InsertParameters>
        <asp:Parameter Name="CusomerName" />
        <asp:Parameter Name="C_address" />
        <asp:Parameter Name="C_phone" />
        <asp:Parameter Name="C_email" />
        <asp:Parameter Name="OrderDetails" />
    </InsertParameters>
    <SelectParameters>
        <asp:QueryStringParameter Name="ProductID" QueryStringField="ProdPass" />
    </SelectParameters>
</asp:SqlDataSource>
<asp:FormView DefaultMode="Insert" ID="FormView2" runat="server" DataSourceID="SqlDataSource2">
    <InsertItemTemplate>
        Quantity:
        <br />
        <asp:DropDownList ID="ddlQuantity" runat="server" 
            SelectedValue='<%# Bind("Quantity") %>' Width="50px">
            <asp:ListItem Text="1" Value="1"></asp:ListItem>
            <asp:ListItem Text="2" Value="2"></asp:ListItem>
            <asp:ListItem Text="3" Value="3"></asp:ListItem>
            <asp:ListItem Text="4" Value="4"></asp:ListItem>
            <asp:ListItem Text="5" Value="5"></asp:ListItem>
            <asp:ListItem Text="6" Value="6"></asp:ListItem>
            <asp:ListItem Text="7" Value="7"></asp:ListItem>
            <asp:ListItem Text="8" Value="8"></asp:ListItem>
            <asp:ListItem Text="9" Value="9"></asp:ListItem>
            <asp:ListItem Text="10" Value="10"></asp:ListItem>
        </asp:DropDownList>
        <br />
        OrderDate:<br />
        <asp:Label ID="Label4" runat="server" Text='<%# Eval("OrderDate", "{0:g}") %>'></asp:Label>
        &nbsp;<br />Orderdetails:
        <asp:TextBox ID="OrderdetailsTextBox" runat="server" 
            Text='<%# Bind("Orderdetails") %>' />
        <br />
        CustomerName:
        <asp:TextBox ID="CustomerNameTextBox" runat="server" 
            Text='<%# Bind("CustomerName") %>' />
        <br />
        C_address:
        <asp:TextBox ID="C_addressTextBox" runat="server" 
            Text='<%# Bind("C_address") %>' />
        <br />
        C_phone:
        <asp:TextBox ID="C_phoneTextBox" runat="server" Text='<%# Bind("C_phone") %>' />
        <br />
        C_email:
        <asp:TextBox ID="C_emailTextBox" runat="server" Text='<%# Bind("C_email") %>' />
        <br />
        <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
                CommandName="Insert" Text="Insert" />

0 个答案:

没有答案