Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 8th, 2004, 02:27 PM
Registered User
 
Join Date: Apr 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem converting type

Hi all,

   Got a very simple task to do, but I have not been able to get it done. It is driving me crazy !!!

   I am doing a invoice sw. I need to pull out the last invoice number(ie the largest number) from the db, and add 1 to it to generate the next invoice number. So far, I have had no problem retriving the last invoice number from the db into a dataset. But when I try to add 1 to it ... it gives me the error "System.InvalidCastException: Cast from type 'DBNull' to type 'Integer' is not valid."

    My code is as follows :
    dim tmp_inv_no2 as integer
    ds = SelectRows(ds,inv_conn_str,mysql,"inv")
    tmp_inv_no2 = ds.tables("inv").compute("MAX(inv_no)","")
    inv_no.text = tmp_inv_no2

But when I changed my code to the following ...
    dim tmp_inv_no2 as string
    ds = SelectRows(ds,inv_conn_str,mysql,"inv")
    tmp_inv_no2 = ds.tables("inv").compute("MAX(inv_no)","").tostrin g()
    inv_no.text = tmp_inv_no2

I can display the last invoice number with no problem !! Therefore, it is not a NULL value.

I next tried to convert the string to a integer by using the following code ...

    dim tmp_inv_no2 as string
    dim tmp_inv_no3 as integer
    ds = SelectRows(ds,inv_conn_str,mysql,"inv")
    tmp_inv_no2 = ds.tables("inv").compute("MAX(inv_no)","").tostrin g()
    tmp_inv_no3 = cint(tmp_inv_no2)

I get an error message
System.FormatException: Input string was not in a correct format.

Please help

Reply With Quote
  #2 (permalink)  
Old April 8th, 2004, 03:12 PM
Friend of Wrox
Points: 3,489, Level: 24
Points: 3,489, Level: 24 Points: 3,489, Level: 24 Points: 3,489, Level: 24
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Central, NJ, USA.
Posts: 1,102
Thanks: 0
Thanked 2 Times in 2 Posts
Default

What type is the inv_no in the database? Can you type out an example of your invoice number?



Hal Levy
Web Developer, PDI Inc.

NOT a Wiley/Wrox Employee
Reply With Quote
  #3 (permalink)  
Old April 8th, 2004, 03:24 PM
Registered User
 
Join Date: Apr 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Thanks for the reply ...

my inv_no is just a simple running integer number

Example:
if the previous number was 1000, I want to generate 1001

I am using a MYSQL database.
The variable type in the database is of type int.

Regards


Reply With Quote
  #4 (permalink)  
Old April 8th, 2004, 03:56 PM
Friend of Wrox
Points: 3,489, Level: 24
Points: 3,489, Level: 24 Points: 3,489, Level: 24 Points: 3,489, Level: 24
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Central, NJ, USA.
Posts: 1,102
Thanks: 0
Thanked 2 Times in 2 Posts
Default

So why are you converting it to a string and then converting it back?

Hal Levy
Web Developer, PDI Inc.

NOT a Wiley/Wrox Employee
Reply With Quote
  #5 (permalink)  
Old April 8th, 2004, 04:16 PM
Registered User
 
Join Date: Apr 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

My original code was like this ...

   dim tmp_inv_no2 as integer
   ds = SelectRows(ds,inv_conn_str,mysql,"inv")
   tmp_inv_no2 = ds.tables("inv").compute("MAX(inv_no)","")
   tmp_inv_no2 += 1

But I would get an error saying ...
"System.InvalidCastException: Cast from type 'DBNull' to type 'Integer' is not valid."

In order to find out what was being passed back by ds.tables("inv").compute("MAX(inv_no)",""), I converted the output to a string so that I may view the value. I found that the value returned was indeed the maximum value of inv_no and not a NULL value.

My problem now is that VB.NET is treating the value of ds.tables("inv").compute("MAX(inv_no)","") of a type DBNull, thus I cannot add 1 to it.

Reply With Quote
  #6 (permalink)  
Old April 8th, 2004, 04:19 PM
Friend of Wrox
Points: 3,489, Level: 24
Points: 3,489, Level: 24 Points: 3,489, Level: 24 Points: 3,489, Level: 24
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Central, NJ, USA.
Posts: 1,102
Thanks: 0
Thanked 2 Times in 2 Posts
Default

I'd be interested in what tmp_inv_no2 is in this example- rather than .tostring do you have a test harness? what's it look like if you just bind to tmp_inv_no2

Hal Levy
Web Developer, PDI Inc.

NOT a Wiley/Wrox Employee
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Any One Help me Type Converting yoord BOOK: Beginning ASP.NET 1.0 0 September 21st, 2004 04:17 AM
Error converting data type varchar to float. Trojan_uk SQL Server ASP 5 April 27th, 2004 02:56 AM
Converting user input - SmallMoney type saturdave ASP.NET 1.0 and 1.1 Basics 1 April 8th, 2004 08:16 PM
Converting int64 data type in VB6 yeeck VB How-To 1 December 9th, 2003 10:13 PM
Converting int64 data type in VB6 yeeck Beginning VB 6 0 November 27th, 2003 11:32 PM



All times are GMT -4. The time now is 09:08 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.