I cannot save data to my SQL database using C#
I am new to C#. I am trying to save the numbers into a SQL Server database table (locally) but I get an error:
Cannot insert the value NULL into column
My code:
private void SaveBtn_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=C:Usersfn1965DesktopWorkTESTDBNumDB.mdf;Integrated Security=True;Connect Timeout=30");
conn.Open();
string insert_query = "INSERT into [NumericTable] (Num1, Num2, Total) VALUES (@Num1, @Num2, @Total)";
SqlCommand cmd = new SqlCommand(insert_query, conn);
cmd.Parameters.AddWithValue("@Num1", textBox1.Text);
cmd.Parameters.AddWithValue("@Num2", textBox2.Text);
cmd.Parameters.AddWithValue("@Total", textBox3.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Record saved");
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show("EROR:"+ ex.ToString());
}
}
Table schema
c#
|
show 7 more comments
I am new to C#. I am trying to save the numbers into a SQL Server database table (locally) but I get an error:
Cannot insert the value NULL into column
My code:
private void SaveBtn_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=C:Usersfn1965DesktopWorkTESTDBNumDB.mdf;Integrated Security=True;Connect Timeout=30");
conn.Open();
string insert_query = "INSERT into [NumericTable] (Num1, Num2, Total) VALUES (@Num1, @Num2, @Total)";
SqlCommand cmd = new SqlCommand(insert_query, conn);
cmd.Parameters.AddWithValue("@Num1", textBox1.Text);
cmd.Parameters.AddWithValue("@Num2", textBox2.Text);
cmd.Parameters.AddWithValue("@Total", textBox3.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Record saved");
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show("EROR:"+ ex.ToString());
}
}
Table schema
c#
3
Sounds like you're trying to insert NULL into a column that is defined in the database as "do not allow nulls."
– Robert Harvey♦
Jan 2 at 19:58
It means you are passing a null value to a column in your table which does not exceptNULLS
– Ryan Wilson
Jan 2 at 19:58
1
Look at the schema information for the table NumericTable. Do you have other columns that are not set from your query? Are these columns allowed to get NULL values?
– Steve
Jan 2 at 20:00
1
Provide the table structure, hope you have some primary key and not setting value during insert.
– Shan
Jan 2 at 20:00
1
Side note: if these columns contain numerical data - why on earth are they defined asnvarchar(50)
?? Use the most appropriate datatype - always, no exceptions. Andnvarchar(50)
is certainly NOT the most appropriate datatype to store numerical values! Useint
,bigint
, ordecimal
- whatever suits your needs
– marc_s
Jan 2 at 22:24
|
show 7 more comments
I am new to C#. I am trying to save the numbers into a SQL Server database table (locally) but I get an error:
Cannot insert the value NULL into column
My code:
private void SaveBtn_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=C:Usersfn1965DesktopWorkTESTDBNumDB.mdf;Integrated Security=True;Connect Timeout=30");
conn.Open();
string insert_query = "INSERT into [NumericTable] (Num1, Num2, Total) VALUES (@Num1, @Num2, @Total)";
SqlCommand cmd = new SqlCommand(insert_query, conn);
cmd.Parameters.AddWithValue("@Num1", textBox1.Text);
cmd.Parameters.AddWithValue("@Num2", textBox2.Text);
cmd.Parameters.AddWithValue("@Total", textBox3.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Record saved");
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show("EROR:"+ ex.ToString());
}
}
Table schema
c#
I am new to C#. I am trying to save the numbers into a SQL Server database table (locally) but I get an error:
Cannot insert the value NULL into column
My code:
private void SaveBtn_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=C:Usersfn1965DesktopWorkTESTDBNumDB.mdf;Integrated Security=True;Connect Timeout=30");
conn.Open();
string insert_query = "INSERT into [NumericTable] (Num1, Num2, Total) VALUES (@Num1, @Num2, @Total)";
SqlCommand cmd = new SqlCommand(insert_query, conn);
cmd.Parameters.AddWithValue("@Num1", textBox1.Text);
cmd.Parameters.AddWithValue("@Num2", textBox2.Text);
cmd.Parameters.AddWithValue("@Total", textBox3.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Record saved");
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show("EROR:"+ ex.ToString());
}
}
Table schema
c#
c#
edited Jan 2 at 22:23
marc_s
583k13011241270
583k13011241270
asked Jan 2 at 19:56
Francis NavalFrancis Naval
33
33
3
Sounds like you're trying to insert NULL into a column that is defined in the database as "do not allow nulls."
– Robert Harvey♦
Jan 2 at 19:58
It means you are passing a null value to a column in your table which does not exceptNULLS
– Ryan Wilson
Jan 2 at 19:58
1
Look at the schema information for the table NumericTable. Do you have other columns that are not set from your query? Are these columns allowed to get NULL values?
– Steve
Jan 2 at 20:00
1
Provide the table structure, hope you have some primary key and not setting value during insert.
– Shan
Jan 2 at 20:00
1
Side note: if these columns contain numerical data - why on earth are they defined asnvarchar(50)
?? Use the most appropriate datatype - always, no exceptions. Andnvarchar(50)
is certainly NOT the most appropriate datatype to store numerical values! Useint
,bigint
, ordecimal
- whatever suits your needs
– marc_s
Jan 2 at 22:24
|
show 7 more comments
3
Sounds like you're trying to insert NULL into a column that is defined in the database as "do not allow nulls."
– Robert Harvey♦
Jan 2 at 19:58
It means you are passing a null value to a column in your table which does not exceptNULLS
– Ryan Wilson
Jan 2 at 19:58
1
Look at the schema information for the table NumericTable. Do you have other columns that are not set from your query? Are these columns allowed to get NULL values?
– Steve
Jan 2 at 20:00
1
Provide the table structure, hope you have some primary key and not setting value during insert.
– Shan
Jan 2 at 20:00
1
Side note: if these columns contain numerical data - why on earth are they defined asnvarchar(50)
?? Use the most appropriate datatype - always, no exceptions. Andnvarchar(50)
is certainly NOT the most appropriate datatype to store numerical values! Useint
,bigint
, ordecimal
- whatever suits your needs
– marc_s
Jan 2 at 22:24
3
3
Sounds like you're trying to insert NULL into a column that is defined in the database as "do not allow nulls."
– Robert Harvey♦
Jan 2 at 19:58
Sounds like you're trying to insert NULL into a column that is defined in the database as "do not allow nulls."
– Robert Harvey♦
Jan 2 at 19:58
It means you are passing a null value to a column in your table which does not except
NULLS
– Ryan Wilson
Jan 2 at 19:58
It means you are passing a null value to a column in your table which does not except
NULLS
– Ryan Wilson
Jan 2 at 19:58
1
1
Look at the schema information for the table NumericTable. Do you have other columns that are not set from your query? Are these columns allowed to get NULL values?
– Steve
Jan 2 at 20:00
Look at the schema information for the table NumericTable. Do you have other columns that are not set from your query? Are these columns allowed to get NULL values?
– Steve
Jan 2 at 20:00
1
1
Provide the table structure, hope you have some primary key and not setting value during insert.
– Shan
Jan 2 at 20:00
Provide the table structure, hope you have some primary key and not setting value during insert.
– Shan
Jan 2 at 20:00
1
1
Side note: if these columns contain numerical data - why on earth are they defined as
nvarchar(50)
?? Use the most appropriate datatype - always, no exceptions. And nvarchar(50)
is certainly NOT the most appropriate datatype to store numerical values! Use int
, bigint
, or decimal
- whatever suits your needs– marc_s
Jan 2 at 22:24
Side note: if these columns contain numerical data - why on earth are they defined as
nvarchar(50)
?? Use the most appropriate datatype - always, no exceptions. And nvarchar(50)
is certainly NOT the most appropriate datatype to store numerical values! Use int
, bigint
, or decimal
- whatever suits your needs– marc_s
Jan 2 at 22:24
|
show 7 more comments
2 Answers
2
active
oldest
votes
You can see in the image that the column Id
is the only one that does not support null values. Since the column is not identity and as you are not providing a value on your insert, then the INSERT fail with the given exception. This code will work (only if there isn't a record with Id = 1 already):
string insert_query = "INSERT into [NumericTable] (Num1,Num2,Total, Id) Values (@Num1,@Num2,@Total, @id)";
SqlCommand cmd = new SqlCommand(insert_query, conn);
cmd.Parameters.AddWithValue("@Num1", textBox1.Text);
cmd.Parameters.AddWithValue("@Num2", textBox2.Text);
cmd.Parameters.AddWithValue("@Total", textBox3.Text);
cmd.Parameters.AddWithValue("@Id", 1);
cmd.ExecuteNonQuery();
I assume that this is obviously not the desired fuctionality. What you should do is either set the Id column to identity = true or set a value on the insert.
I also encourage you to not use AddWithValue
method since it can lead you to some undesired problems. You can read more here: https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/
this will not work if there is a record with anID = 1
as it would violate the primary key constraint. I don't imagine that was intentional, just being nit-picky. If the OP desired to insert their own primary key, they would need to either query their table first to find a unique integer or modify their query to find a unique integer value and use it as the primary key. In any case, using an Identity auto-increment is much easier.
– Ryan Wilson
Jan 2 at 20:27
I recommend not using this method of data access st all, and learn Entity Framework instead! :)
– Caius Jard
Jan 2 at 20:27
1
@RyanWilson you are right i will make the corresponding edit
– NicoRiff
Jan 2 at 20:29
@NicoRiff Thank you for updating your answer. :)
– Ryan Wilson
Jan 2 at 20:31
@CaiusJard it is matter of what everyone likes to use. I prefer to use Dapper as I personally do not like EF. If OP wants to have more control over DB and go on writing queries I will definitely go with Dapper. But that is my preference.
– NicoRiff
Jan 2 at 20:33
|
show 1 more comment
That screenshot you took of your table columns design; get back to that, then click the id column, look in the Properties grid for Identity Specification (might need to expand it) and set it to Yes. Set other properties relevant to your needs and save the table.
Borrowed from another SO question:
There are ways to do this from script but they're generally longer/more awkward than using the UI in management studio.
This will (should) change th column so it auto inserts an incrementing number into itself when you insert values for other rows. Someone else has posted an answer as to how to insert values for it yourself but my recommendation to you as a learner is to use auto increment to save the additional needless complication of providing your own primary key values
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54012376%2fi-cannot-save-data-to-my-sql-database-using-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can see in the image that the column Id
is the only one that does not support null values. Since the column is not identity and as you are not providing a value on your insert, then the INSERT fail with the given exception. This code will work (only if there isn't a record with Id = 1 already):
string insert_query = "INSERT into [NumericTable] (Num1,Num2,Total, Id) Values (@Num1,@Num2,@Total, @id)";
SqlCommand cmd = new SqlCommand(insert_query, conn);
cmd.Parameters.AddWithValue("@Num1", textBox1.Text);
cmd.Parameters.AddWithValue("@Num2", textBox2.Text);
cmd.Parameters.AddWithValue("@Total", textBox3.Text);
cmd.Parameters.AddWithValue("@Id", 1);
cmd.ExecuteNonQuery();
I assume that this is obviously not the desired fuctionality. What you should do is either set the Id column to identity = true or set a value on the insert.
I also encourage you to not use AddWithValue
method since it can lead you to some undesired problems. You can read more here: https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/
this will not work if there is a record with anID = 1
as it would violate the primary key constraint. I don't imagine that was intentional, just being nit-picky. If the OP desired to insert their own primary key, they would need to either query their table first to find a unique integer or modify their query to find a unique integer value and use it as the primary key. In any case, using an Identity auto-increment is much easier.
– Ryan Wilson
Jan 2 at 20:27
I recommend not using this method of data access st all, and learn Entity Framework instead! :)
– Caius Jard
Jan 2 at 20:27
1
@RyanWilson you are right i will make the corresponding edit
– NicoRiff
Jan 2 at 20:29
@NicoRiff Thank you for updating your answer. :)
– Ryan Wilson
Jan 2 at 20:31
@CaiusJard it is matter of what everyone likes to use. I prefer to use Dapper as I personally do not like EF. If OP wants to have more control over DB and go on writing queries I will definitely go with Dapper. But that is my preference.
– NicoRiff
Jan 2 at 20:33
|
show 1 more comment
You can see in the image that the column Id
is the only one that does not support null values. Since the column is not identity and as you are not providing a value on your insert, then the INSERT fail with the given exception. This code will work (only if there isn't a record with Id = 1 already):
string insert_query = "INSERT into [NumericTable] (Num1,Num2,Total, Id) Values (@Num1,@Num2,@Total, @id)";
SqlCommand cmd = new SqlCommand(insert_query, conn);
cmd.Parameters.AddWithValue("@Num1", textBox1.Text);
cmd.Parameters.AddWithValue("@Num2", textBox2.Text);
cmd.Parameters.AddWithValue("@Total", textBox3.Text);
cmd.Parameters.AddWithValue("@Id", 1);
cmd.ExecuteNonQuery();
I assume that this is obviously not the desired fuctionality. What you should do is either set the Id column to identity = true or set a value on the insert.
I also encourage you to not use AddWithValue
method since it can lead you to some undesired problems. You can read more here: https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/
this will not work if there is a record with anID = 1
as it would violate the primary key constraint. I don't imagine that was intentional, just being nit-picky. If the OP desired to insert their own primary key, they would need to either query their table first to find a unique integer or modify their query to find a unique integer value and use it as the primary key. In any case, using an Identity auto-increment is much easier.
– Ryan Wilson
Jan 2 at 20:27
I recommend not using this method of data access st all, and learn Entity Framework instead! :)
– Caius Jard
Jan 2 at 20:27
1
@RyanWilson you are right i will make the corresponding edit
– NicoRiff
Jan 2 at 20:29
@NicoRiff Thank you for updating your answer. :)
– Ryan Wilson
Jan 2 at 20:31
@CaiusJard it is matter of what everyone likes to use. I prefer to use Dapper as I personally do not like EF. If OP wants to have more control over DB and go on writing queries I will definitely go with Dapper. But that is my preference.
– NicoRiff
Jan 2 at 20:33
|
show 1 more comment
You can see in the image that the column Id
is the only one that does not support null values. Since the column is not identity and as you are not providing a value on your insert, then the INSERT fail with the given exception. This code will work (only if there isn't a record with Id = 1 already):
string insert_query = "INSERT into [NumericTable] (Num1,Num2,Total, Id) Values (@Num1,@Num2,@Total, @id)";
SqlCommand cmd = new SqlCommand(insert_query, conn);
cmd.Parameters.AddWithValue("@Num1", textBox1.Text);
cmd.Parameters.AddWithValue("@Num2", textBox2.Text);
cmd.Parameters.AddWithValue("@Total", textBox3.Text);
cmd.Parameters.AddWithValue("@Id", 1);
cmd.ExecuteNonQuery();
I assume that this is obviously not the desired fuctionality. What you should do is either set the Id column to identity = true or set a value on the insert.
I also encourage you to not use AddWithValue
method since it can lead you to some undesired problems. You can read more here: https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/
You can see in the image that the column Id
is the only one that does not support null values. Since the column is not identity and as you are not providing a value on your insert, then the INSERT fail with the given exception. This code will work (only if there isn't a record with Id = 1 already):
string insert_query = "INSERT into [NumericTable] (Num1,Num2,Total, Id) Values (@Num1,@Num2,@Total, @id)";
SqlCommand cmd = new SqlCommand(insert_query, conn);
cmd.Parameters.AddWithValue("@Num1", textBox1.Text);
cmd.Parameters.AddWithValue("@Num2", textBox2.Text);
cmd.Parameters.AddWithValue("@Total", textBox3.Text);
cmd.Parameters.AddWithValue("@Id", 1);
cmd.ExecuteNonQuery();
I assume that this is obviously not the desired fuctionality. What you should do is either set the Id column to identity = true or set a value on the insert.
I also encourage you to not use AddWithValue
method since it can lead you to some undesired problems. You can read more here: https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/
edited Jan 2 at 20:30
answered Jan 2 at 20:22


NicoRiffNicoRiff
3,79821943
3,79821943
this will not work if there is a record with anID = 1
as it would violate the primary key constraint. I don't imagine that was intentional, just being nit-picky. If the OP desired to insert their own primary key, they would need to either query their table first to find a unique integer or modify their query to find a unique integer value and use it as the primary key. In any case, using an Identity auto-increment is much easier.
– Ryan Wilson
Jan 2 at 20:27
I recommend not using this method of data access st all, and learn Entity Framework instead! :)
– Caius Jard
Jan 2 at 20:27
1
@RyanWilson you are right i will make the corresponding edit
– NicoRiff
Jan 2 at 20:29
@NicoRiff Thank you for updating your answer. :)
– Ryan Wilson
Jan 2 at 20:31
@CaiusJard it is matter of what everyone likes to use. I prefer to use Dapper as I personally do not like EF. If OP wants to have more control over DB and go on writing queries I will definitely go with Dapper. But that is my preference.
– NicoRiff
Jan 2 at 20:33
|
show 1 more comment
this will not work if there is a record with anID = 1
as it would violate the primary key constraint. I don't imagine that was intentional, just being nit-picky. If the OP desired to insert their own primary key, they would need to either query their table first to find a unique integer or modify their query to find a unique integer value and use it as the primary key. In any case, using an Identity auto-increment is much easier.
– Ryan Wilson
Jan 2 at 20:27
I recommend not using this method of data access st all, and learn Entity Framework instead! :)
– Caius Jard
Jan 2 at 20:27
1
@RyanWilson you are right i will make the corresponding edit
– NicoRiff
Jan 2 at 20:29
@NicoRiff Thank you for updating your answer. :)
– Ryan Wilson
Jan 2 at 20:31
@CaiusJard it is matter of what everyone likes to use. I prefer to use Dapper as I personally do not like EF. If OP wants to have more control over DB and go on writing queries I will definitely go with Dapper. But that is my preference.
– NicoRiff
Jan 2 at 20:33
this will not work if there is a record with an
ID = 1
as it would violate the primary key constraint. I don't imagine that was intentional, just being nit-picky. If the OP desired to insert their own primary key, they would need to either query their table first to find a unique integer or modify their query to find a unique integer value and use it as the primary key. In any case, using an Identity auto-increment is much easier.– Ryan Wilson
Jan 2 at 20:27
this will not work if there is a record with an
ID = 1
as it would violate the primary key constraint. I don't imagine that was intentional, just being nit-picky. If the OP desired to insert their own primary key, they would need to either query their table first to find a unique integer or modify their query to find a unique integer value and use it as the primary key. In any case, using an Identity auto-increment is much easier.– Ryan Wilson
Jan 2 at 20:27
I recommend not using this method of data access st all, and learn Entity Framework instead! :)
– Caius Jard
Jan 2 at 20:27
I recommend not using this method of data access st all, and learn Entity Framework instead! :)
– Caius Jard
Jan 2 at 20:27
1
1
@RyanWilson you are right i will make the corresponding edit
– NicoRiff
Jan 2 at 20:29
@RyanWilson you are right i will make the corresponding edit
– NicoRiff
Jan 2 at 20:29
@NicoRiff Thank you for updating your answer. :)
– Ryan Wilson
Jan 2 at 20:31
@NicoRiff Thank you for updating your answer. :)
– Ryan Wilson
Jan 2 at 20:31
@CaiusJard it is matter of what everyone likes to use. I prefer to use Dapper as I personally do not like EF. If OP wants to have more control over DB and go on writing queries I will definitely go with Dapper. But that is my preference.
– NicoRiff
Jan 2 at 20:33
@CaiusJard it is matter of what everyone likes to use. I prefer to use Dapper as I personally do not like EF. If OP wants to have more control over DB and go on writing queries I will definitely go with Dapper. But that is my preference.
– NicoRiff
Jan 2 at 20:33
|
show 1 more comment
That screenshot you took of your table columns design; get back to that, then click the id column, look in the Properties grid for Identity Specification (might need to expand it) and set it to Yes. Set other properties relevant to your needs and save the table.
Borrowed from another SO question:
There are ways to do this from script but they're generally longer/more awkward than using the UI in management studio.
This will (should) change th column so it auto inserts an incrementing number into itself when you insert values for other rows. Someone else has posted an answer as to how to insert values for it yourself but my recommendation to you as a learner is to use auto increment to save the additional needless complication of providing your own primary key values
add a comment |
That screenshot you took of your table columns design; get back to that, then click the id column, look in the Properties grid for Identity Specification (might need to expand it) and set it to Yes. Set other properties relevant to your needs and save the table.
Borrowed from another SO question:
There are ways to do this from script but they're generally longer/more awkward than using the UI in management studio.
This will (should) change th column so it auto inserts an incrementing number into itself when you insert values for other rows. Someone else has posted an answer as to how to insert values for it yourself but my recommendation to you as a learner is to use auto increment to save the additional needless complication of providing your own primary key values
add a comment |
That screenshot you took of your table columns design; get back to that, then click the id column, look in the Properties grid for Identity Specification (might need to expand it) and set it to Yes. Set other properties relevant to your needs and save the table.
Borrowed from another SO question:
There are ways to do this from script but they're generally longer/more awkward than using the UI in management studio.
This will (should) change th column so it auto inserts an incrementing number into itself when you insert values for other rows. Someone else has posted an answer as to how to insert values for it yourself but my recommendation to you as a learner is to use auto increment to save the additional needless complication of providing your own primary key values
That screenshot you took of your table columns design; get back to that, then click the id column, look in the Properties grid for Identity Specification (might need to expand it) and set it to Yes. Set other properties relevant to your needs and save the table.
Borrowed from another SO question:
There are ways to do this from script but they're generally longer/more awkward than using the UI in management studio.
This will (should) change th column so it auto inserts an incrementing number into itself when you insert values for other rows. Someone else has posted an answer as to how to insert values for it yourself but my recommendation to you as a learner is to use auto increment to save the additional needless complication of providing your own primary key values
edited Jan 2 at 20:32
answered Jan 2 at 20:26


Caius JardCaius Jard
12.5k21340
12.5k21340
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54012376%2fi-cannot-save-data-to-my-sql-database-using-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Sounds like you're trying to insert NULL into a column that is defined in the database as "do not allow nulls."
– Robert Harvey♦
Jan 2 at 19:58
It means you are passing a null value to a column in your table which does not except
NULLS
– Ryan Wilson
Jan 2 at 19:58
1
Look at the schema information for the table NumericTable. Do you have other columns that are not set from your query? Are these columns allowed to get NULL values?
– Steve
Jan 2 at 20:00
1
Provide the table structure, hope you have some primary key and not setting value during insert.
– Shan
Jan 2 at 20:00
1
Side note: if these columns contain numerical data - why on earth are they defined as
nvarchar(50)
?? Use the most appropriate datatype - always, no exceptions. Andnvarchar(50)
is certainly NOT the most appropriate datatype to store numerical values! Useint
,bigint
, ordecimal
- whatever suits your needs– marc_s
Jan 2 at 22:24