p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   VB.NET 2002/2003 Basics (http://p2p.wrox.com/forumdisplay.php?f=76)
-   -   Problem converting type (http://p2p.wrox.com/showthread.php?t=11612)

gylim78 April 8th, 2004 02:27 PM

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


Hal Levy April 8th, 2004 03:12 PM

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

gylim78 April 8th, 2004 03:24 PM

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



Hal Levy April 8th, 2004 03:56 PM

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

gylim78 April 8th, 2004 04:16 PM

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.


Hal Levy April 8th, 2004 04:19 PM

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


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

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