Basic4ppc - Windows Mobile Development  

Go Back   Basic4ppc - Windows Mobile Development > Main Category > Questions & Help Needed
Home Register FAQ Members List Search Today's Posts Mark Forums Read

Questions & Help Needed Post any question regarding Basic4ppc.


Specified cast is not valid


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-10-2008, 05:57 PM
Junior Member
 
Join Date: Dec 2007
Posts: 40
Default Specified cast is not valid

Hello all,
reading a database table with the SQL executeTable command and then accessing each single cell with

myVar=Table1.Cell(Table1.ColName(k),0)

I get the "Specified cast is not valid" error message if the cell contains a "null" value deriving from an empty field in the database.

How to avoid this message? Only providing a default value to the field? or is there any other solution?

Many thanks
Mimmo
Reply With Quote
  #2 (permalink)  
Old 03-10-2008, 06:02 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 3,199
Default

You can use IsNull to first check if the value is null.
Code:
If IsNull(Table1.Cell(Table1.ColName(k),0)) Then myVar = 0 else myVar = Table1.Cell(Table1.ColName(k),0)
Reply With Quote
  #3 (permalink)  
Old 03-10-2008, 10:12 PM
Junior Member
 
Join Date: Dec 2007
Posts: 40
Unhappy

Hello Erel,
thanks for your reply but, I'm sorry, I forgot to mention that I already tried with IsNull and the error is the same. I splitted the IF statement just to be sure about the error line. Running the program, it stops at line with IF

if IsNull(table1.cell(table1.Colname(k),0)) then
..
else
..
end if

and raises the same error.

Mimmo
Reply With Quote
  #4 (permalink)  
Old 03-11-2008, 06:36 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 3,199
Default

Can you upload a small example (with part of the database)?
Reply With Quote
  #5 (permalink)  
Old 03-11-2008, 10:18 AM
Junior Member
 
Join Date: Dec 2007
Posts: 40
Default

Hi Erel,
please find attached an excerpt of what I was doing. Please don't care about the logic inside the FOR cycle. It has been semplified just to help in isolating the problem.

The strange thing is that the 3rd fields is also "null" (as you can see from the table component) but does not raise any error.

Many thanks for the time you will dedicate me.
Mimmo
Attached Files
File Type: zip Certificati.zip (2.7 KB, 5 views)
Reply With Quote
  #6 (permalink)  
Old 03-11-2008, 10:55 AM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 1,770
Awards Showcase
Beta Tester Forum Contributer 
Total Awards: 2
Default

Quote:
Originally Posted by skipper View Post
The strange thing is that the 3rd fields is also "null" (as you can see from the table component) but does not raise any error.
It does if you optimise compile it "Unable to cast object of type 'System.DBNull to type 'System.String'" but not if you legacy compile or run in the IDE.
Reply With Quote
  #7 (permalink)  
Old 03-11-2008, 01:13 PM
Junior Member
 
Join Date: Dec 2007
Posts: 40
Default

thanks Agraham,
I didn't notice that because I was testing in the IDE....
Reply With Quote
  #8 (permalink)  
Old 03-12-2008, 08:17 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 3,199
Default

When you read a value from a table that value must not be null.
You can use the COALESCE keyword which replaces NULLs with a default value.

Code:
SqlCommand.CommandText="Select COALESCE(cod_asl,0) FROM T_Certificati where ID_certificato=1"
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Parameter is not valid lancaster Questions & Help Needed 2 10-27-2008 10:27 PM
Strange cast error agraham Beta Versions 1 10-11-2008 07:43 PM
Specified cast is not valid bish0p Questions & Help Needed 1 07-11-2008 08:17 PM
Unable to cast Object Treeview.node Mr_Gee Questions & Help Needed 15 03-21-2008 07:13 PM


All times are GMT. The time now is 02:36 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.1.0