| | |
Cast from type 'DBNull" to type 'String' is not valid
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2007
Posts: 34
Reputation:
Solved Threads: 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?
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?
•
•
Join Date: Mar 2007
Posts: 2
Reputation:
Solved Threads: 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:
Regards,
Nitin
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)
//Select the max QuestionNum where SurveyID=SurveyID sql = "SELECT isnull(MAX(QuestionNum),0) FROM Question WHERE SurveyID='" & Session("sSurveyID") & "'"
Regards,
Nitin
![]() |
Similar Threads
- Data validation- want only Int's (C++)
- finding out variable type from html input type tag in javascsript (HTML and CSS)
- how to cast a string type variable to LPCTSTR type (C++)
- Cast from string "Display" to type "Interger" is not valid (VB.NET)
- Element is undefined in a Java object of type class [Ljava.lang.String; referenced as (ColdFusion)
- Doc. type, CSS and IE's "expression." - ARRRGGH! (HTML and CSS)
Other Threads in the ASP.NET Forum
- Previous Thread: Run messenger on button click
- Next Thread: generate drop down list..checkboxes...listbox...
| Thread Tools | Search this Thread |
.net 2.0 3.5 ajax appliances application asp asp.net beginner box browser businesslogiclayer button c# c#gridviewcolumn cac checkbox child class compatible confirmationcodegeneration content contenttype control countryselector courier css database datagrid datagridview datalist deadlock deployment development dgv dialog dropdownmenu dynamic dynamically edit embeddingactivexcontrol feedback fileuploader fill findcontrol flash flv form forms grid gridview gudi homeedition hosting iis image javascript jquery list menu mssql multistepregistration nameisnotdeclared novell objects opera order problem ratings redirect registration relationaldatabases rotatepage search security select serializesmo.table sessionvariables silverlight smoobjects sql ssl tracking treeview typeof validatedate validation vb.net virtualdirectory vista visual-studio visualstudio vs2008 web webapplications webarchitecture webdevelopment wizard xml xsl





