I don't entirely understand how to xml but this is what i have been working on.
This is my Add Product code. It insert into database and xml file. I took me 4 days trying to get the xml to work and now it works nicely. But now the problem is I dont know how to edit only the selected id nodes
if (FileUpload1.HasFile)
{
string str = FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Image/" + str));
string Image = "Image/" + str.ToString();
con.Open();
string query = "INSERT INTO Product (product_Name, product_image, product_Price,product_Description) VALUES (@product_Name, @product_image, @product_Price, @product_Description)";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@product_Name", product_name.Text);
cmd.Parameters.AddWithValue("@product_image", Image);
cmd.Parameters.AddWithValue("@product_Price", product_price.Text);
cmd.Parameters.AddWithValue("@product_Description", product_description.Text.Trim());
cmd.ExecuteNonQuery();
string strSQL = "SELECT * FROM Product";
SqlDataAdapter dt = new SqlDataAdapter(strSQL, con);
DataSet ds = new DataSet("productdetails");
dt.Fill(ds, "product");
ds.WriteXml(Server.MapPath(@".\xml\products.xml"));
con.Close();
Response.Redirect("AdminMenu.aspx");
}
else
{
Label5.Text = "Please Upload your Image";
Label5.ForeC
This is the Editing Product Code still in progress with the xml. No problem for the database site.
string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Product WHERE Id=" + contact_id))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
foreach (DataRow row in dt.Rows)
{
string id = row["Id"].ToString();
string Name = row["Product_Name"].ToString();
string Price = row["Product_Price"].ToString();
string Description = row["Product_Description"].ToString();
this.HiddenField1.Value = id;
this.TextBoxName.Text = Name;
this.TextBoxPrice.Text = Price;
this.TextBoxDescription.Text = Description;
}
}
}
}
}
What i am trying to achieve here is to update the existing details in the product.xml.
XML Content
<?xml version="1.0" standalone="yes"?>
<productdetails>
<product>
<Id>3</Id>
<Product_Name>Banana</Product_Name>
<Product_Price>12.0000</Product_Price>
<Product_image>Image/YellowBanana.jpg</Product_image>
<Product_Description>Banana brighter than the sun</Product_Description>
</product>
<product>
<Id>4</Id>
<Product_Name>Apple</Product_Name>
<Product_Price>23.0000</Product_Price>
<Product_image>Image/Apple.jpg</Product_image>
<Product_Description>Very Red and Delicious</Product_Description>
</product>
<product>
<Id>5</Id>
<Product_Name>Mango</Product_Name>
<Product_Price>17.9000</Product_Price>
<Product_image>Image/AwesomeMango.jpg</Product_image>
<Product_Description>Juicy Fruit Mango</Product_Description>
</product>
</productdetails>
First, when inserting, you need to get the newly created Id. Modify you insert command to get the Id out of it. Add this after the INSERT
statement ;SELECT SCOPE_IDENTITY()
and use ExecuteScalar()
to retreive it.
...
string query = "INSERT INTO Product (product_Name, product_image, product_Price,product_Description) VALUES (@product_Name, @product_image, @product_Price, @product_Description); SELECT SCOPE_IDENTITY();";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@product_Name", product_name.Text);
cmd.Parameters.AddWithValue("@product_image", Image);
cmd.Parameters.AddWithValue("@product_Price", product_price.Text);
cmd.Parameters.AddWithValue("@product_Description", product_description.Text.Trim());
int newId = (int)cmd.ExecuteScalar();
//Call function to add product info to products.xml file
appendToProducts(Server.MapPath(@".\xml\products.xml"),newId, product_name.Text, Image, product_price.Text, product_description.Text.Trim());
...
Then call a function named appendToProducts
(code follow)
...
private void appendToProducts(string xmlFilePath, int productId, string productName, string productImage, string productPrice, string productDescription) {
XmlDocument xmlDoc = new System.Xml.XmlDocument();
//LOAD FILE
xmlDoc.load(xmlFilePath);
XmlElement productElement = xmlDoc.CreateElement("product"); //CREATE <product> node
//Create child nodes
XmlElement idElement = xmlDoc.CreateElement("Id");
XmlElement nameElement = xmlDoc.CreateElement("Product_Name");
XmlElement priceElement = xmlDoc.CreateElement("Product_Price");
XmlElement imgElement = xmlDoc.CreateElement("Product_image");
XmlElement descElement= xmlDoc.CreateElement("Product_Description");
idElement.InnerText = productId.ToString();
nameElement.InnerText = productName;
priceElement.InnerText = productPrice;
imgElement.InnerText = productImage;
descElement.InnerText = productDescription;
//Append childs element to product node
productElement.AppendChild(idElement);
productElement.AppendChild(nameElement);
productElement.AppendChild(priceElement);
productElement.AppendChild(imgElement);
productElement.AppendChild(descElement);
//Append <product> to <productdetails> root element
xmlDoc.DocumentElement.AppendChild(productElement);
//Save
xmlDoc.Save(xmlFilePath)
}
...
Read more on Xml on Microsoft website: https://docs.microsoft.com/en-us/dotnet/api/system.xml?view=netframework-4.8
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.