|
 |
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 .
|
 |
|
|
 |

April 8th, 2004, 02:27 PM
|
Registered User
|
|
Join Date: Apr 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

April 8th, 2004, 03:12 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Location: Central, NJ, USA.
Posts: 1,102
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
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
|

April 8th, 2004, 03:24 PM
|
Registered User
|
|
Join Date: Apr 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

April 8th, 2004, 03:56 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Location: Central, NJ, USA.
Posts: 1,102
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
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
|

April 8th, 2004, 04:16 PM
|
Registered User
|
|
Join Date: Apr 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|

April 8th, 2004, 04:19 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Location: Central, NJ, USA.
Posts: 1,102
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
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
|
Thread Tools |
Search this Thread |
|
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |