473,326 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Problems with casting database values

Hello,

I´ve a problem with the return values of a database (MS SQL) query.

My code looks like this:

foreach (DataRow row in dt.Rows)
{
this.fingerprints.Add(new DocumentFingerprint(row["record_id"],
row["XMLString"]));
}
My class DocumentFingerprint is defined as:

public DocumentFingerprint(int record_id,
XmlDocument documentXMLString)
In the Database row["record_id"] is defined as int
and row["XMLString"] is defined as "xml".
My question is now... how do I have to cast what?

When I retrieve row["record_id"] and row["XMLString"] both are "Objects"
and neither "int" nor "xml"/ "XmlDocument".

How do I convert a "xml" value from the database to an "XmlDocument"?

Regards,

Martin
Aug 30 '06 #1
1 1502
The indexer of a DataRow will always return an object. You need to cast
it to the appropriate datatype.

so your code should be something like
foreach (DataRow row in dt.Rows)
{
int lnRecordId = (int)row["record_id"];
string lsXMLString = (string)row["XMLString"];
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(lsXMLString);

this.fingerprints.Add(new DocumentFingerprint(lnRecordId, xDoc);
}

Unfortunately, there is no direct cast from a database field to an
XmlDocument. You have to cast it as string first and load the string
into a new XmlDocument.

Regards,
Sarin.

Martin Pöpping wrote:
Hello,

I´ve a problem with the return values of a database (MS SQL) query.

My code looks like this:

foreach (DataRow row in dt.Rows)
{
this.fingerprints.Add(new DocumentFingerprint(row["record_id"],
row["XMLString"]));
}
My class DocumentFingerprint is defined as:

public DocumentFingerprint(int record_id,
XmlDocument documentXMLString)
In the Database row["record_id"] is defined as int
and row["XMLString"] is defined as "xml".
My question is now... how do I have to cast what?

When I retrieve row["record_id"] and row["XMLString"] both are "Objects"
and neither "int" nor "xml"/ "XmlDocument".

How do I convert a "xml" value from the database to an "XmlDocument"?

Regards,

Martin
Aug 31 '06 #2

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

Similar topics

14
by: mr_semantics | last post by:
I have been reading about the practise of casting values to unsigned char while using the <ctype.h> functions. For example, c = toupper ((unsigned char) c); Now I understand that the standard...
3
by: Matt | last post by:
Hi, Recently we had some code like this cause a failure: MyEnum myEnum = (MyEnum) (int) dt; i.e. reading an int out of the database and casting it into a type-safe enum. The thought...
7
by: Dan | last post by:
I'm fairly new to C# and I am having some problems understanding int variables and null values. I have created a class that populates the values of it's fields from a DataReader in it's...
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
24
by: rn5a | last post by:
The date in my local machine is set to the dd/MM/yyyy format. When I insert a date in a MS-Access DB table, it gets populated in the above format. For e.g. if the date is, say, 8th March 2007, it...
4
by: mitdej | last post by:
Hi there, I have an several enum types that starts from a nunmber other than 0. For example: public enum InternalStatus { Pending = 1, Ported = 2, Suspended = 3 } I put this values in a int...
11
by: weg22 | last post by:
Hi all, I'm having problems trying to type cast in VB.Net. I'd like to read in a 16 bit unsigned integer and convert it to a signed 16 bit integer. I'm using the following line of code: Dim...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
0
by: Michael Rudolph | last post by:
Hi DB2 newsgroup, I have encountered a difference between our development DB2 on Windows and the test environment on AIX. If using the LOCATE scalar function with a SMALLINT as LENGTH parameter...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.