DATETIME <NULL>

Hi,
Visual Basic 2003 Web App. This code fires from select column on datagrid. When fired the row from datagrid is copied to sql 2000 table. Everything was working until the field was left null. It's not an option to make the field required forcing the user to enter a date.


SqlConnection1.Open()
Dim rinsertCommand As SqlCommand = New SqlCommand
rinsertCommand.Connection = SqlConnection1
rsql = "insert into reschedulehistory(ReqShipDate)
"values (@ReqShipDate)
rinsertCommand.CommandText = rsql
rinsertCommand.Parameters.Add("@ReqShipDate", System.Data.SqlDbType.DateTime).Value = DateTime.Parse(colreqshipdate)
rinsertCommand.ExecuteNonQuery()
SqlConnection1.Close()

I get this error: The string was not recognized as a valid DateTime. There is a unknown word starting at index 1

I can paste all the code if you like.
Thanks,
Donnie

LVL 12
Donnie4572Asked:
Who is Participating?
 
eozz_2000Commented:
If the compiler say you that colreqshipdate isn't a type then you can try with this:

dim obj as Object
obj = colreqshipdate
If Not typeof(obj) is DBNull Then
    rinsertCommand.Parameters.Add("@ReqShipDate", System.Data.SqlDbType.DateTime).Value = DateTime.Parse(colreqshipdate)
Else
   rinsertCommand.Parameters.Add("@ReqShipDate", System.Data.SqlDbType.DateTime).Value = DBNull.Value
End If
0
 
eozz_2000Commented:
Why don't try with this:

If Not typeof(colreqshipdate) is DBNull Then
    rinsertCommand.Parameters.Add("@ReqShipDate", System.Data.SqlDbType.DateTime).Value = DateTime.Parse(colreqshipdate)
Else
   rinsertCommand.Parameters.Add("@ReqShipDate", System.Data.SqlDbType.DateTime).Value = DBNull.Value
End If
0
 
Donnie4572Author Commented:
Thanks for reply.

Sorry,
I can't get either of these to work. I get error:  String or binary data would be truncated. The statement has been terminated.

Any Ideas?
Thanks for your help.
0
 
Donnie4572Author Commented:
Your right. The second answer works! I had a second datetime/<NULL> field that that generated this error: String or binary data would be truncated. The statement has been terminated.
Thank You.
Donnie
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.