Problems with casting database values

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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.
 
Back
Top