2017-06-20 176 views
0

我一直在試圖將最後一個插入的ID插入到我的代碼中,但是如果參數類型分別設置爲int 32或int64,則返回11或12作爲返回的ID。獲取最後插入的行的ID

這是我使用

CREATE procedure [dbo].[Sophia_FND_SalesOrder_CREATE](
@contact int, 
@deliveryAddress varchar(50), 
@Charge float, 
@GrossTotal float, 
@PaymentMode varchar(50), 
@organisationId int, 
@userid varchar(50), 
@amtpaid float, 
@SalesOrderId int OUTPUT 
) 
as 
begin 
insert into Sophia_FND_SalesOrder(ContactId, deliveryAddress, Charge, GrossTotal, PaymentMode, organisationId,userid, AmountPaid) 
values(@contact,@deliveryAddress, @Charge, @GrossTotal, @PaymentMode, @organisationId, @userid, @amtpaid) 
SET @SalesOrderId = SCOPE_IDENTITY() 
RETURN @SalesOrderId 
end 

這是我用在我的課的功能的過程

Public Shared Function Create_salesOrder(ByVal contact As Integer, ByVal deliveryAddress As String, ByVal Charge As Double, ByVal GrossTotal As Double, ByVal PaymentMode As String, ByVal organisationId As Integer, ByVal userid As String, ByVal amtpaid As Double) As Integer 

     Dim comm As DbCommand = CreateCommand17() 
     comm.CommandText = "Sophia_FND_SalesOrder_CREATE" 

     AddParameter(comm, "@contact", contact, DbType.Int64) 
     AddParameter(comm, "@deliveryAddress", deliveryAddress, DbType.String) 
     AddParameter(comm, "@Charge", Charge, DbType.Double) 
     AddParameter(comm, "@GrossTotal", GrossTotal, DbType.Double) 
     AddParameter(comm, "@PaymentMode", PaymentMode, DbType.String) 
     AddParameter(comm, "@organisationId", organisationId, DbType.Int64) 
     AddParameter(comm, "@userid", userid, DbType.String) 
     AddParameter(comm, "@amtpaid", amtpaid, DbType.Double) 
     AddParameter(comm, "@SalesOrderId", DbType.Int64, ParameterDirection.Output) 




     Dim id As Integer 
     Dim insertResult As Int32 = -1 
     Try 

      insertResult = ExecuteNonQuery6(comm) 
      id = Convert.ToInt64(comm.Parameters("@SalesOrderId").Value) 
     Catch ex As Exception 

      Throw ex 
     End Try 
     'Return insertResult <> -1 
     Return id 
    End Function 

這是我後面把它在我的代碼

Dim sale_id As Integer 
      If IsNothing(Session("soid")) Then 
       sale_id = Create_salesOrder(CInt(drpcontact.SelectedValue), txtdelivery.Text, lblCharge_unformated.Text, total, drpPayMode.SelectedItem.Text, SessionWrapper.OrganisationId, SessionWrapper.LoggedInUserID, CDbl(txtpayment.Text)) 
       If Not IsNothing(sale_id) Then 
        Session("soid") = sale_id 

        Me.MessageBoard1.MessagePanel.CssClass = "errorMessage" 
        Me.MessageBoard1.MessagePanel.Visible = True 
        Me.MessageBoard1.MessageLabel.ForeColor = Drawing.Color.Green 
        Me.MessageBoard1.MessageLabel.Text = "done" 
        FillGridview() 
       Else 
        Me.MessageBoard1.MessagePanel.CssClass = "errorMessage" 
        Me.MessageBoard1.MessagePanel.Visible = True 
        Me.MessageBoard1.MessageLabel.ForeColor = Drawing.Color.Red 
        Me.MessageBoard1.MessageLabel.Text = "Error Occured on creating sales" 
       End If 
end if 

我發現了一種工作方式,但語法完全不同,我想堅持我的語法。這個問題應該在我的班級職能,但我似乎不知道我犯了什麼錯誤。

每當我運行此,我要麼得到11或12返回ID這是完全錯誤的

感謝

+0

我沒有看到任何關於此代碼的問題。你能詳細解釋一下這段代碼的問題嗎? – Praveen

+0

我在SQL Studio測試,一切運作良好,但是當我測試的功能, 我要麼得到11或12
返回的ID始終是11,如果參數類型('AddParameter(COMM,「@SalesOrderId」的DbType .Int32,ParameterDirection.Output)' )被設置爲int32,它是12如果參數類型('AddParameter(COMM 「@SalesOrderId」,DbType.Int64,ParameterDirection.Output)' )的INT64 @praveen – Nipek

+0

你好Nipek,使用ExecuteScaler而不是ExecuteNonQuery並試試,希望它能起作用。 –

回答

0

必須改變把這個在我後面的代碼,這似乎爲我工作。謝謝

Dim sale_id As Integer 
      ' Dim saleslastid As Integer 
      Dim salecreate_line As Boolean 
      If IsNothing(Session("soid")) Then 
       ' slastid = SalesOrder_id() 
       ' saleslastid = StringHelper_2_0.ClearDBNullError(slastid.Rows(0)("id")) 
       'sale_id = Create_salesOrder(CInt(drpcontact.SelectedValue), txtdelivery.Text, lblCharge_unformated.Text, total, drpPayMode.SelectedItem.Text, SessionWrapper.OrganisationId, SessionWrapper.LoggedInUserID, CDbl(txtpayment.Text)) 
       Dim strConnString As String = ConfigurationManager.ConnectionStrings("SophiaERP_Foundation_4").ConnectionString 
       Dim con As New SqlConnection(strConnString) 
       Dim cmd As New SqlCommand() 
       cmd.CommandType = CommandType.StoredProcedure 
       cmd.CommandText = "Sophia_FND_SalesOrder_CREATE" 
       cmd.Parameters.Add("@contact", SqlDbType.Int).Value = CInt(drpcontact.SelectedValue) 
       cmd.Parameters.Add("@deliveryAddress", SqlDbType.VarChar).Value = txtdelivery.Text 
       cmd.Parameters.Add("@Charge", SqlDbType.Float).Value = lblCharge_unformated.Text 
       cmd.Parameters.Add("@GrossTotal", SqlDbType.Float).Value = total 
       cmd.Parameters.Add("@PaymentMode", SqlDbType.VarChar).Value = drpPayMode.SelectedItem.Text 
       cmd.Parameters.Add("@organisationId", SqlDbType.Int).Value = SessionWrapper.OrganisationId 
       cmd.Parameters.Add("@userid", SqlDbType.VarChar).Value = SessionWrapper.LoggedInUserID 
       cmd.Parameters.Add("@amtpaid", SqlDbType.Float).Value = 0 
       cmd.Parameters.Add("@duration", SqlDbType.Int).Value = CInt(lblduration.Text) 
       cmd.Parameters.Add("@SalesOrderId", SqlDbType.Int).Direction = ParameterDirection.Output 
       cmd.Connection = con 
       Try 
        con.Open() 
        cmd.ExecuteNonQuery() 

        sale_id = cmd.Parameters("@SalesOrderId").Value 


       Catch ex As Exception 
        Throw ex 
       Finally 
        con.Close() 
        con.Dispose() 
       End Try 
end if