开发者

How to prevent crashes when parameter is null

开发者 https://www.devze.com 2022-12-21 00:23 出处:网络
i am inserting the value of dynamic controls into the db like so; Dim ae1 As DropDownList = FindControl(\"AreasExpertise1\")

i am inserting the value of dynamic controls into the db like so;

Dim ae1 As DropDownList = FindControl("AreasExpertise1")
        If (ae1 IsNot Nothing) Then
            x.Parameters.AddWithValue("@areasexpertise1", ae1.SelectedValue)
        End If

then in my query i have parameters like so;

INSERT INTO [foo] ([etc], [etc], [etc], [areasexpertise1], [areasexpertise2])
VALUES (@etc, @etc, @etc, @areasexpertise1, @areasexpertise2)

When the user fills out the form, they see only areasexpertise1 and have to press a button to dynamically insert another area of expertise...

If they only have 1 area of expertise, the query crashes and says 'must declare variable @areasexpertise2.

Please can you tell me a way to 开发者_如何学运维either insert nulls when the parameters are empty, or to ignore them?

Thanks a lot


Try passing in DBNull.Value, like this:

If (ae1 IsNot Nothing) Then
    x.Parameters.AddWithValue("@areasexpertise1", ae1.SelectedValue)
Else
   x.Parameters.AddWithValue("@areasexpertise1", DBNull.Value)
End If
0

精彩评论

暂无评论...
验证码 换一张
取 消