0

Redesign of webpage problem - I have a field in my SQL database called Approved. This field was either true for approved, or false for not approved. Now, the user would like to track denied. So, it's approved - true, not approved - false, or not decided -null.

I found out that SQL database bit field can also be null. So, it can keep the values true, false, or null. In SQL, I tested it, and the field can be null.

My problem - on my webpage, I'm added a record to this table (not even mentioning that field) and it automatically enters that field as it as false. I have tried to add null to that field, but it still goes into sql as false. Is there a way to do this with a bit field or do I have to create a new field (please say there's a way to do this)? Here's my code for trying to add the field as null:

Using connection As New SqlConnection("Data Source=WillSQL\ict2;Initial Catalog=TimeSQL_testing;Integrated Security=SSPI ")

                commandText = "Insert into tblWorkHours(Employee, EmployeeName, DateRequested, WorkCode, BeginDateOff, EndDateOff, AllDay_YesNo, approved, sys_usr, stampaddtime) values (@Employee,@EmployeeName,@DateRequested,@WorkCode,@BeginDateOff,@EndDateOff,@AllDay_YesNo, @approved, @sys_user, @stampaddtime)"

                Dim cmd As New SqlCommand(commandText, connection)

                connection.Open()

                cmd.Parameters.AddWithValue("@Employee", stempid)
                cmd.Parameters.AddWithValue("@EmployeeName", stempname)
                cmd.Parameters.AddWithValue("@DateRequested", stdtreq)
                cmd.Parameters.AddWithValue("@WorkCode", streason)
                cmd.Parameters.AddWithValue("@BeginDateOff", stdtbeg)
                cmd.Parameters.AddWithValue("@EndDateOff", stdtend)
                cmd.Parameters.AddWithValue("@AllDay_YesNo", stallday)
                cmd.Parameters.AddWithValue("@approved", vbNull)
                cmd.Parameters.AddWithValue("@sys_user", vname)
                cmd.Parameters.AddWithValue("@stampaddtime", dt)

                cmd.ExecuteNonQuery()

                connection.Close()
            End Using

Thanks!

user3033348
  • 145
  • 13

1 Answers1

1

To set a database value to NULL from .net, you need to use dbnull.value:

So change this:

cmd.Parameters.AddWithValue("@approved", vbNull)

To this:

cmd.Parameters.AddWithValue("@approved", DbNull.Value)
Tab Alleman
  • 31,483
  • 7
  • 36
  • 52