Reputation: 1003
I have a table in which there are some datetime type columns. I use a stored procedure to insert values into the table. In the stored procedure I have variables that accept null for inserting into the table. My problem is when I try to insert a null value into my table's datetime column it throws error. my code is as.
System.Data.SqlTypes.SqlDateTime getDate;
//set DateTime null
getDate = SqlDateTime.Null;
if (InsertDate.Text == "")
{
cmd1.Parameters["@InsertDate"].Value = getDate;
}
else
{
cmd1.Parameters["@InsertDate"].Value = InsertDate.Text;
}
Upvotes: 13
Views: 95173
Reputation: 809
You can use this. There i no shortcut!!
if (InsertDate.Text == "") cmd.Parameters.AddWithValue("@enddate", DBNull.Value); else cmd.Parameters.AddWithValue("@enddate", Convert.ToDateTime(InsertDate.Text));
Upvotes: 0
Reputation: 1
Try this logic if DBNull.Value is not worked .. use DateTime? null
Ensure that ur database field should allow null
dtexpiry!=null? dtexpiry :(DateTime?) null
Upvotes: 0
Reputation: 131
Try something like this, using Add rather than AddWithValue..
DB.Parameters.Add("@date", SqlDbType.DateTime).Value = DBNull.Value;
Upvotes: 2
Reputation: 1526
In the stored procedure do something like this:
insert into table(date)
values(case when @InsertDate ='' then
null
else
@insertDate
end)
Upvotes: 0
Reputation: 91
if (//some condition)
{
cmd.Parameters.AddWithValue("@dateofBirth", txtdob.text);
}
else
{
cmd.Parameters.AddWithValue("@dateofBirth", DBNull.Value);
}
Upvotes: 9
Reputation: 517
To pass null value to db you can use DBNull.Value. Please try this
if (string.IsNullOrWhiteSpace(InsertDate.Text))
{
cmd1.Parameters["@InsertDate"].Value =DBNull.Value;
}
else
{
cmd1.Parameters["@InsertDate"].Value = InsertDate.Text;
}
Upvotes: 5
Reputation: 30882
First of all, to be able to insert null
into a database column, the column must accept null
s. Open the table in the designer, and see if the relevant column is nullable or not. If it's not and you try to insert a null value, you'll get the error:
Cannot insert the value NULL into column 'InsertDate', table 'TableName'; column does not allow nulls.
That said, since you are using stored procedures to insert data, I would recommend using default values for the parameters on the procedure level, instead of burdening the aplication code. Than, you can just not set the parameter on the application side, and it would work correctly:
create procedure InsertIntoTable
-- ....other parameters here...
@InsertDate datetime = null -- this means that if the value is not supplied,
-- null is used by default
as begin
insert into TableName (InsertDate) values (@InsertDate)
end
and on the C# side, just do:
if (!string.IsNullOrWhitespace(InsertDate.Text)) {
// check to see if the entered text is actually a date
DateTime insertDate = ... some parsing/verification code...;
cmd1.Parameters["@InsertDate"].Value = insertDate;
}
Note that there is no else
branch, so if the InsertDate
is empty, the parameter is not sent at all.
Upvotes: 3
Reputation: 883
Try using:
if (string.IsNullOrWhiteSpace(InsertDate.Text))
{
cmd1.Parameters["@InsertDate"].Value = getDate;
}
else
{
cmd1.Parameters["@InsertDate"].Value = InsertDate.Text;
}
Upvotes: 0
Reputation: 29668
You should just be able to supply null
directly as a parameter value:
if (String.IsNullOrWhitespace(InsertDate.Text)) {
cmd1.Parameters["@InsertDate"].Value = null;
} else {
cmd1.Parameters["@InsertDate"].Value = InsertDate.Text;
}
Also switched to using String.IsNullOrWhitespace()
to check for an empty string.
Upvotes: 1
Reputation: 4352
cmd1.Parameters["@InsertDate"].Value = getDate ?? DBNull.Value;
The key being DBNull.Value. I imagine you have a nullable DateTime variable, i.e. DateTime? someVariable
. You can test it for null and if it is use DBNull.Value
(as shown above).
Upvotes: 17