Ive been given the job of debugging someones code (who has left the company). Been struggling with a Cheshire cat problem of it losing data input by the user which has taken ages to pinpoint. I think I might have found the reason.
He (or she - I'm not sure) has some code similar to:
XmlDocument customers = dbConn.Query("SELECT * FROM customer WHERE ID=" + "15"
which I believe is suppose to pull the data from the db (MS SQL) and create xml node. Some of the values in the database are Nulls, however and I think this is causing a problem. Outputting the data ie using customers.OuterXml I get a lot of 'Unable to cast object of type System.DBNull to System.String' warnings within the output text, whereever the value is null in the db, such as
<row><id>15</id>
<name>Customer</name>
<website></website><warning>Exception: Unable to cast object of type 'System.DBNull' to type 'System.String'.</warning>
<address1>22 Somewhere</address>...
I assume this would cause a problem in the xml(and its not a problem in printing out the node to a string)
Does anyone know the best way to solve this? I assume I can do a cast within the sql itself to convert dbnulls to an empty string, but this would require adding every field name explicitly rather than, as they are doing, using an * for all fields. is thera way I can convert the whole lot so I don't need to do this.
Dont know if this is the cause of the error, but Ive spent 3 days trying to find whats causing the loss and getting desperate. If anyone has suggestions they'd be gratefully recieved
Thanks
AndyH1
He (or she - I'm not sure) has some code similar to:
XmlDocument customers = dbConn.Query("SELECT * FROM customer WHERE ID=" + "15"
which I believe is suppose to pull the data from the db (MS SQL) and create xml node. Some of the values in the database are Nulls, however and I think this is causing a problem. Outputting the data ie using customers.OuterXml I get a lot of 'Unable to cast object of type System.DBNull to System.String' warnings within the output text, whereever the value is null in the db, such as
<row><id>15</id>
<name>Customer</name>
<website></website><warning>Exception: Unable to cast object of type 'System.DBNull' to type 'System.String'.</warning>
<address1>22 Somewhere</address>...
I assume this would cause a problem in the xml(and its not a problem in printing out the node to a string)
Does anyone know the best way to solve this? I assume I can do a cast within the sql itself to convert dbnulls to an empty string, but this would require adding every field name explicitly rather than, as they are doing, using an * for all fields. is thera way I can convert the whole lot so I don't need to do this.
Dont know if this is the cause of the error, but Ive spent 3 days trying to find whats causing the loss and getting desperate. If anyone has suggestions they'd be gratefully recieved
Thanks
AndyH1