2

I have this xml file, and I want to save value NUMBER (for example) to a SQL Server table.

<ORDER>
  <ORDER_HEADER>
    <NUMBER>10945</NUMBER>
    <TIME>7.8.2013 12:45:20</TIME>
    <NOTE>this is Note</NOTE>   
  </ORDER_HEADER>
</ORDER> 

This is my code:

XDocument doc = XDocument.Load("C:\\Users\\L\\Desktop\\data.xml");
var NUMBER = doc.Descendants("NUMBER");
var TIME = doc.Descendants("TIME");
var NOTE = doc.Descendants("NOTE");

foreach (var cislo in NUMBER)
{
    SqlConnection conn = new SqlConnection("Data Source=***");
    conn.Open();

    using (SqlCommand cmd = conn.CreateCommand())
    {
       cmd.CommandText = "Update CISLO SET cislo = @cislo1;";
       cmd.Parameters.AddWithValue("@cislo1", doc);

       cmd.ExecuteNonQuery();
    }
 }

 MessageBox.Show("OK");

I get this error:

There is no mapping from object type System.Xml.Linq.XDocument to a known managed provider native type.

On row:

cmd.ExecuteNonQuery();

1 Answer 1

3

You are passing 'doc', which is your XDocument, into the parameter. Try changing

cmd.Parameters.AddWithValue("@cislo1", doc);

to

cmd.Parameters.AddWithValue("@cislo1", cislo);
Sign up to request clarification or add additional context in comments.

3 Comments

probably should be cislo.Value
YES!! cmd.Parameters.AddWithValue("@cislo1", cislo.Value);
Good call, @MarcinJuraszek

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.