We help IT Professionals succeed at work.

SQL 2000 uniqueidentifier field and using VB .NET

Belificus
Belificus asked
on
1,059 Views
Last Modified: 2012-06-21
I am trying to learn how to create a unique identifer for an SQL database using VB .NET.

Say this is the table, tblMembers in a database designed to track people.

tblMembers has fields
fldUniqueID as uniqueidentifer
fldFName as varchar
fldLName as varchar

When I try to write to the database I get errors that the type is not compatible with type uniqueidentifier.

I could have used typ Int and simply added 1 each time a new record is created, but I want to learn how to use the uniqueidentifier.

Can anyone explain that to me?

This is just an exercise attempt, here is my user control code off the "Add Member" button...

Public Class ucAddMembers
    Private Sub btnAddMember_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddMember.Click
        Dim UniqueID As ?????
        Try
            cnnMembers.Open()
            cmdMembers.Connection = cnnMembers
            cmdMembers.CommandText = "INSERT INTO tblMembers VALUES('" & UniqueID & "','" & txtFName.Text & "','" & txtLName.Text & "'" & ");"
            cmdMembers.ExecuteNonQuery()
            MsgBox("Record added successfully!")
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            cnnMembers.Close()
        End Try
    End Sub
End Class

Comment
Watch Question

Commented:
uniqueidentifier is unique across the database, it should never be repeated in any table. If you are inserting the value then sql server can't be sure you haven't used the value before. Don't give a value for fldUniqueID. When you do the insert sql server will create its own value and stored in the row.

Author

Commented:
Well, I am doing something wrong because SQL did not create a unique ID when the record add attempt was made.

Let me re-create the error messsage and see what it says

Author

Commented:
Ok.

When creating the INSERT commandtext, the values are given in the order in which the fields appear.

The error i get if I leave out a variable for the uniqueidentifier is that it cannot convert from type string to type uniqueidentifier.
This is because the INSERT is tyring to put the FNAME text into the first field in the table which is fldUniqueID.

I'd like to learn how to use that table field type, uniqueidentifier.
your SQL should read like this:

cmdMembers.CommandText = "INSERT INTO tblMembers (fldFname, fldLName) VALUES('" & txtFName.Text & "','" & txtLName.Text & "'" & ");"

and if the fldUniqueID has been created as Type UniqueIdentifier (which, by the way is the SQL Server equivalent of an Access AUtoNumber field), the fldUniqueID will be automatically created for the record, when it is inserted, so there is no need to provide the value in you insert SQL.

be sure to change (fldFname, fldLName)  to the actual names of those two fields in your table.

AW

Author

Commented:
Arthur

I get errors as follows.

Cannot insert the value NULL into column 'fldUniqueID', table 'Members.dbo.tblMembers';coumn does nto allow nulls.  INSERT fails.
The statement has been terminated.

Here is the code:

        Try
            cnnMembers.Open()
            cmdMembers.Connection = cnnMembers


            cmdMembers.CommandText = "INSERT INTO tblMembers (fldFname, fldLName) VALUES('" & txtFName.Text & "','" & txtLName.Text & "'" & ");"


            cmdMembers.ExecuteNonQuery()
            MsgBox("Record added successfully!")
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            cnnMembers.Close()
        End Try
ok, then you do it this way:

        Dim UniqueID As GUID =  Guid.NewGuid
        Try
            cnnMembers.Open()
            cmdMembers.Connection = cnnMembers
            cmdMembers.CommandText = "INSERT INTO tblMembers VALUES(" & UniqueID & ",'" & txtFName.Text & "','" & txtLName.Text & "'" & ");"
            cmdMembers.ExecuteNonQuery()
            MsgBox("Record added successfully!")
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            cnnMembers.Close()
        End Try

AW

Author

Commented:
This will not build.

Will the GUID create a unique value for each new record added?


Error      1      Operator '&' is not defined for types 'String' and 'System.Guid'.      C:\Documents and Settings\binary\My Documents\Visual Studio 2005\Projects\MemberTrakker\MemberTrakker\ucAddMembers.vb      27      38      MemberTrakker
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Arthur:

It looks like SQL is returning syntax errors that are in memory.  

The msgbox is ERROR [42000][Microsoft][ODBC SQL Server Driver][SQL Server] Line 1: Incorrect Syntax near 'c03fb'.

c03fb looked liek a memory address so I ran it again and got the same line but with 'b56dc' instead.
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thank you gstieger!

I split points on this because I think Arthur was very close.

gstieger 250
Arthur 50

Thanks again for your help!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.