Insert Null Value

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....."
 
G

Guest

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"
 
R

Riki

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top