Solved

INSERT Command in VB6 to a Table with RowGUID

Posted on 2006-11-02
15
644 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3
  • +2
15 Comments
 
LVL 2

Expert Comment

by:eheimer
ID: 17861364
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
ID: 17861580
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
ID: 17861854
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:eheimer
ID: 17861888
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
ID: 17863097
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
ID: 17863265
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
ID: 17863517
Perfect...i'll give that a try...

Thanks!!!
0
 
LVL 2

Expert Comment

by:amiableansari
ID: 17864205
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
ID: 17866137
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
ID: 17866231
What about just using a randomizer or something?  Do we think that would work?

Chris
0
 
LVL 2

Expert Comment

by:chrsmrtn
ID: 17866659
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
ID: 17867843
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
ID: 17867924
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
ID: 17871865
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
ID: 17871916
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.

736 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