Insert null values into database vb.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ayakamacy
    New Member
    • May 2015
    • 14

    Insert null values into database vb.net

    Hello all, i was wondering how should i insert null values into a database. I have textbox1 and the textbox2 will convert the value out of textbox1, when i the time i click save and the textbox1 is empty i got an error, "Conversion from string "" to type 'Date' is not valid." i just want to insert null value if the textbox is empty.
  • computerfox
    Contributor
    • Mar 2010
    • 276

    #2
    What the data type in the database?
    Can you update the column to default null?
    Are you doing any verification in your code?

    By the looks of it, the column currently is set to a date type, is this correct?

    Comment

    • ayakamacy
      New Member
      • May 2015
      • 14

      #3
      i only used varchar(MAX)
      as I've said above, when the time i left txtShipoutDateA ctual.Text empty, i got an error. "Conversion from string "" to type 'Date' is not valid."
      because all the input value of txtShipoutDateA ctual.Text i convert it into Dayvalue.
      here is my code:
      Code:
      Dim dateValueSDA As Date
      dateValueSDA = txtShipoutDateActual.Text
      lblDaysda.Text = WeekdayName(Weekday(dateValueSDA))
      How can i code that into a conditional statement if txtShipoutDateA ctual.Text is null value, and lblDaysda.Text should also null value to save in database.

      Comment

      • ayakamacy
        New Member
        • May 2015
        • 14

        #4
        Best answer

        I knew the answer for this problem.
        for those people who will encounter this problem

        Try this sample code, and its working =)

        Code:
        Dim cmd As New SqlCommand("SELECT * FROM tblJOL", SQLcon)
        cmd.Parameters.Add(New SqlParameter("@DRsda", SqlDbType.VarChar))
        If (txtShipoutDateActual.Text = "") Then
           cmd.Parameters("@DRsda").Value = DBNull.Value
        Else
           Dim dateValueSDA As Date
           dateValueSDA = txtShipoutDateActual.Text
           lblDaysda.Text = WeekdayName(Weekday(dateValueSDA))
        End If

        Comment

        Working...