A Database Problem

Problem accessing a part of a database

Page 1 of 1

8 Replies - 631 Views - Last Post: 24 August 2010 - 03:51 PM Rate Topic: -----

#1 Guest_Dave*


Reputation:

A Database Problem

Posted 23 August 2010 - 04:30 PM

Hi. I am having a problem with a MSAccess database using C#. I have multiple columns in the database and all are of type text. I can access all but one part of it with my program but when I try to access the column "productCode" I get an Invalid cast exception. productCode is the only column that contains numbers but as I said it is of type text in MsAccess.

Here is the code I am using:
//(aReader is an OleDBDataReader.
                          case 14:
                              if (aReader.GetString(06) == productCode) // <<<--- This is where Studio says the error is.
                                {
                                    resTable.Rows.Add(aReader.GetString(1), aReader.GetString(8), aReader.GetString(6), aReader.GetString(9));
                                    i++;
                                }
                                break;

This post has been edited by JackOfAllTrades: 23 August 2010 - 05:43 PM
Reason for edit:: Added code tags.


Is This A Good Question/Topic? 0

Replies To: A Database Problem

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: A Database Problem

Posted 24 August 2010 - 04:12 AM

can you show the query that you are using?
Was This Post Helpful? 0
  • +
  • -

#3 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 636
  • Joined: 03-February 10

Re: A Database Problem

Posted 24 August 2010 - 06:02 AM

Are sure there are no NULL values in the column? You could perform a check for nulls before hand.

In line #3 of your posted code, it looks like you are comparing the value from the table to a variable named productCode. What is productCode's datatype?
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Dave*


Reputation:

Re: A Database Problem

Posted 24 August 2010 - 08:52 AM

The Query I am using is
SELECT * FROM products

and the data type of product code is text


There are null values in the column but I have tried checking for nulls before hand.

if (aReader.GetString(06).Equals(DBNull.Value)) //<<<------ I am now getting the same error here.

Was This Post Helpful? 0

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,591
  • Joined: 16-October 07

Re: A Database Problem

Posted 24 August 2010 - 09:19 AM

View PostDave, on 24 August 2010 - 09:52 AM, said:

if (aReader.GetString(06).Equals(DBNull.Value)) //<<<------ I am now getting the same error here.


GetString... Think about it. You've already told it the thing is a string, if it's of type DBNull it's going to crash.

Rather, ask the reader. e.g.
if (aReader.IsNull(6))


Alternately, this should also work:
if (aReader.GetValue(06).Equals(DBNull.Value))


However, I would prefer the IsNull form. Also, it will always crash if you have less than 7 fields.
Was This Post Helpful? 1
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: A Database Problem

Posted 24 August 2010 - 09:28 AM

View PostDave, on 24 August 2010 - 11:52 AM, said:

The Query I am using is
SELECT * FROM products

and the data type of product code is text


There are null values in the column but I have tried checking for nulls before hand.

if (aReader.GetString(06).Equals(DBNull.Value)) //<<<------ I am now getting the same error here.


1. Never do a SELECT *. ONLY return the columns that you need.
2. Try this query... SELECT * FROM Products WHERE Not(ProductCode) Is Null(I am using the * because I don't know your columns.)

Using that query, see if you still get the error.
Was This Post Helpful? 0
  • +
  • -

#7 Guest_Dave*


Reputation:

Re: A Database Problem

Posted 24 August 2010 - 09:56 AM

Thank you! I have had that problem for ages now and your solution has worked.

:bigsmile::)
Was This Post Helpful? 0

#8 Guest_Dave*


Reputation:

Re: A Database Problem

Posted 24 August 2010 - 10:09 AM

baavgai. Your solution also worked so thank you. I can now allow for null values which I need to do in certain situations.

Regards all,
Dave
Was This Post Helpful? 0

#9 Guest_Dave*


Reputation:

Re: A Database Problem

Posted 24 August 2010 - 03:51 PM

Forgot to mention but
if (aReader.IsNull(6))


should be
if (aReader.IsDBNull(6))


just in case anybody else finds this thread useful.
Was This Post Helpful? 0

Page 1 of 1