Quantcast
Channel: Visual Studio and Visual Web Developer Express
Viewing all articles
Browse latest Browse all 3509

How to retrieve the ID (Primary Key)value of a just inserted row where the Primary Key or ID is GUID

$
0
0

Hello

I am trying to retrieve the ID value of a just inserted row. The primary key value is a GUID. So scope_identity() will not work.

        Dim survey = Guid.NewGuid().ToString()

        Dim SurveyTitle As String = "Diversity Monitoring Survey"
        Dim SurveyDetail As String = ""

        Core.DB.DoQuery("insert into survey(id,title, detail,employerid,userid) values(@id,@title, @detail, @eid, @uid);", Core.DB.SIP("title", SurveyTitle), Core.DB.SIP("detail", SurveyDetail), Core.DB.SIP("eid", LocalHelper.UserEmployerID()), Core.DB.SIP("uid", LocalHelper.UserID()), Core.DB.SIP("id", survey))

Now I want to retrieve the value of the primary key into a variable.

Dim strSurveyId As String 

How can I do That?

In my code DoQuery is 

        Shared Sub DoQuery(ByVal commandText As String, ByVal ParamArray params As SqlParameter())
            Dim conn As SqlConnection = Nothing
            Try
                conn = GetOpenSqlConnection()
                DoQuery(conn, commandText, params)
            Finally
                If conn IsNot Nothing Then conn.Dispose()
            End Try
        End Sub

The custom code for DB communication  to select data or get data in my application are GetString or GetScalar. where GetString or GetScalar are 


        Shared Function GetString(ByVal selectQueryText As String, ByVal ParamArray params As SqlParameter()) As String
            Dim dt As DataTable = Nothing
            Try
                dt = GetData(selectQueryText, CommandType.Text, params)
                If dt.Rows.Count = 0 Then
                    Return ""
                Else
                    If TypeOf dt.Rows(0)(0) Is DBNull Then
                        Return ""
                    Else
                        Return CStr(dt.Rows(0)(0))
                    End If
                End If
            Finally
                If dt IsNot Nothing Then dt.Dispose()
            End Try
        End Function


        Shared Function GetScalar(ByVal selectQueryText As String, ByVal ParamArray params As SqlParameter()) As Object
            Dim dt As DataTable = Nothing
            Try
                dt = GetData(selectQueryText, CommandType.Text, params)
                If dt.Rows.Count = 0 Then
                    Return Nothing
                Else
                    Return dt.Rows(0)(0)
                End If
            Finally
                If dt IsNot Nothing Then dt.Dispose()
            End Try
        End Function

How to retrieve the Primary key value where the id is a GUID and store that in the variable so that I can use that as a foreign Key for data insertion in another table 

Thanks


Viewing all articles
Browse latest Browse all 3509

Trending Articles



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