473,804 Members | 2,191 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

NULL Value - Specified cast is not valid

I am accessing MS Access db with tblA whose column UNABLE can have NULL
value. When I access UNABLE whose value is null, I got an error "
Run-time exception thrown : System.InvalidC astException - Specified cast is
not valid." either when I do
IsDBNull(dr.Get String(0))
or
dr.GetString(0)
How can I fix this problem ?

Thanks.

Dim sUnable as string
Dim sql As String
Dim dr As OleDb.OleDbData Reader
Dim cmd As New OleDb.OleDbComm and

sql = "select UNABLE from tblA where ACCOUNT = 'xyz'"
With cmd
.Connection = g_ConnectionDem oOLE
.CommandText = sql
dr = .ExecuteReader( )
End With
Do While dr.Read
If bErr Then
sUnable = ""
Else
If IsDBNull(dr.Get String(0)) Then sUnable = "" Else sUnable =
dr.GetString(0) --> ERROR HERE
End If
If bErr Then sUnable = ""
Loop


May 4 '06 #1
3 6471
> I am accessing MS Access db with tblA whose column UNABLE can have
NULL
value. When I access UNABLE whose value is null, I got an error "
Run-time exception thrown : System.InvalidC astException - Specified
cast is
not valid." either when I do
IsDBNull(dr.Get String(0))
or
dr.GetString(0)
How can I fix this problem ?
Thanks.


You need to check the IsDBNull on the column, not the result of the column.
Thus you should change it to:

If dr.IsDBNull(0) then MyVal = dr.GetSTring(0)

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx
May 4 '06 #2
That is because you are calling the GetString method before checking if
the value is null. Use the IsDBNull method:

If dr.IsDBNull(0) Then sUnable = String.Empty Else sUnable = dr.GetString(0)

fniles wrote:
I am accessing MS Access db with tblA whose column UNABLE can have NULL
value. When I access UNABLE whose value is null, I got an error "
Run-time exception thrown : System.InvalidC astException - Specified cast is
not valid." either when I do
IsDBNull(dr.Get String(0))
or
dr.GetString(0)
How can I fix this problem ?

Thanks.

Dim sUnable as string
Dim sql As String
Dim dr As OleDb.OleDbData Reader
Dim cmd As New OleDb.OleDbComm and

sql = "select UNABLE from tblA where ACCOUNT = 'xyz'"
With cmd
.Connection = g_ConnectionDem oOLE
.CommandText = sql
dr = .ExecuteReader( )
End With
Do While dr.Read
If bErr Then
sUnable = ""
Else
If IsDBNull(dr.Get String(0)) Then sUnable = "" Else sUnable =
dr.GetString(0) --> ERROR HERE
End If
If bErr Then sUnable = ""
Loop

May 4 '06 #3
fniles,
As the other suggest I normally use dr.IsDBNull(0) also.

However the "problem" you are having is that dr.GetString is attempting to
return a String, when you have a Null. You then attempt to pass this String
to the IsDBNull function. You need to pass an Object to the IsDBNull
function, you can use need to use dr.GetValue to retrieve this object then
use the IsDBNull function:

If IsDBNull(dr.Get Value(0)) Then

However as I stated, I simply use dr.IsDBNull instead...

| If dr.IsDBNull(0) Then sUnable = "" Else sUnable =
| dr.GetString(0)

As it avoids "getting" the value twice...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"fniles" <fn****@pfmail. com> wrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
|I am accessing MS Access db with tblA whose column UNABLE can have NULL
| value. When I access UNABLE whose value is null, I got an error "
| Run-time exception thrown : System.InvalidC astException - Specified cast
is
| not valid." either when I do
| IsDBNull(dr.Get String(0))
| or
| dr.GetString(0)
| How can I fix this problem ?
|
| Thanks.
|
| Dim sUnable as string
| Dim sql As String
| Dim dr As OleDb.OleDbData Reader
| Dim cmd As New OleDb.OleDbComm and
|
| sql = "select UNABLE from tblA where ACCOUNT = 'xyz'"
| With cmd
| .Connection = g_ConnectionDem oOLE
| .CommandText = sql
| dr = .ExecuteReader( )
| End With
| Do While dr.Read
| If bErr Then
| sUnable = ""
| Else
| If IsDBNull(dr.Get String(0)) Then sUnable = "" Else sUnable =
| dr.GetString(0) --> ERROR HERE
| End If
| If bErr Then sUnable = ""
| Loop
|
|
|
|
May 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3644
by: Tao | last post by:
I just upgraded .NET framework to 1.1 and VS.Net to 2003 version and tried to test it out. I created an ASP.NET project using the wizard and tried to run it by hitting "F5". I got an exception: "Specified cast is not valid." The only thing i put there was a "test this." inside the form. What might be the problem here? Thanks in advance. The Exception:
102
6078
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV" ( i am talking of unix machine ) while trying to read that location. Then how can i distinguish between a NULL pointer and an invalid location ? Is this essential that NULL pointer should not point to any of the location in the virtual address...
29
3772
by: Jason Curl | last post by:
I've been reading this newsgroup for some time and now I am thoroughly confused over what NULL means. I've read a NULL pointer is zero (or zero typecast as a void pointer), others say it's compiler dependent (and that NULL might be anything, but it is always NULL). The source snippet is below. The question is: - When I use calloc to allocate a block of memory, preinitialising it to zero, is this equivalent (and portable C) to...
0
623
by: QA | last post by:
I am using a Business Scorecard Accelarator in a Sharepoint Portal 2003 using SQL Server 2005 I am getting the following error: Error,5/7/2005 10:50:14 AM,580,AUE1\Administrator,"Specified cast is not valid.","Microsoft.BusinessIntelligence.Scorecard.ScorecardException: Specified cast is not valid. ---> Microsoft.BusinessIntelligence.Scorecard.ScorecardException: Specified cast is not valid. ---> System.InvalidCastException: Specified...
0
1630
by: Alan Z. Scharf | last post by:
this question in datagrid group for several days with no repsonse. I'm hoping for an answer her because of greater activity in this group. No cross-posting intended. Thanks. ----------------------------------------------------------- Server VS.NET 2003 SQLServer 2000 IIS 66.0
64
3962
by: yossi.kreinin | last post by:
Hi! There is a system where 0x0 is a valid address, but 0xffffffff isn't. How can null pointers be treated by a compiler (besides the typical "solution" of still using 0x0 for "null")? - AFAIK C allows "null pointers" to be represented differently then "all bits 0". Is this correct? - AFAIK I can't `#define NULL 0x10000' since `void* p=0;' should work just like `void* p=NULL'. Is this correct?
5
8389
by: Web learner | last post by:
while (dr.Read()) //dr is an instance of sqlDataReader { double minAirTemp = (double)(Single)dr; // minAirTemp is column in SQLExpress table (data type: real, allows nulls) } The above code works. But when there are NULL, I get the following error System.InvalidCastException: Specified cast is not valid
46
3699
by: lovecreatesbea... | last post by:
Do you prefer malloc or calloc? p = malloc(size); Which of the following two is right to get same storage same as the above call? p = calloc(1, size); p = calloc(size, 1);
3
12596
by: amija0311 | last post by:
Hi, I am new using DB2 9.1 database by windows base. I want to query the data that contain string then translate the string into integer using DB2. The problems is If the data is null, i got the problem to translate. How to translate string also allow null data to integer. If null data it will read as space. My Data :- GEOSEG_ID SEQNO
0
9715
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10600
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10354
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9175
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7642
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.