Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hello There

I have a C# application in which I have to create a stored procedure for Inserting records.

Currently I am using below function:-
C#
cmd.Parameters.Add(new SqlParameter("PO_Date", Convert.ToDateTime(Convert.ToString(mDT_PoHead.Rows[0]["PO_Date"]) == "" ? null : Convert.ToString(mDT_PoHead.Rows[0]["PO_Date"]))));


So if the datatable field has a blank value then NULL will be passed else actual value pass.

But at the end error arise "SqlDateTime Overflow" (mDT_PoHead.Rows[0]["PO_Date"] has a blank value.)

Need Suggestion
Best Regards
Posted
Updated 10-Jan-13 23:09pm
v2
Comments
Wendelius 11-Jan-13 0:59am    
The question is a bit unclear. Can you edit the question and post the relevant code fragments.
Vishal.Shimpi144 11-Jan-13 1:10am    
can you please post your code
Mayank Topiwala 11-Jan-13 4:24am    
cmd.Parameters.Add(new SqlParameter("PO_Date", Convert.ToDateTime(Convert.ToString(mDT_PoHead.Rows[0]["PO_Date"]) == "" ? null : Convert.ToString(mDT_PoHead.Rows[0]["PO_Date"]))));

is it correct ?

in here you can have two option,
one way is using nullable type, as below (replace the DATA_TABLE_VALUE from you actual value)

C#
DateTime? value = Convert.IsDBNull([DATA_TABLE_VALUE]) ? null : (DateTime?)[DATA_TABLE_VALUE];


2nd option,
Since DateTime is a value type (like int), a Null value cannot be assigned to it. In this instance you can use DateTime.MinValue or
DateTime.MaxValue
instead.
 
Share this answer
 
Comments
Mayank Topiwala 11-Jan-13 4:29am    
cmd.Parameters.Add(new SqlParameter("PO_Date", Convert.ToDateTime(Convert.ToString(mDT_PoHead.Rows[0]["PO_Date"]) == "" ? null : Convert.ToString(mDT_PoHead.Rows[0]["PO_Date"]))));

is it correct ?
Tharaka MTR 11-Jan-13 5:01am    
try this

cmd.Parameters.Add(new SqlParameter("PO_Date", Convert.IsDBNull(mDT_PoHead.Rows[0]["PO_Date"]) ? null : (DateTime?)Convert.ToDateTime(mDT_PoHead.Rows[0]["PO_Date"])));
C#
If(Field[""].ToString()!="")Value=Conver.ToDateTime(Field[""])
 
Share this answer
 
Comments
Mayank Topiwala 11-Jan-13 2:54am    
what else if I don't want to insert default value i.e., '01/01/1900'
Subrata.In 11-Jan-13 3:30am    
Fine, don't use default value. Just store Field=Null (Don't use quotation mark) id sql. When retrieving value just check for null, that's all.
Mayank Topiwala 11-Jan-13 3:49am    
ok and now what if I want to insert into dd/MM/yyyy format
Subrata.In 11-Jan-13 4:03am    
In a single word you can't insert date in a specific format. It will manage by server and also regional settings of client system. But you can show date as your require format, before show date just format it.
On C#: use Conver.ToDateTime(Field[""]).ToString("dd/MM/yyyy")
On sql: create user define function and/or using DateName function
Mayank Topiwala 11-Jan-13 4:14am    
cmd.Parameters.Add(new SqlParameter("PO_Date", Convert.ToDateTime(Convert.ToString(mDT_PoHead.Rows[0]["PO_Date"]) == "" ? null : Convert.ToString(mDT_PoHead.Rows[0]["PO_Date"]))));

is it correct ?
Hi,

Set parameter property
C#
boolIsNullable=true
when creating new sqlparameter.
 
Share this answer
 
try this:
C#
DateTime? dt = IIF((your_datetime_field == NULL)? NULL: Conver.ToDateTime(your_datetime_field))
 
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