Solved

INSERT INTO with sqlCommand object not working

Posted on 2004-09-13
10
500 Views
Last Modified: 2008-03-10
When I use the following code:

                strSQL = "INSERT INTO  tblCookTimes(ItemNum, KettleSize, Cooktime) VALUES ('8085001013', '650', '60' )"
                cmdUpdateTime.CommandText = strSQL
                cmdUpdateTime.ExecuteNonQuery()

I get an error saying that the field "RecID" in the table "tblCookTimes" cannot be null.  

I know that I set RecID as an Identity set to increment by 1 and that if I use a straight SQL statement in SQL Query Analyzer it will work and just insert the record and increment the RecID.  But in code it fails.  

How do I get around this? Thanks in advance.
0
Comment
Question by:mrbass78
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 8

Expert Comment

by:wguerram
ID: 12047609
What Command object are you using?

OleDbCommand or SqlCommand
0
 

Author Comment

by:mrbass78
ID: 12048300
SqlCommand
0
 
LVL 15

Expert Comment

by:Timbo87
ID: 12049133
Is it also a primary key?
0
 

Author Comment

by:mrbass78
ID: 12049238
Yes
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12049523
Are there any other fields in "tblCookTimes" table, except these four ?
-Baan
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 8

Expert Comment

by:wguerram
ID: 12049609
Remove the primary key attribute of the column and let it allow nulls.

and try your code.
0
 

Author Comment

by:mrbass78
ID: 12049694
Baan:  No just those four fields are in the table.  
wguerram: I want it to be the primary key and I want it to have unique values, and I don't want to manage that through code.
0
 
LVL 8

Expert Comment

by:wguerram
ID: 12049712
I just want to see what happens...

I understand you need it as a primary.
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12049761
ok, then don't specify the field name while inserting..
strSQL = "INSERT INTO  tblCookTimes VALUES ('8085001013', '650', '60' )"   'Use this.

-Baan
0
 
LVL 8

Accepted Solution

by:
wguerram earned 250 total points
ID: 12054665
I Tried inserting values in my sqlserver and didn't problems.
What is you framework version? Sql Sever Version?

Can you post your code starting from the point when you create your connection and connection string also.
'--------------------------------------------


This a Work around:


Create a stored procedure in your SQLServer DB:

CREATE PROCEDURE proc_Insert_tblCookTimes (@ItemNum char(10), @KettleSize char(10), @Cooktime char(10))  AS

INSERT INTO tblCookTimes (ItemNum, KettleSize, Cooktime) VALUES (@ItemNum, @KettleSize, @Cooktime)
GO
'End of store procedure

'---------------------------------
In you VB Code


Dim cmd As New System.Data.SqlClient.SqlCommand("proc_Insert_tblCookTimes", cn)
       
        With cmdUpdateTime
            .CommandText = "proc_Insert_tblCookTimes"
            .Parameters.Add(New System.Data.SqlClient.SqlParameter("@ItemNum", Data.SqlDbType.Char, 10)).Value = "8085001013"
            .Parameters.Add(New System.Data.SqlClient.SqlParameter("@KettleSize", Data.SqlDbType.Char, 10)).Value = "650"
            .Parameters.Add(New System.Data.SqlClient.SqlParameter("@Cooktime", Data.SqlDbType.Char, 10)).Value = "60"
            .CommandType = Data.CommandType.StoredProcedure
           
            .ExecuteNonQuery()
        End With
               
        cmdUpdateTime.Parameters.Clear()
        cmdUpdateTime.Dispose()


If you have problems, let me know it.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

16 Experts available now in Live!

Get 1:1 Help Now