Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi need a help.


From front end, how can i pass null values to the database if my datatype is 'int', 'Datetime'.
Posted

In this case either you need to modify the input parameters of the SP or you need to send null values from front-end. I would suggest you to use modify input parameter of the sp as null. Try this:
SQL:
SQL
CREATE PROCEDURE spTest
   @Param1 INT = NULL,
   @Param2 DATETIME = NULL
AS 
BEGIN
   -- Your code goes here
END

If user has entered or selected the values then only add the parameters to the command object.
Front-end:
C#
if (textbox1.Text.Trim() != "")
{
    cmd.Parameters.Add("@Param1", Convert.ToInt32(textbox1.Text));
}
if (datetimepicker.SelectedDate.HasValue)
{
    cmd.Parameters.Add("@Param2", datetimepicker.SelectedDate);
}



All the best.
--Amit
 
Share this answer
 
Hi ,
Check this similar
send null to sql bit type from c#[^]
Best Regards
M.Mitwalli
 
Share this answer
 
hi....

For allowing null values, u must use datatype 'int?' instead of 'int' for a variable. Then u can set null value for that variable and pass it to database.
 
Share this answer
 
hi see this syntax:
C#
TextBox1.Text = System.Data.SqlTypes.SqlInt32.Null;
 
Share this answer
 
v2
add "?" in front of int or DateTime like:
int? or DateTime?
? Means it accepts Null values.
Gud Luck.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900