Insert Null Value

  • Thread starter Thread starter RN1
  • Start date Start date
R

RN1

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 objErrorPercentage As Object

If (CType(ea.Item.Cells(8).Controls(0), TextBox).Text = "") Then
objErrorPercentage = DBNull.Value
Else
objErrorPercentage = CType(ea.Item.Cells(8).Controls(0),
TextBox).Text
End If

strSQL = "UPDATE Table1 SET.......ErrorPerc = " & objErrorPercentage &
".....WHERE....."
 
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 objErrorPercentage As Object

If (CType(ea.Item.Cells(8).Controls(0), TextBox).Text = "") Then
    objErrorPercentage = DBNull.Value
Else
    objErrorPercentage = CType(ea.Item.Cells(8).Controls(0),
TextBox).Text
End If

strSQL = "UPDATE Table1 SET.......ErrorPerc = " & objErrorPercentage &
".....WHERE....."

1. Be careful with the sql injections.

2. If (CType(ea.Item.Cells(8).Controls(0), TextBox).Text = "") Then
objErrorPercentage = "NULL"
 
RN1 said:
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 objErrorPercentage As Object

If (CType(ea.Item.Cells(8).Controls(0), TextBox).Text = "") Then
objErrorPercentage = DBNull.Value
Else
objErrorPercentage = CType(ea.Item.Cells(8).Controls(0),
TextBox).Text
End If

strSQL = "UPDATE Table1 SET.......ErrorPerc = " & objErrorPercentage &
".....WHERE....."

1) See Alexey's response
2) Use parameters instead of string concatenation:
strSQL = "UPDATE Table1 SET.......ErrorPerc =
@ParamErrorPerc.....WHERE....."
and
myCommand.Parameters.AddValue("ParamErrorPerc", objErrorPercentage)
 
Back
Top