Solved

DATETIME <NULL>

Posted on 2004-10-25
505 Views
Last Modified: 2012-06-27
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

0
Question by:Donnie4572
    4 Comments
     
    LVL 4

    Expert Comment

    by:eozz_2000
    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
     
    LVL 4

    Accepted Solution

    by:
    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
     
    LVL 12

    Author Comment

    by:Donnie4572
    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
     
    LVL 12

    Author Comment

    by:Donnie4572
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    Want to pick and choose which updates you receive? Feel free to check out this quick video on how to manage your email notifications.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    934 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now