944,123 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 6311
  • ASP.NET RSS
Apr 4th, 2007
0

Cast from type 'DBNull" to type 'String' is not valid

Expand Post »
hi..
my system should select the maximum QuestionNum from one of the my table in the database. after all, i would like to increment the value QuestionNum to 1...
but i keep on receiving error
"Cast from type 'DBNull' to type 'String' is not valid."

this is my code...

Public Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim MyConnection As SqlConnection
MyConnection = New SqlConnection("server=localhost;database=xxxxxxx;Trusted_Connection=yes")
MyConnection.Open()
Dim sql As String
//Select the max QuestionNum where SurveyID=SurveyID
sql = "SELECT MAX(QuestionNum) FROM Question WHERE SurveyID='" & Session("sSurveyID") & "'"
Dim cmd As New SqlCommand(sql, MyConnection)
Dim QuestionNum As Integer
Dim id As String
id = cmd.ExecuteScalar
QuestionNum = id
MyConnection.Close()
End If
End Sub


p/s: can anyone explain what is wrong here?
Reputation Points: 10
Solved Threads: 0
Light Poster
shy_wani is offline Offline
34 posts
since Mar 2007
Apr 4th, 2007
0

Re: Cast from type 'DBNull" to type 'String' is not valid

Hi,

What's hapenning is that the SurveyID which you are passing in Session variable does not have any Questions as yet in the table and so Null is returned.

Please change Max(QuestionNum) to Isnull(Max(QuestionNum),0)

as below:

ASP.NET Syntax (Toggle Plain Text)
  1. //Select the max QuestionNum where SurveyID=SurveyID
  2. sql = "SELECT isnull(MAX(QuestionNum),0) FROM Question WHERE SurveyID='" & Session("sSurveyID") & "'"

Regards,

Nitin
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nitinmms is offline Offline
2 posts
since Mar 2007
Apr 5th, 2007
0

Re: Cast from type 'DBNull" to type 'String' is not valid

owh..thanks nitinmms

now i understand...this problem has been bugging me since last week..haha..

thanks again yer...
take care
Reputation Points: 10
Solved Threads: 0
Light Poster
shy_wani is offline Offline
34 posts
since Mar 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Run messenger on button click
Next Thread in ASP.NET Forum Timeline: generate drop down list..checkboxes...listbox...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC