473,466 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Can't insert a row to a table with identity on

2 New Member
Hey

I'm trying to nsert a raw to a table that has one column marked with identity ON.

The code is like this:

string sqlString1 = "INSERT into T_Branches (BranchTypeID, BranchName) VALUES (" + _BranchType + ", '" + _BranchName + "')";


SqlConnection objConnection = new SqlConnection();
SqlDataAdapter objAdapater = new SqlDataAdapter();
SqlCommand objCommand = new SqlCommand(sqlString1);
objConnection.ConnectionString = connectionString
objConnection.Open();
objCommand.Connection = objConnection;
objAdapater.InsertCommand = objCommand;
objAdapater.InsertCommand.ExecuteNonQuery();

For some reason I get the following error message:
---------------------------------------------------------------------------------------------------------
Cannot insert the value NULL into column 'BranchID', table 'CAR_RENTAL.MDF.dbo.T_Branches'; column does not allow nulls. INSERT fails.
The statement has been terminated.
--------------------------------------------------------------------------------------------------------
BranchID is the primary key and an identify column

When I try to insert values manually I succeed and the value in BranchID increments automatically

Any idea what am I doing wrong?
Apr 23 '10 #1
3 1763
ck9663
2,878 Recognized Expert Specialist
Here, read this...

Happy Coding!!!

~~ CK
Apr 23 '10 #2
Jake77
2 New Member
I actually turned ALL table's Identity attribute to OFF except for the one in question.
Still I get the same error.

The question is why do I get a "Cant Insert NULL" error message when the values in this column should be automatically inserted and increased by the DB?

Tnx!
Apr 23 '10 #3
shek124
50 New Member
@Jake77
hi,

the primary key column does n't allow nulls value. So you have to insert the values in the column using insert query
Apr 30 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Paul | last post by:
I need to insert a row into a table in SQL Server 2000. The primary key for the row is an identity type, so it auto-numbers for me without needing to put in the value in the insert statement. ...
6
by: Maria L | last post by:
I have a client who needs to copy an existing sale. The problem is the Sale is made up of three tables: Sale, SaleEquipment, SaleParts. Each sale can have multiple pieces of equipment with...
6
by: Dave | last post by:
I have to automate a process that assigns sales leads to sales people. For example: Every day we buy a list of sales leads, it ranges in size from 50 - 100 records. We have a team of sales...
2
by: Tom Gao | last post by:
I have a problem. The project that I'm working on requires me to duplicate records. As in a series of records are entered into the system the user then click on a button to make these as...
2
by: Eugene | last post by:
Hi, Problem: I need to get the value of auto-incremented field from just inserted record In Oracle this is INSERT .. RETURNING command. In SQL Server there are @@IDENTITY, IDENT_CURRENT,...
8
by: Bri | last post by:
Greetings, I'm having a very strange problem in an AC97 MDB with ODBC Linked tables to SQL Server 7. The table has an Identity field and a Timestamp field. The problem is that when a new record...
8
by: shenanwei | last post by:
I have 2 same windows machine, same instance configure and Database , all run DB2 UDB V8.1.5 Test 1 : create table OUT_1 (LINE VARCHAR(350), LINENUMBER INTEGER NOT NULL GENERATED ALWAYS AS...
7
by: Cindy H | last post by:
Hi I'm having a problem getting the insert statement correct for an Access table I'm using. The Access table uses an autonumber for the primary key. I have tried this: INSERT INTO Tournaments...
5
by: .Net Sports | last post by:
I'm trying to insert records into an sql database coming from a page using the request ..form method. The table "general" has a primary key 'geid .' I get the following error: Cannot insert the...
6
by: insirawali | last post by:
Hi all, I have this problem, i need to know is there a way i cn use the data adapter's update method in this scenario. i have 3 tables as below create table table1{ id1 int identity(1,1)...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.