i Dont Know
i Dont Know

Reputation: 75

How to save XML data in SQL Server?

In my table there is one XML column. I want to fetch the XML data in one textbox and make some corrections and update it:

private void button2_Click(object sender, EventArgs e)
{
     con.Open();
     string str = "select C1 from TableName where C2='" + txt1.Text+ "'";

     SqlCommand cmd1 = new SqlCommand(str, con);
     XmlReader xml = cmd1.ExecuteXmlReader();
     xml.Read();

     txt2.Text = xml.ReadOuterXml();

     XmlDocument doc = new XmlDocument();
     doc.PreserveWhitespace = true;
     doc.LoadXml(txt2.Text);
}

Now I want to make some changes and update it in my database. When I try to change in textbox it does not work. How can I make changes and update in database? Please help

Upvotes: 1

Views: 682

Answers (1)

Arun Gairola
Arun Gairola

Reputation: 884

Update Like this

using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = connection.CreateCommand())
{ 
command.CommandText = "Update TableName set  C1 =  @C1 where C2 = @C2 ";
command.Parameters.AddWithValue("@C1", Textbox2.Text);
command.Parameters.AddWithValue("@C2", Textbox1.text);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
} 

Upvotes: 2

Related Questions