Solved

INSERT Command in VB6 to a Table with RowGUID

Posted on 2006-11-02
15
598 Views
Last Modified: 2008-02-01
Ok

I'm doing a simple insert from a VB6 application and the INSERT command gives me the error:

Cannot insert the vaule NULL into column 'rowguid' table.  bla-bla-bla.  Column doesn't allow NULLS.  

How do I get around this?  Is there a way I can generate the ROWGUID, put it into a variable, then insert it?  I'm using an ADO connection created within the code.

Chris, Baltimore.  
0
Comment
Question by:clebo99
  • 5
  • 5
  • 3
  • +2
15 Comments
 
LVL 2

Expert Comment

by:eheimer
Comment Utility
What is the SQL command you are using to INSERT?  Use 'debug.print' to get the exact statement before you send it to the database.
0
 

Author Comment

by:clebo99
Comment Utility
I'm using a general INSERT command.  

INSERT into TABLENAME (info1, info2, info3.....) values (var1, var2, var3....) using the "','" and such.   Even if I go into SQL Query Analyzer and try to insert a record I get the same error so it's that ROWGUID column.

Chris
0
 
LVL 2

Accepted Solution

by:
eheimer earned 500 total points
Comment Utility
If you have a field in the table defined as the primary key, you must provide a value for that field when you do the insert.  The alternative is to define that field as "Identity", which would have MSSQL automatically insert an auto-incrementing value without the need to specify it in the INSERT command.
0
 
LVL 2

Expert Comment

by:eheimer
Comment Utility
To clarify, the problem is not with VB6, or your INSERT command, it's probably on the MSSQL side with the definition of your table and specifically the primary key.  Hope that helps.
0
 

Author Comment

by:clebo99
Comment Utility
Unfortunately, I can't change the DB........There has to be a way to insert in this scenario.  
0
 
LVL 6

Expert Comment

by:DonKyles
Comment Utility
If you cannot change anyting on the DB side, you need to get the latest ROWGUID by using select command and plus 1 (see the sql statement below)

SELECT MAX(ROWGUID) + 1 AS 'NewRowGUID'
FROM TableName

now you can use the new ROWGUID to insert the new record
0
 

Author Comment

by:clebo99
Comment Utility
Perfect...i'll give that a try...

Thanks!!!
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 2

Expert Comment

by:amiableansari
Comment Utility
Hi

ROWGUID is autogenerated unique id. check in table design mode that whether its default value has the value (newid()) and IsRowGUID is set to yes. and in the insert statement you dont need to provide it as parameter and value; the statement should be without rowguid.
0
 

Author Comment

by:clebo99
Comment Utility
DonKyles,

I tried your solution just within Query Analyzer and got the following error message:

Server: Msg 409, Level 16, State 2, Line 1
The maximum aggregate operation cannot take a uniqueidentifier data type as an argument.
Server: Msg 206, Level 16, State 1, Line 1
Operand type clash: uniqueidentifier is incompatible with int

I tried to take out the "+1" and got the MSG 409 error only.  Any thoughts?  Also, can you provide the code where I would actually get this information into a VB6 variable called GUID?

Chris
0
 

Author Comment

by:clebo99
Comment Utility
What about just using a randomizer or something?  Do we think that would work?

Chris
0
 
LVL 2

Expert Comment

by:chrsmrtn
Comment Utility
If your rowguid is not compatible with an int, then "auto incrementing" it from the program side will be very difficult.  Does anyone know what data type this "uniqueidentifier" data type is?  I've never heard of it.  What database are you working with?

Also keep in mind that using a program created incremented key is not a good idea.  It's possible to get two of the same numbers.  Basically if two people try to start your insert process at about the same time, both of them will get the same rowguid from your step 1 and when they both try to save their data in step 2 only one of them will make it into the database.

I'd suggest getting access to the database, either physically or through the DB admin.

Overall i think we need to know the database that you are working with, MYSQL, MSSQL, Oracle, etc.
0
 
LVL 2

Expert Comment

by:eheimer
Comment Utility
This article should shed some light on the difference between uniqueidentifier and identity fields: http://www.sqlteam.com/item.asp?ItemID=283

From that article:

Now, unlike an IDENTITY column, a uniqueidentifier column doesn't automagically get an assigned value when a row is inserted into a table. You either need to place a default on the uniqueidentifier column (DEFAULT NEWID()), or do something like the following:

DECLARE @GUID uniqueidentifier
SET @GUID = NEWID()
INSERT Item VALUES (@GUID,'Yak Hoof')

Hope that helps.
0
 
LVL 2

Expert Comment

by:eheimer
Comment Utility
Here's a link to some VB code that will create a GUID for you:

http://www.freevbcode.com/ShowCode.Asp?ID=21
0
 
LVL 6

Expert Comment

by:DonKyles
Comment Utility
What is the variable type of the GUID?

From what I understand your GUID type is not an integer you may need to convert ROWGUID to int by the following code.

SELECT MAX(CONVERT(INT,ROWGUID)) + 1 AS 'NewRowGUID'
FROM TableName

You may need to give me more information about how did you create the table.
0
 
LVL 6

Expert Comment

by:DonKyles
Comment Utility
Oh I got your problem sorry for the privious post

eheimer is correct

DECLARE @ROWGUID uniqueidentifier
SET @ROWGUID = NEWID()
INSERT TableName VALUES (@ROWGUID, 'Data for column1', 'Data for column2', ...)

is your solution.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
thread-safe code in c++ 2 69
noX challenge 17 75
base64 decode encode 12 93
Controlled Assessment GCSE - desperate help needed 4 47
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now