Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old September 6th, 2004, 03:55 AM
Authorized User
 
Join Date: Sep 2004
Location: Delhi, Delhi, India.
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default cast problem

I write following code

dim chk as string
Dim maxid as integer

cmdmax = New SqlCommand("select max(Reply_ID) from Reply where Q_ID='" & Request("Topic") & "' and Tech_ID='" & Request("Tech") & "' ", conPubs)

        chk = cmdmax.ExecuteScalar
        If (chk = "NULL") Then
            maxid = 0
        Else
            maxid = CInt("chk")
        End If
        maxid = maxid + 1

It gives the following error
"Cast from type 'DBNull' to type 'String' is not valid."
What is the problem.

vicky
__________________
vicky
  #2 (permalink)  
Old September 6th, 2004, 09:40 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

There are several things wrong with your code.

If no rows meet your selection criteria when the query executes, the database will return a NULL value. This is not the string "NULL", but rather a special (object) value represented by the constant system.dbnull.value.

The ExecuteScalar method returns a value of type object which you must cast to the appropriate type.

The CInt() function converts its argument to an integer. You are attempting to convert the string "chk" to an integer, which won't work and will generate an invalid cast exception.


Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
  #3 (permalink)  
Old September 8th, 2004, 12:08 AM
Authorized User
 
Join Date: Sep 2004
Location: Delhi, Delhi, India.
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Now what is the solution for this problem.

vicky
  #4 (permalink)  
Old September 8th, 2004, 01:03 AM
Friend of Wrox
 
Join Date: Oct 2003
Location: Chennai, TamilNadu, India.
Posts: 326
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Santhi Send a message via MSN to Santhi
Default

Typecast the return value of ExecuteScalar method to the appropriate type

  #5 (permalink)  
Old September 8th, 2004, 05:32 AM
Authorized User
 
Join Date: Sep 2004
Location: Delhi, Delhi, India.
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

But how to Typecast the returned value,is there any function for this.
pls solve this with a ex.

vicky
  #6 (permalink)  
Old September 8th, 2004, 06:16 AM
Friend of Wrox
 
Join Date: Oct 2003
Location: Chennai, TamilNadu, India.
Posts: 326
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Santhi Send a message via MSN to Santhi
Default

chk is string right so use CStr(cmdmax.ExecuteScalar)..
as u have done for integer conversion

  #7 (permalink)  
Old September 8th, 2004, 06:21 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by vickyj
 Now what is the solution for this problem.
If you cannot work out the "solution" on your own with the information that was given, you are going to have a lot of trouble down the line, since it seems there are many fundamental things about programming in VB.NET you don't appear to understand. You really shouldn't have to be given the answer.

But, I don't want to be accused of being unhelpful, so:
Code:
dim chk as object
...
chk = cmdmax.executescalar
If chk = system.dbnull.value Then
    maxid = 0
Else
    maxid = CInt(chk)
EndIf
...
The 'typecast' function in VB.NET is either CTYPE or DIRECTCAST depending on the situation. DIRECTCAST doesn't really apply here, so the type conversion function CTYPE could be used in place of the CInt, (they're equivalent) as:
Code:
    maxid = CType(chk, Integer)
Alternatively, you could also use the IsDBNull function to test for the NULL value, as:
Code:
If IsDBNull(chk) Then
    maxid = 0
Else
    maxid = CInt(chk)
EndIf
Good luck. You are probably going to need it.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com


Similar Threads
Thread Thread Starter Forum Replies Last Post
Specified cast is not valid. ayem Visual Studio 2008 1 September 8th, 2008 09:15 PM
Cast Problem in Web service proxy Amit_Topper Crystal Reports 0 January 20th, 2007 03:28 AM
Another "Specified Cast Not Valid" Problem radiohalo5 BOOK: ASP.NET Website Programming Problem-Design-Solution 0 February 15th, 2006 01:50 PM
Specified cast is not valid TheMole BOOK: ASP.NET Website Programming Problem-Design-Solution 9 November 1st, 2004 05:09 PM
problem in cast of type yoord SQL Language 1 October 18th, 2004 03:19 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.