[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 139
  • Last Modified:

VB Sql

Hi
Hope this is enough information

VB.net
SQL Server 2008

Code -
parameters = new DataParameterCollection
paramerers.add("@test", cStr(Values(2)))

Produces an DBNull to string not valid error
How do I test cStr(Values(2) is Null set the Parameter to ""
or if not Null paramerers.add("@test", cStr(Values(2)))

Thanks
0
Kevinfeening
Asked:
Kevinfeening
  • 2
  • 2
1 Solution
 
NARANTHIRANCommented:
Check whether @test returns the value.

Your trying to convert the null value into string that's why it returns  "DBNull to string not valid" error..
0
 
KevinfeeningAuthor Commented:
Thanks
as I asked how do I as you commented
Check whether @test returns the value
0
 
CodeCruiserCommented:
Change

paramerers.add("@test", cStr(Values(2)))

to

If Not String.IsNullOrEmpty(Values(2)) AND Not ISDBNull(Values(2)) Then
    paramerers.add("@test", cStr(Values(2)))
Else
    paramerers.add("@test", "")
End If
0
 
KevinfeeningAuthor Commented:
Thanks but it causes errors

On the If Not String.IsNullOrEmpty(Values(2))  Error -
Option strict on disallows implicit conversion from object to string

and Name isdbNull is not declared

Had to Change  Value(2) to cstr(Value(2)) in the first line to remove object to string error and remove the ISDBNull but didn't work

Thanks
0
 
CodeCruiserCommented:
Ok. Try changing

If Not String.IsNullOrEmpty(Values(2)) AND Not ISDBNull(Values(2)) Then

to

If Not Convert.ISDBNull(Values(2)) Then
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now