?
Solved

INSERT INTO with sqlCommand object not working

Posted on 2004-09-13
10
Medium Priority
?
525 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
[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
  • 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
Independent Software Vendors: 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!

 

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
 
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 1000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

650 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