Click here to Skip to main content
15,893,401 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Ive managed to fix the previous code but now its writing and empty XML file with an empty dataset, any reason why? thanks :) x


C#
private void databasexml_Click(object sender, EventArgs e)
     {



         string sql = "SELECT * FROM BookDetails";
         DataSet ds = new DataSet();
         BindingSource bs = new BindingSource();
         SqlDataAdapter sda = new SqlDataAdapter(sql, stringConnection);


         try
         {

         ds.WriteXml("C:\\Users\\Louise Tooze\\Desktop\\XMLFile2.xml");
         dataGridView1.DataMember = "";
         sda.Fill(ds);


         dataGridView1.DataSource = ds.Tables[0];
         dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
         dataGridView1.Update();
         sda.Update(ds);

         sda.Dispose();

         MessageBox.Show("XML File created Successfully");
         }

         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
         }

     }
Posted
Updated 15-May-12 8:10am
v2
Comments
Maciej Los 15-May-12 14:09pm    
Do not repost! Please, remove this question.

You are creating a new DataSet ds, then using WriteXml before there is anything in it. Try filling it first, then writing it.
 
Share this answer
 
Change your code to:
C#
try
{
    .   
    .
    .
   
    // data filled in dataset
    sda.Fill(ds);
     
    // This line will be after you populate the dataset with data
    ds.WriteXml("C:\\Users\\Louise Tooze\\Desktop\\XMLFile2.xml");
    .
    .
    .
    .
    .
}
 
Share this answer
 
Comments
VJ Reddy 15-May-12 20:57pm    
Good answer. 5!
Sandeep Mewara 16-May-12 1:19am    
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900