locked
How to allow null value inside datetime RRS feed

  • Question

  • User66371569 posted

    I have textbox     for date 

    I am using this DateTime.ParseExact(Startdate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture)  to  insert date   its works  fine  but sometimes I need to insert null or empty

    I got error  

    how can I solve tis

    Thursday, September 27, 2018 10:17 AM

Answers

  • User66371569 posted
    thank u so much It worked
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 29, 2018 3:49 PM

All replies

  • User-1716253493 posted

    If empty pass dbnull.value

    Thursday, September 27, 2018 11:22 AM
  • User66371569 posted
    I am using this code inside insert method

    Insert into table (startdate) values (DateTime.ParseExact(Startdate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture))


    how to apply null value in above code
    Thursday, September 27, 2018 12:35 PM
  • User-1716253493 posted

    Use parameterize query

    Insert into table (startdate) values (@startdate)

    you can pass dbnull.value when textbox text is empty

    or change the query

    Insert into table (startdate) values (null)

    Thursday, September 27, 2018 12:41 PM
  • User66371569 posted
    u mean like that

    Dim sdate as string= startdate.text
    If sdate=' ' then
     dbnull.value
    Else
    Dim Ddate as Datetime=DateTime.ParseExact(sdate, "dd/MM/yyyy", CultureInfo.InvariantCulture)

    insert into table startdate values (sdate)
    Thursday, September 27, 2018 12:52 PM
  • User-1716253493 posted
            cmd.CommandText = "Insert into table (startdate) values (@startdate)"
            If sdate = Nothing Then
                cmd.Parameters.AddWithValue("@startdate", DBNull.Value)
            Else
                cmd.Parameters.AddWithValue("@startdate", DateTime.ParseExact(sdate, "dd/MM/yyyy", CultureInfo.InvariantCulture))
            End If

    Friday, September 28, 2018 12:59 AM
  • User66371569 posted

    Dim ds1 As New SqlClient.SqlCommand
    'Dim intcount1 As String
    Dim str As String

    conTest.Open()

    str = " insert into HpdCoboneRequests (cobonerequestid,coboneno,StartDate,Enddate,coboneid,operationid) values('" & cobrequest.Text & "',N'" & cobno.Text & "','" & "@startdate" & "','" & "@startdate" & "',N'" & cobid.SelectedValue.ToString & "',N'" & Session("op").ToString & "')"


    Dim ds As New SqlClient.SqlCommand(str, conTest)
    Dim s As Integer

    Dim sdate As String = Startdate.Text
    If sdate = Nothing Then
    ds.Parameters.AddWithValue("@startdate", DBNull.Value)
    Else
    ds.Parameters.AddWithValue("@startdate", DateTime.ParseExact(sdate, "dd/MM/yyyy", CultureInfo.InvariantCulture))
    End If


    s = ds.ExecuteNonQuery()

    when run code   and insert null value  or also  right date       both occurs error "Conversion failed when converting date and/or time from character string" 

    Saturday, September 29, 2018 5:59 AM
  • User66371569 posted
    thank u so much It worked
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 29, 2018 3:49 PM