Handling NULL field in SQL server with asp.net

  • Thread starter Thread starter sanju
  • Start date Start date
S

sanju

hi guyz

i want to print user friendly message to html page if there is no record in
database. i tried this code ..its working if there is no record.. but record
exists then i am getting error..
System.MissingMemberException: Public member 'Value' on type 'String' not
found.

if Not IsDBNull(dgleads("industrytypeid").Value) Then
indtype=dgleads("industrytypeid").Value
else
indtype="Unknown"
end if

plz help me
 
Hi Sanju,

Does this sample do the job for you?

Dim cmd As SqlCommand
Dim sqlstring As String = "select count(*) from tbl"
cmd = New SqlCommand(sqlstring, conn)
cmd.Connection.Open()
Dim count As Integer = CInt(cmd.ExecuteScalar())
conn.Close()

I hope this helps a little bit?

Cor
 
1) Turn Option Strict On. This will elminate these kinds of problems at
compile time, because your program will not compile. It also prevents lazy
coding and forces you to use types as they were meant to.
2) You need to use Not IsDBNull(dgleads("industrytypeid")) for your
condition. You are trying to get the Value property - but that is only there
when the value is NULL. For real values - you are getting a string, so
Value is not a property on the string class - hence the error.
 
To check for any values:

If (dataReader.HasRows) Then
'WOrk here
Else
'Display message
End If

For dataset, change first line
If(dataSet.Tables[0].Rows.Count <> 0) Then

For a particular column, strongly typed datasets give you IsNull methods,
which are easier to use than non-strongly typed DataSets.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
 
Back
Top