尝试获取插入的 ID 时强制转换无效

发布于 2024-10-31 19:38:21 字数 2880 浏览 2 评论 0原文

我试图获取最近插入的表 ID,但我不断收到转换错误。该项目总是包含 NULL,我不知道为什么。我按照此处的说明进行操作: http://www.mikesdotnetting .com/Article/54/Getting-the-identity-of-the-most-recently-added-record

这是我的 SqlDataSource:

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
    ConnectionString="<%$ ConnectionStrings:NH_SWAGConnectionString %>"             
    InsertCommand="INSERT INTO [tblOrders] ([OrderDate], [OrderTotal], [OrderAccount], [OrderCostCentre]) VALUES (@OrderDate, @OrderTotal, @OrderAccount, @OrderCostCentre); SET @OrderNewID = SCOPE_IDENTITY()" 
    SelectCommand="SELECT * FROM [tblOrders]" 
    UpdateCommand="UPDATE [tblOrders] SET [OrderDate] = @OrderDate, [OrderTotal] = @OrderTotal, [OrderAccount] = @OrderAccount, [OrderCostCentre] = @OrderCostCentre WHERE [OrderID] = @original_OrderID AND [OrderDate] = @original_OrderDate AND [OrderTotal] = @original_OrderTotal AND [OrderAccount] = @original_OrderAccount AND [OrderCostCentre] = @original_OrderCostCentre"            
    ConflictDetection="CompareAllValues" 
    OldValuesParameterFormatString="original_{0}"
    OnInserting="SqlDataSource2_Inserting"
    OnInserted="SqlDataSource2_Inserted">
    <InsertParameters>
        <asp:Parameter Direction="Output" Name="OrderNewId" Type="Int32" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="OrderDate" Type="DateTime" />
        <asp:Parameter Name="OrderTotal" Type="Decimal" />
        <asp:Parameter Name="OrderAccount" Type="String" />
        <asp:Parameter Name="OrderCostCentre" Type="String" />
        <asp:Parameter Name="original_OrderID" Type="Int32" />
        <asp:Parameter Name="original_OrderDate" Type="DateTime" />
        <asp:Parameter Name="original_OrderTotal" Type="Decimal" />
        <asp:Parameter Name="original_OrderAccount" Type="String" />
        <asp:Parameter Name="original_OrderCostCentre" Type="String" />
    </UpdateParameters>
</asp:SqlDataSource>

这是插入新记录并调用 SCOPE_IDENTITY( ) 获取新 ID:

protected void btnOrder_Click(object sender, EventArgs e)
{
    //Add entry to Order Table    

    SqlDataSource2.InsertParameters.Add("OrderDate", DateTime.Now.ToString("MMMM dd, yyyy"));
    SqlDataSource2.InsertParameters.Add("OrderTotal", "0");
    SqlDataSource2.InsertParameters.Add("OrderAccount", ItmUser);
    SqlDataSource2.InsertParameters.Add("OrderCostCentre", ItmCostCode.Text);

    SqlDataSource2.Insert();
}

protected void SqlDataSource2_Inserted(object sender, SqlDataSourceStatusEventArgs e)
{
    OrderNewID = (int)e.Command.Parameters["@OrderNewId"].Value;
}

为什么上面的 @OrderNewId 为 NULL?

I am attempting to grab the recently inserted ID for a table, but I keep receiving a casting error. The item always contains NULL, and I am not sure why. I am following the instructions located here:
http://www.mikesdotnetting.com/Article/54/Getting-the-identity-of-the-most-recently-added-record

Here is my SqlDataSource:

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
    ConnectionString="<%$ ConnectionStrings:NH_SWAGConnectionString %>"             
    InsertCommand="INSERT INTO [tblOrders] ([OrderDate], [OrderTotal], [OrderAccount], [OrderCostCentre]) VALUES (@OrderDate, @OrderTotal, @OrderAccount, @OrderCostCentre); SET @OrderNewID = SCOPE_IDENTITY()" 
    SelectCommand="SELECT * FROM [tblOrders]" 
    UpdateCommand="UPDATE [tblOrders] SET [OrderDate] = @OrderDate, [OrderTotal] = @OrderTotal, [OrderAccount] = @OrderAccount, [OrderCostCentre] = @OrderCostCentre WHERE [OrderID] = @original_OrderID AND [OrderDate] = @original_OrderDate AND [OrderTotal] = @original_OrderTotal AND [OrderAccount] = @original_OrderAccount AND [OrderCostCentre] = @original_OrderCostCentre"            
    ConflictDetection="CompareAllValues" 
    OldValuesParameterFormatString="original_{0}"
    OnInserting="SqlDataSource2_Inserting"
    OnInserted="SqlDataSource2_Inserted">
    <InsertParameters>
        <asp:Parameter Direction="Output" Name="OrderNewId" Type="Int32" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="OrderDate" Type="DateTime" />
        <asp:Parameter Name="OrderTotal" Type="Decimal" />
        <asp:Parameter Name="OrderAccount" Type="String" />
        <asp:Parameter Name="OrderCostCentre" Type="String" />
        <asp:Parameter Name="original_OrderID" Type="Int32" />
        <asp:Parameter Name="original_OrderDate" Type="DateTime" />
        <asp:Parameter Name="original_OrderTotal" Type="Decimal" />
        <asp:Parameter Name="original_OrderAccount" Type="String" />
        <asp:Parameter Name="original_OrderCostCentre" Type="String" />
    </UpdateParameters>
</asp:SqlDataSource>

Here is the code behind inserting the new record, and calling SCOPE_IDENTITY() to grab the new ID:

protected void btnOrder_Click(object sender, EventArgs e)
{
    //Add entry to Order Table    

    SqlDataSource2.InsertParameters.Add("OrderDate", DateTime.Now.ToString("MMMM dd, yyyy"));
    SqlDataSource2.InsertParameters.Add("OrderTotal", "0");
    SqlDataSource2.InsertParameters.Add("OrderAccount", ItmUser);
    SqlDataSource2.InsertParameters.Add("OrderCostCentre", ItmCostCode.Text);

    SqlDataSource2.Insert();
}

protected void SqlDataSource2_Inserted(object sender, SqlDataSourceStatusEventArgs e)
{
    OrderNewID = (int)e.Command.Parameters["@OrderNewId"].Value;
}

Why @OrderNewId above is NULL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮生未歇 2024-11-07 19:38:21

好吧,我明白了。我没有使用 SET,而是在 InsertQuery 中使用了 SELECT:

INSERT INTO [tblOrders] ([OrderDate], [OrderTotal], [OrderAccount], [OrderCostCentre]) VALUES (@OrderDate, @OrderTotal, @OrderAccount, @OrderCostCentre); SELECT @OrderNewID = SCOPE_IDENTITY()

Ok I got it figured. Instead of using SET I used SELECT in the InsertQuery:

INSERT INTO [tblOrders] ([OrderDate], [OrderTotal], [OrderAccount], [OrderCostCentre]) VALUES (@OrderDate, @OrderTotal, @OrderAccount, @OrderCostCentre); SELECT @OrderNewID = SCOPE_IDENTITY()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文