XML Insert Update Delete
XML Select.Insert,Update,Delete
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
binddata();
}
binddata();
}
void binddata()
{
//-- FOR SORTING THE PARTICULAR FIELD
//DataView dv = ds.Tables[0].DefaultView;
// dv.Sort = "id desc";
//GridView1.DataSource = dv;
//DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("XML/GuestBook.xml"), XmlReadMode.ReadSchema);
dsBind();
}
void dsBind()
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (!File.Exists(Server.MapPath("XML/GuestBook.xml")))
{
CreateXMLFile();
dt = ds.Tables.Add("GuestBook");
}
binddata();
ds = GridView1.DataSource as DataSet;
DataRow dr = ds.Tables[0].NewRow();
DataColumn dc = dt.Columns.Add("id", Type.GetType("System.Int32"));
dc.AutoIncrement = true;
dc.AutoIncrementSeed = 1;
dc.AutoIncrementStep = 1;
dr[1] = System.DateTime.Today.ToString();
dr[2] = txtName.Text;
dr[3] = txtEmail.Text;
dr[4] = txtComments.Text;
ds.Tables[0].Rows.Add(dr);
ds.AcceptChanges();
ds.WriteXml(Server.MapPath("XML/GuestBook.xml"), XmlWriteMode.WriteSchema);
dsBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
binddata();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
binddata();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int i = GridView1.Rows[e.RowIndex].DataItemIndex;
string strDate = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
string strName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
string strMail = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
string strComm = ((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text;
// Update the XML file using the new values
binddata();
ds.Tables[0].Rows[i]["datetime"] = strDate;
ds.Tables[0].Rows[i]["name"] = strName;
ds.Tables[0].Rows[i]["email"] = strMail;
ds.Tables[0].Rows[i]["comments"] = strComm;
GridView1.EditIndex = -1;
ds.WriteXml(Server.MapPath("XML/GuestBook.xml"), XmlWriteMode.WriteSchema);
dsBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
binddata();
DataSet ds = GridView1.DataSource as DataSet;
ds.Tables[0].Rows[GridView1.Rows[e.RowIndex].DataItemIndex].Delete();
//ds.Tables["GuestBook"].Rows[GridView1.Rows[e.RowIndex].DataItemIndex].Delete();
if (ds.Tables[0].Rows.Count == 0)
{
CreateXMLFile();
}
dsBind();
}