Click here to Skip to main content
15,917,862 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing an application where i need to read xml file and using readxml() method and transfer data into the database. But I am getting some problem. Please help me to do this
Code is something like this:

DataSet ds = new DataSet("student");
        DataSet ds1 = new DataSet("student");
        private void button2_Click(object sender, EventArgs e)
        {

            
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=true;");
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * from student", con);
            ds.ReadXml(textBox1.Text);
            da.Fill(ds1,"student");
            ds.Merge(ds1);
            da.Update(ds, "student");
            dataGridView1.DataSource = ds.Tables[0].DefaultView;
            MessageBox.Show("Updated Successfully.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Posted
Updated 15-Apr-13 2:15am
v3
Comments
[no name] 15-Apr-13 7:48am    
The table "student" probably does not even exist in your master database. Connect to the correct database.
You will never, ever update anything using a select statement....
Pheonyx 15-Apr-13 7:54am    
Question 1) What is the exception
Question 2) Why are you not setting the update command? The update method runs what what ever SQL code is set to be the update command.
Question 3) Why not use a Typed Dataset based around the XML/Database table schema?
Question 4) What is the format of the XML document?

There are a lot of unknowns and without more information we cannot provide much help.

hi, Try this ...

protected void button1_Click(object sender, EventArgs e)
   {
       SqlCommand command;
       SqlDataAdapter adpter = new SqlDataAdapter();
       DataSet ds = new DataSet();
       XmlReader xmlFile;
       string sql = null;
       string Product_Name = null;
       string path = MapPath("XMLFile.xml");
       xmlFile = XmlReader.Create(path, new XmlReaderSettings());
       ds.ReadXml(xmlFile);
       int i = 0;
       con.Open();
       for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
       {
           Product_Name = ds.Tables[0].Rows[i].ItemArray[1].ToString();
           sql = "insert into tab1 values('" + Product_Name + "')";
           command = new SqlCommand(sql, con);
           adpter.InsertCommand = command;
           adpter.InsertCommand.ExecuteNonQuery();
       }
       con.Close();
   }
 
Share this answer
 
 
Share this answer
 

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