0

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?

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by shy_wani
0

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:

//Select the max QuestionNum where SurveyID=SurveyID
            sql = "SELECT isnull(MAX(QuestionNum),0) FROM Question WHERE SurveyID='" & Session("sSurveyID") & "'"

Regards,

Nitin

0

owh..thanks nitinmms

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

thanks again yer...
take care

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.