Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Working with UniqueIdentifier field (UserID)

Status
Not open for further replies.

Andel

Programmer
Feb 15, 2001
366
0
0
US
I ceated a table with a UserID field. This is a uniqueidentifier type field. (I would use this as foreign key of the UserID from aspnetdb..aspnet_users table.)

The problem is that I can't figure out how to insert data in it. Here's my current code.

Code:
     Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

        'get user id
        'if user is not logged in then exit sub

        Dim CurrentUser As Guid
        Dim theUser As MembershipUser = Membership.GetUser()
        If theUser Is Nothing Then
            lblMessage.Text = "You need to log in first."
            Exit Sub
        Else
            CurrentUser = theUser.ProviderUserKey
        End If

        'insert data
        SqlDataSource1.InsertParameters("UserID").DefaultValue = CurrentUser [b]'(THIS WON'T WORK)[/b]
        SqlDataSource1.InsertParameters("CategoryID").DefaultValue = drpCategoryID.DataValueField
        SqlDataSource1.InsertParameters("Title").DefaultValue = txtTopicTitle.Text
        SqlDataSource1.InsertParameters("Details").DefaultValue = txtTopicDetails.Text

    End Sub

The first line under 'insert data' gives me this error: Value of type 'System.Guid' cannot be converted to 'String'.

I would appreciate any help.

Thanks.


 
Hi,

GUIDs are quite a unique datatype (if you pardon the pun!).
Although they are Unique Identifiers, they are also represented as a string. This is why you are getting the error that you are.

Convert the GUID to a string and insert that instead. This should work.
Are you using Stored Procedures to insert your data and if so, have you tested the SP from within SQLServer (assuming you are using SQLServer)? If you have, you will have probably tested by enclosing the GUID in single quotes hence making it a string that you are passing in.
 
I'm using the above code to insert my data.

I tried converting it to a string but it gave me different error.

Can you please give me an example?

 
Is UserID a GUID in your sql table? if so, maybe the value you are trying to assing to the variable is a string and not a guid.. I suggest stepping through your code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top