Solved

INSERT INTO with sqlCommand object not working

Posted on 2004-09-13
10
509 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

831 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