Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Having trouble working with non-string values reading from dataset 2

Status
Not open for further replies.

CraigBest

Programmer
Aug 1, 2001
545
US
Hi Folks

Well, I've set up my classes and collections of classes, and now I'm trying to read data out of my database and into the fields of the class objects. Everything seems to work fine when I read out String vaules, but when I hit anything other than a string (a Short, let's say) I am getting a type conversion error. I tried surrounding the statement with a cType() function but that doesn't stop the error.

Here's a breif example of what I'm talking about:

RR = New clsRailroad
Me.RailroadTableAdapter.Fill(Me.DsSpatcher.Railroad)
RR.ID = CType(DsSpatcher.Railroad.Item("ID").ToString, Short)

RR.ID is defined in the class as a Short data type, and a Numeric in the DB (Access 2003).

The last statement brings a type conversion error with or without the cType() function.

I would prefer not to use the .ToString method on the end but there doesn't seem to be any alternative that I can find...?

Thanks for any help


CraigHartz
 
Is this a typed dataset? If so, if your class members and your table columns have the same datatype, you should not have to convert or use .toString...

RR.ID = DsSpatcher.Railroad.rows(0).ID

mmaz
 
Hi mmaz,

I don't beleve that it is a typed dataset - at least I am sure it was not specifically created as one. It's my understanding from what I've read recently that a typed DS would solve a lot of problems for me. But I have not been able to find instructions I can understand on how to create it. The current Dataset was built by the IDE from a data source object.

Don't suppose you can explain to me how to create a typed DS, or point me to some instructions on the web to learn how?

Craig

CraigHartz
 
A typed dataset would be a physical file in your project. To replicate, add new item, and then select "Dataset". It will create a file. Then, if you have a connection to a database displayed in your Server Explorer, within Visual Studio, you can drag-drop tables directly in your dataset file. This saves the structure of your database and you can solve a lot of problems at compile-time instead of run-time.

In your case, your dataset file would be DsSpatcher, and you should see a table "Railroad" on it...
 
1) What is the exact error message.
2) Put a break on the line and what is the actual value of DsSpatcher.Railroad.Item("ID").ToString when it errors.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Sorwen,

The exact message is:
"Conversion from string "ID" to type 'Integer' is not valid."

mmaz,

Yes - I do have a physical file in the project dsSpatcher.xsd in the project. There is a table called Railroad (as well as all the other tables from the DB as well). What you describe is ecactly how I created it - is this then a typed recordset after all?



CraigHartz
 
That error means the actual value of DsSpatcher.Railroad.Item("ID").ToString is ID and not the number you were looking for it to return. Would need to see more of your code to find the problem, but the first guess would be you are pulling the header information rather than the actual row.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I agree with Sorwen. In my first post, I put in the line of code:

RR.ID = DsSpatcher.Railroad.rows(0).ID

this gets the first row in the table. You just need to get the appropriate row.
 
I agree with mmaz.

RR.ID = DsSpatcher.Railroad.Rows(0).ID
or to look like yours
RR.ID = DsSpatcher.Railroad.Rows(0).Item("ID")

Both are the same thing.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
mmaz, Sorwen,

Thanks - you are right, I was not specifying the first row. For some reason I was thinking that because I was only returning one row I didn't need to make that specification - obviously that was wrong.

Thanks, that fixed the problem (And I appreciate knowing that I set up a typed dataset, even if I had no idea that was what I was doing!


CraigHartz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top