CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Threaded View

  1. #1
    Join Date
    Dec 2009
    Posts
    596

    [RESOLVED] Need Help with Handling Null

    Hello everybody. In an insert routine I get a "FormatException was unhandled" - "Failed to convert parameter value from a String to a DateTime." error when attempting to insert a record. The error points to myCommand.ExecuteNonQuery(). In the record insert attempt I put values in txtFirstName and txtLastName and purposely leave the rest of the fields blank. In the table FirstName and LastName must have values. For the rest of the columns NULL is allowed. How do you guys and gals handle this situation in a graceful way? I haven't coded any validation yet. I don't want to insert '0' or 'n/a' or anything like that in the table where NULLs are permitted. Would someone please share their insight?

    Code:
    Dim myCommand As SqlCommand
    myCommand = New SqlCommand
    myCommand.Connection = CN
    myCommand.CommandText = "sInsertGuide"
    myCommand.CommandType = CommandType.StoredProcedure
    myCommand.Parameters.Add("@FirstName", SqlDbType.VarChar, 50, ParameterDirection.Output).Value = Me.TxtFirstName.Text
            myCommand.Parameters.Add("@LastName", SqlDbType.VarChar, 50, ParameterDirection.Output).Value = Me.TxtLastName.Text
            myCommand.Parameters.Add("@Qualification", SqlDbType.VarChar, 2048, ParameterDirection.Output).Value = Me.TxtQuailfication.Text
            myCommand.Parameters.Add("@DateOfBirth", SqlDbType.DateTime, 10, ParameterDirection.Output).Value = Me.TxtDateOfBirth.Text
            myCommand.Parameters.Add("@DateOfHire", SqlDbType.DateTime, 10, ParameterDirection.Output).Value = Me.TxtDateOfHire.Text
    
    myCommand.ExecuteNonQuery()
    Last edited by HanneSThEGreaT; March 17th, 2010 at 11:00 AM. Reason: Added [CODE] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured