如何插入NULL到数据库中,如果表单字段使用ASP.NET和放大器是空的; VB [英] How to insert NULL into database if form field is empty using ASP.NET & VB
问题描述
我有一个从形式插入数据的格式,并存储过程。它工作正常,但如果一个字段没有填写不插入 NULL
到SQL它插入,
。
I have a form and stored procedure that inserts the data from the form. It works fine except that if a field isn't filled in it doesn't insert a NULL
into SQL it inserts ""
.
我已经尝试了几种不同的方法,但似乎没有要插入 NULL
,下面还是插入一个,
,任何人都可以点我在正确的方向?
I've tried a few different ways but none seem to insert NULL
, the one below still inserts ""
, can anyone point me in the right direction?
下面是code的必需部分,如果你需要更多的只是让我知道。
Here is the required part of the code, if you require more just let me know.
Dim rdr As SqlDataReader
Dim cmdInsert As SqlCommand = New SqlCommand()
cmdInsert.CommandText = "spPersonalDetailsInsert"
cmdInsert.CommandType = CommandType.StoredProcedure
cmdInsert.Connection = connSQL
Dim firstname, lastname, address, address1, town, county, postcode As SqlParameter
'convert to null if ""
Dim frmFirstName As String
If pd_first_name.Text = "" Then
frmFirstName = Convert.DBNull
Else
frmFirstName = pd_first_name.Text
End If
firstname = New SqlParameter()
firstname.ParameterName = "@firstname"
firstname.SqlDbType = SqlDbType.NVarChar
firstname.Size = 50
firstname.Direction = ParameterDirection.Input
firstname.Value = frmFirstName
修改
我测试了以下code:
I tested the following code:
If pd_first_name.Text = "" Then
frmFirstName = DBNull.Value
Else
frmFirstName = pd_first_name.Text
End If
但它仍然不会插入 NULL
所以我测试的:
If pd_first_name.Text = "" Then
Response.Write("NULL")
address1.Value = DBNull.Value
Else
Response.Write("NOT NULL")
address1.Value = pd_address1.Text
End If
所以,如果我输入什么到地址1
字段应该写 NULL
屏幕,但它始终写入 NOT NULL
。这是什么一个空的表单字段平等的吗?在传统的ASP它总是,
。
So if I enter nothing into address1
field it should write NULL
to screen but it always writes NOT NULL
. What does an empty form field equal? in classic ASP it was always ""
.
推荐答案
您需要使用的 DBNull.Value
If String.IsNullOrEmpty(pd_first_name.Text.ToString().Trim) = true Then
frmFirstName = DBNull.Value
Else
frmFirstName = pd_first_name.Text
End If
这篇关于如何插入NULL到数据库中,如果表单字段使用ASP.NET和放大器是空的; VB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!