473,378 Members | 1,319 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Why cannot insert all the values in the database?

Hi,

The user_id is set as primary key.
"Cannot insert the value NULL into column 'user_id', table 'C:\DOCUMENTS AND SETTINGS\KAREN\MY DOCUMENTS\VISUAL STUDIO 2005\WEBSITES\WEBSITE1\APP_DATA\DB.MDF.dbo.users'; column does not allow nulls. INSERT fails.
The statement has been terminated." after debugging it.

protected void btnAdd_Click(object sender, EventArgs e)
{
SqlConnection MyConnection;
SqlCommand MyCommand;

MyConnection = new SqlConnection();
MyConnection.ConnectionString =
ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
MyConnection.Open();

MyCommand = new SqlCommand("StoredProcedure1", MyConnection);
MyCommand.CommandType = CommandType.StoredProcedure;
MyCommand.Parameters.Add("@user_login", txtUserLogin.Text);
MyCommand.Parameters.Add("@user_password", txtPassword.Text);
MyCommand.Parameters.Add("@user_name", txtName.Text);
MyCommand.Parameters.Add("@user_position", txtPosition.Text);
MyCommand.Parameters.Add("@user_hp", "+6" + txtHpA.Text + txtHpB.Text);
MyCommand.Parameters.Add("@user_email", txtEmail.Text);
MyCommand.Parameters.Add("@user_dept", txtDept.Text);
MyCommand.Parameters.Add("@user_date_added", DateTime.Today.ToString());

if (CBActive.Checked == true)
{
MyCommand.Parameters.Add("@user_is_approved", 1);
}
else
{
MyCommand.Parameters.Add("@user_is_approved", -1);
}

MyCommand.ExecuteScalar();

MyConnection.Close();
Response.Redirect("UserView.aspx?admin_id=" + Request.QueryString["admin_id"]);
}
Plz help.

Tq.

Karen
Mar 11 '08 #1
2 1194
Plater
7,872 Expert 4TB
That seems pretty darn obvious to me.
That column is the primary key and is left null.
Don't leave it null and your insert should work fine.
Mar 11 '08 #2
kunal pawar
297 100+
Hi,

Here userid_id is not passed so query try to insert column as NULL, and primary is not null. so it throw an exception. Please pass user_id or make autonumber

To make column autonumber in sql server set property identity as yes and
identity speed =1 and identity increment=1
Mar 12 '08 #3

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

Similar topics

1
by: Krista | last post by:
Hi guys, i have a problem about inserting data into the table. I dont know why the output always said cannot be added to the database! Anyone has idea for me? i check the connect is ok, but dont...
15
by: Jack | last post by:
I have a text file of data in a file (add2db.txt) where the entries are already entered on separate lines in the following form: INSERT INTO `reviews` VALUES("", "Tony's", "Lunch", "Great...
3
by: jason | last post by:
How does one loop through the contents of a form complicated by dynamic construction of checkboxes which are assigned a 'model' and 'listingID' to the NAME field on the fly in this syntax:...
10
by: shank | last post by:
I have a recordset that contains multiple records of product a user is purchasing. For clarity, I converted the recordset fields to variables. I need to take that entire recordset and insert it...
5
by: BashiraInTrouble | last post by:
Hi Friends, I have tried almost everything but I cant seem to shrink the transaction log. Executing DBCC SQLPERF(LOGSPACE) gives me this info: Database Log Size (MB) Log Space Used (%) ...
14
by: serge | last post by:
I have a scenario where two tables are in a One-to-Many relationship and I need to move the data from the Many table to the One table so that it becomes a One-to-One relationship. I need to...
7
by: David Bear | last post by:
I have a dictionary that contains a row of data intended for a data base. The dictionary keys are the field names. The values are the values to be inserted. I am looking for a good pythonic...
24
by: clinttoris | last post by:
Could some please finish this line of code. For the life of me whatever I try it does not work. I am trying to insert the current date time into oracle. Now I know that in oracle if I run ...
3
by: bb nicole | last post by:
Below is my job post code, it cannot post into database... WHAT IS THE PROBLEM OF IT?? <?php session_start(); ob_start(); //connect to server and select database
0
by: yukijocelyn | last post by:
I have experienced a problem here while doing a form for accessing datadbase using MS Access. I'm using the AccessDataSource control, and using Gridview to pull the data from the database. I am able...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.