Click here to Skip to main content
15,917,565 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
I make gridview cell as a date time picker through progammicaly
now i want to save the grid view cell date in SQl . But i'm facing an error
please help me how can i solve it thousand time i search it from google but not get any solution
I'm getting this error message
"
System.Data.SqlTypes.SqlTypeException: 'SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.'

"

What I have tried:

 foreach (DataGridViewRow row in StockdataGridView.Rows)
             {
             using (SqlCommand cmd = new SqlCommand("USP_Insert_Stock_Info", conn1))
{
  cmd.CommandType = CommandType.StoredProcedure;
 DateTime expdate = Convert.ToDateTime(row.Cells[5].Value );
                            cmd.Parameters.AddWithValue("@ExpDate", expdate);
}     
        } {
Posted
Updated 21-Dec-18 21:28pm
v2

1 solution

I think you need to place a breakpoint at the line:
DateTime expdate = Convert.ToDateTime(row.Cells[5].Value );
and look what expdate looks like by hovering over it with the mouse, the format is probably not correct for your stored procedure.
You should also check if row.Cells[5].Value is not NULL first.

It might also be needed to explicity specify the SqlDbType, like this:
command.Parameters.Add(new SqlParameter("@ExpDate", SqlDbType.DateTime2) { Scale = 3, Value = expdate });
More info here: AddWithValue is Evil | Dan Guzman's Blog[^]

See example here for DateTimePicker usage: How to: Host Controls in Windows Forms DataGridView Cells | Microsoft Docs[^]
 
Share this answer
 
v4
Comments
Fahid Zahoor 22-Dec-18 3:45am    
when i hover the mouse on "expdate"
the value is {1/1/0001 12:00:00 AM}
but i cannot select this value i set today date .
now how can i refine it
Fahid Zahoor 22-Dec-18 3:49am    
ohhh i checked it
if row.Cells[5].Value is not NULL.
it show null value but i set today date
RickZeeland 22-Dec-18 3:55am    
Looks like you are on the right track now :) I will also update the solution with an interesting link about SqlDbType.
Fahid Zahoor 22-Dec-18 3:57am    
but please tell me how can i correct it really worried
Fahid Zahoor 22-Dec-18 4:07am    
I used it but now this error occur
System.InvalidCastException: 'Failed to convert parameter value from a DateTimePicker to a DateTime.'

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