Insert Null Value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RN1

    Insert Null Value

    How do I insert NULL values in a SQL Server DB table in a column whose
    datatype is int? This is what I am trying currently (in the
    UpdateCommand event function of a DataGrid):

    ----------------------------
    Dim strSQL As String
    Dim objErrorPercent age As Object

    If (CType(ea.Item. Cells(8).Contro ls(0), TextBox).Text = "") Then
    objErrorPercent age = DBNull.Value
    Else
    objErrorPercent age = CType(ea.Item.C ells(8).Control s(0),
    TextBox).Text
    End If

    strSQL = "UPDATE Table1 SET.......Error Perc = " & objErrorPercent age &
    ".....WHERE.... ."
    ----------------------------

    But the above generates the "Incoorrect syntax" error if the TextBox
    is empty.
  • Alexey Smirnov

    #2
    Re: Insert Null Value

    On Oct 12, 2:28 pm, RN1 <r...@rediffmai l.comwrote:
    How do I insert NULL values in a SQL Server DB table in a column whose
    datatype is int? This is what I am trying currently (in the
    UpdateCommand event function of a DataGrid):
    >
    ----------------------------
    Dim strSQL As String
    Dim objErrorPercent age As Object
    >
    If (CType(ea.Item. Cells(8).Contro ls(0), TextBox).Text = "") Then
        objErrorPercent age = DBNull.Value
    Else
        objErrorPercent age = CType(ea.Item.C ells(8).Control s(0),
    TextBox).Text
    End If
    >
    strSQL = "UPDATE Table1 SET.......Error Perc = " & objErrorPercent age &
    ".....WHERE.... ."
    ----------------------------
    >
    But the above generates the "Incoorrect syntax" error if the TextBox
    is empty.
    1. Be careful with the sql injections.

    2. If (CType(ea.Item. Cells(8).Contro ls(0), TextBox).Text = "") Then
    objErrorPercent age = "NULL"

    Comment

    • Riki

      #3
      Re: Insert Null Value

      RN1 wrote:
      How do I insert NULL values in a SQL Server DB table in a column whose
      datatype is int? This is what I am trying currently (in the
      UpdateCommand event function of a DataGrid):
      >
      ----------------------------
      Dim strSQL As String
      Dim objErrorPercent age As Object
      >
      If (CType(ea.Item. Cells(8).Contro ls(0), TextBox).Text = "") Then
      objErrorPercent age = DBNull.Value
      Else
      objErrorPercent age = CType(ea.Item.C ells(8).Control s(0),
      TextBox).Text
      End If
      >
      strSQL = "UPDATE Table1 SET.......Error Perc = " & objErrorPercent age &
      ".....WHERE.... ."
      ----------------------------
      >
      But the above generates the "Incoorrect syntax" error if the TextBox
      is empty.
      1) See Alexey's response
      2) Use parameters instead of string concatenation:
      strSQL = "UPDATE Table1 SET.......Error Perc =
      @ParamErrorPerc .....WHERE..... "
      and
      myCommand.Param eters.AddValue( "ParamErrorPerc ", objErrorPercent age)

      --

      Riki


      Comment

      Working...