Solved

INSERT INTO with sqlCommand object not working

Posted on 2004-09-13
10
519 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
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!

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

717 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