Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27189

VS 2010 SQL Server Retrieving Unique Identifier with Parametized Query

$
0
0
Hi,

I recently moved my access db into sql server 2008 r2 and I am now unable retrieve the unique identifier field on insert statements using parametrized queries. The new datatable rows are inserted into database and that works fine.

However, previously I had used the OnRowUpdated event handler to check to see if the statement type was an insert statement and if it was it would retrieve the uniqueidentifer and add it to the datatable and update the dgv that the data table is bound to.

Here is the parametised query code -

Code:

  Dim sqlInsertNewRisk As String = "INSERT INTO [Risk Analysis] ([Quotation Name],[Bid Reference],[Quotation Reference]," & _
            "[Site Specific Reference],[Category],[Risk/Opportunity],[Checked],[Description],[PreMit Classification]," & _
            "[PreMit Chance of Event Occuring],[PreMit Total Cost],[PreMit Cost],[PreMit Total Cost Range 1],[PreMit Total Cost Range 2]," & _
            "[PreMit Values],[PreMit Values MIN],[PreMit Calculated Value],[PreMit Values Max],[Mitigation Strategy],[PostMit Classification]," & _
            "[PostMit Chance of Event Occuring],[PostMit Total Cost],[PostMit Cost],[PostMit Total Cost Range 1],[PostMit Total Cost Range 2]," & _
          "[PostMit Values],[PostMit Values MIN],[PostMit Calculated Value],[PostMit Values MAX]) " & _
          "Values (@QuotationName,@BidReference,@QuotationReference,@SiteSpecificReference,@Category,@RiskOpportunity,@Checked,@Description," & _
          "@PreMitClassification,@PreMitChanceOfEventOccuring,@PreMitTotalCost,@PreMitCost,@PreMitTotalCostRange1,@PreMitTotalCostRange2,@PreMitValues," & _
          "@PreMitValuesMIN,@PreMitCalculatedValue,@PreMitValuesMAX,@MitigationStrategy,@PostMitClassification,@PostMitChanceOfEventOccuring," & _
          "@PostMitTotalCost,@PostMitCost,@PostMitTotalCostRange1,@PostMitTotalCostRange2,@PostMitValues,@PostMitValuesMIN,@PostMitCalculatedValue," & _
        "@PostMitValuesMAX)"

        Dim sqlUpdateRisk As String = "UPDATE [Risk Analysis] SET [Quotation Name]=@QuotationName,[Bid Reference]=@BidReference,[Quotation Reference]=@QuotationReference," & _
            "[Site Specific Reference]=@SiteSpecificReference,[Category]=@Category,[Risk/Opportunity]=@RiskOpportunity,[Checked]=@Checked," & _
            "[Description]=@Description,[PreMit Classification]=@PreMitClassification," & _
            "[PreMit Chance of Event Occuring]=@PreMitChanceOfEventOccuring,[PreMit Total Cost]=@PreMitTotalCost,[PreMit Cost]=@PreMitCost," & _
            "[PreMit Total Cost Range 1]=@PreMitTotalCostRange1,[PreMit Total Cost Range 2]=@PreMitTotalCostRange2," & _
            "[PreMit Values]=@PreMitValues,[PreMit Values MIN]=@PreMitValuesMIN,[PreMit Calculated Value]=@PreMitCalculatedValue," & _
            "[PreMit Values Max]=@PreMitValuesMAX,[Mitigation Strategy]=@MitigationStrategy,[PostMit Classification]=@PostMitClassification," & _
            "[PostMit Chance of Event Occuring]=@PostMitChanceOfEventOccuring,[PostMit Total Cost]=@PostMitTotalCost,[PostMit Cost]=@PostMitCost," & _
            "[PostMit Total Cost Range 1]=@PostMitTotalCostRange1,[PostMit Total Cost Range 2]=@PostMitTotalCostRange2," & _
          "[PostMit Values]=@PostMitValues,[PostMit Values MIN]=@PostMitValuesMIN,[PostMit Calculated Value]=@PostMitCalculatedValue," & _
          "[PostMit Values MAX]=@PostMitValuesMAX WHERE [Risk Reference Code]=@RiskReferenceCode"

        If SISCon.State = ConnectionState.Closed Then
            SISCon.Open()
        End If

        Dim NewRiskInsertNewParameter As New SqlCommand(sqlInsertNewRisk, SISCon)

        Dim UpdateRiskCmdParameter As New SqlCommand(sqlUpdateRisk, SISCon)

        With NewRiskInsertNewParameter.Parameters

            .Add("@QuotationName", SqlDbType.NVarChar, 50, "Quotation Name")
            .Add("@BidReference", SqlDbType.NVarChar, 50, "Bid Reference")
            .Add("@QuotationReference", SqlDbType.NVarChar, 50, "Quotation Reference")
            .Add("@SiteSpecificReference", SqlDbType.NVarChar, 50, "Site Specific Reference")
            .Add("@Category", SqlDbType.NVarChar, 50, "Category")
            .Add("@RiskOpportunity", SqlDbType.NVarChar, 200, "Risk/Opportunity")
            .Add("@Checked", SqlDbType.Bit, 4, "Checked")
            .Add("@Description", SqlDbType.NVarChar, 1000, "Description")
            .Add("@PreMitClassification", SqlDbType.NVarChar, 50, "PreMit Classification")
            .Add("@PreMitChanceOfEventOccuring", SqlDbType.Float, 5, "PreMit Chance Of Event Occuring")
            .Add("@PreMitTotalCost", SqlDbType.Float, 10, "PreMit Total Cost")
            .Add("@PreMitCost", SqlDbType.Bit, 4, "PreMit Cost")
            .Add("@PreMitTotalCostRange1", SqlDbType.Float, 5, "PreMit Total Cost Range 1")
            .Add("@PreMitTotalCostRange2", SqlDbType.Float, 5, "PreMit Total Cost Range 2")
            .Add("@PreMitValues", SqlDbType.Bit, 4, "PreMit Values")
            .Add("@PreMitValuesMIN", SqlDbType.Float, 10, "PreMit Values MIN")
            .Add("@PreMitCalculatedValue", SqlDbType.Float, 10, "PreMit Calculated Value")
            .Add("@PreMitValuesMAX", SqlDbType.Float, 10, "PreMit Values MAX")
            .Add("@MitigationStrategy", SqlDbType.NVarChar, 1000, "Mitigation Strategy")
            .Add("@PostMitClassification", SqlDbType.NVarChar, 50, "PostMit Classification")
            .Add("@PostMitChanceOfEventOccuring", SqlDbType.Float, 5, "PostMit Chance Of Event Occuring")
            .Add("@PostMitTotalCost", SqlDbType.Float, 10, "PostMit Total Cost")
            .Add("@PostMitCost", SqlDbType.Bit, 4, "PostMit Cost")
            .Add("@PostMitTotalCostRange1", SqlDbType.Float, 5, "PostMit Total Cost Range 1")
            .Add("@PostMitTotalCostRange2", SqlDbType.Float, 5, "PostMit Total Cost Range 2")
            .Add("@PostMitValues", SqlDbType.Bit, 4, "PostMit Values")
            .Add("@PostMitValuesMIN", SqlDbType.Float, 10, "PostMit Values MIN")
            .Add("@PostMitCalculatedValue", SqlDbType.Float, 10, "PostMit Calculated Value")
            .Add("@PostMitValuesMAX", SqlDbType.Float, 10, "PostMit Values MAX")
            ' .Add("@RiskReferenceCode", SqlDbType.UniqueIdentifier, 20, "Risk Reference Code")
        End With

        With UpdateRiskCmdParameter.Parameters
            .Add("@QuotationName", SqlDbType.NVarChar, 50, "Quotation Name")
            .Add("@BidReference", SqlDbType.NVarChar, 50, "Bid Reference")
            .Add("@QuotationReference", SqlDbType.NVarChar, 50, "Quotation Reference")
            .Add("@SiteSpecificReference", SqlDbType.NVarChar, 50, "Site Specific Reference")
            .Add("@Category", SqlDbType.NVarChar, 50, "Category")
            .Add("@RiskOpportunity", SqlDbType.NVarChar, 200, "Risk/Opportunity")
            .Add("@Checked", SqlDbType.Bit, 4, "Checked")
            .Add("@Description", SqlDbType.NVarChar, 1000, "Description")
            .Add("@PreMitClassification", SqlDbType.NVarChar, 50, "PreMit Classification")
            .Add("@PreMitChanceOfEventOccuring", SqlDbType.Float, 5, "PreMit Chance Of Event Occuring")
            .Add("@PreMitTotalCost", SqlDbType.Float, 10, "PreMit Total Cost")
            .Add("@PreMitCost", SqlDbType.Bit, 4, "PreMit Cost")
            .Add("@PreMitTotalCostRange1", SqlDbType.Float, 5, "PreMit Total Cost Range 1")
            .Add("@PreMitTotalCostRange2", SqlDbType.Float, 5, "PreMit Total Cost Range 2")
            .Add("@PreMitValues", SqlDbType.Bit, 4, "PreMit Values")
            .Add("@PreMitValuesMIN", SqlDbType.Float, 10, "PreMit Values MIN")
            .Add("@PreMitCalculatedValue", SqlDbType.Float, 10, "PreMit Calculated Value")
            .Add("@PreMitValuesMAX", SqlDbType.Float, 10, "PreMit Values MAX")
            .Add("@MitigationStrategy", SqlDbType.NVarChar, 1000, "Mitigation Strategy")
            .Add("@PostMitClassification", SqlDbType.NVarChar, 50, "PostMit Classification")
            .Add("@PostMitChanceOfEventOccuring", SqlDbType.Float, 5, "PostMit Chance Of Event Occuring")
            .Add("@PostMitTotalCost", SqlDbType.Float, 10, "PostMit Total Cost")
            .Add("@PostMitCost", SqlDbType.Bit, 4, "PostMit Cost")
            .Add("@PostMitTotalCostRange1", SqlDbType.Float, 5, "PostMit Total Cost Range 1")
            .Add("@PostMitTotalCostRange2", SqlDbType.Float, 5, "PostMit Total Cost Range 2")
            .Add("@PostMitValues", SqlDbType.Bit, 4, "PostMit Values")
            .Add("@PostMitValuesMIN", SqlDbType.Float, 10, "PostMit Values MIN")
            .Add("@PostMitCalculatedValue", SqlDbType.Float, 10, "PostMit Calculated Value")
            .Add("@PostMitValuesMAX", SqlDbType.Float, 10, "PostMit Values MAX")
            .Add("@RiskReferenceCode", SqlDbType.UniqueIdentifier, 20, "Risk Reference Code")
        End With

        Try

            RiskAssessmentSummarySheetDA.InsertCommand = NewRiskInsertNewParameter
            RiskAssessmentSummarySheetDA.UpdateCommand = UpdateRiskCmdParameter
            RiskAssessmentSummarySheetDA.MissingSchemaAction = MissingSchemaAction.AddWithKey
            RiskAssessmentSummarySheetDA.Update(dtRiskAssessmentSummarySheet)
            Catch ex As Exception
            MsgBox(ex.Message.ToString)

        End Try

This works fine but the RowUpdated handler below is not updating the datatable and reflecting the uniqueidentifier in column "Risk Reference Code".

This is activated after RiskAssessmentSummarySheetDA.Update(dtRiskAssessmentSummarySheet) is run.

Code:

Public Sub RiskAssessmentSummarySheetDA_OnRowUpdated( _
ByVal sender As Object, ByVal e As SqlRowUpdatedEventArgs) Handles RiskAssessmentSummarySheetDA.RowUpdated
     
        If e.StatementType = StatementType.Insert Then
     
            Dim cmdNewID As New SqlCommand("SELECT SCOPE_IDENTITY()", _
              SISCon)

            e.Row("Risk Reference Code") = cmdNewID.ExecuteScalar()

            e.Row.AcceptChanges()
            e.Status = UpdateStatus.SkipCurrentRow
        End If
    End Sub


Viewing all articles
Browse latest Browse all 27189

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>