How To Create XML File and Insert,Update,Delete Record In XML File using ASP.NET with C#
Default.aspx
<%@ Page Language="C#"
AutoEventWireup="false" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="CrystalDecisions.Web,
Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"
Namespace="CrystalDecisions.Web" TagPrefix="CR"
%>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body bgcolor="#3399CC">
<form id="form1" runat="server">
<div>
<table border="1" bordercolor="white">
<tr>
<td>
<asp:Label ID="Label5" ForeColor="White" runat="server" Text="emp_no"></asp:Label></td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label6" ForeColor="White" runat="server" Text="emp_nm"></asp:Label></td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label7" runat="server" ForeColor="White" Text="emp_add"></asp:Label></td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label8" runat="server" ForeColor="White" Text="emp_salary"></asp:Label></td>
<td>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label9" runat="server" ForeColor="White" Text="emp_image"></asp:Label></td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="Button2"
runat="server" Text="insert"
onclick="Button2_Click"
/>
<asp:Button ID="Button3" runat="server" Visible="false" Text="update" onclick="Button3_Click" />
</td>
</tr>
</table>
<asp:Button ID="Button4" BackColor="White" runat="server" Text="create crystal report"
onclick="Button4_Click" />
<asp:Button ID="Button1" runat="server" Text="show grid"
onclick="Button1_Click" /><br />
<asp:GridView ID="GridView1" runat="server" DataKeyNames="emp_no,emp_image"
AutoGenerateColumns="False" onrowediting="GridView1_RowEditing"
onrowdeleting="GridView1_RowDeleting" ForeColor="White"
BorderStyle="Solid"
BorderColor="White">
<Columns>
<asp:TemplateField HeaderText="emp_no">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("emp_no")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="emp_nm">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#Eval("emp_nm")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="emp_add">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%#Eval("emp_add")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="emp_salary">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%#Eval("emp_salary")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="emp_image">
<ItemTemplate>
<asp:Label ID="Label9" Visible="false" runat="server" Text='<%#Eval("emp_image")%>'></asp:Label>
<asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("emp_image")%>' Height="75px" Width="75px" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="edit">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" ForeColor="Yellow" CommandName="edit" runat="server">edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="delete">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" ForeColor="Yellow" CommandName="delete" runat="server">delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="select">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" ImageUrl="~/image/face.jpeg" Width="30px"
Height="30px"
OnClick="select"
runat="server"
/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"
AutoDataBind="true"
/>
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using System.IO;
using
System.Xml;
using
CrystalDecisions.CrystalReports.Engine;
using
CrystalDecisions.Shared;
using
System.Data;
using
System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
grid();
DataSet
ds = new DataSet("detail");
ds.ReadXml(Server.MapPath(@"\example of xmlfile\XMLFile.xml"));
}
protected void Button1_Click(object
sender, EventArgs e)
{
DataSet
ds = new DataSet();
ds.ReadXml(Server.MapPath(@"\example of xmlfile\XMLFile.xml"));
GridView1.DataSource = ds;
GridView1.DataBind();
}
public void cleardata()
{
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
}
public void grid()
{
DataSet
ds = new DataSet();
ds.ReadXml(Server.MapPath(@"\example of xmlfile\XMLFile.xml"));
if (ds
!= null)
{
GridView1.DataSource = ds;
GridView1.DataBind();
// im();
}
}
public void im()
{
foreach
(GridViewRow r in
GridView1.Rows)
{
Image
i = (Image)(GridView1.Rows[r.RowIndex].Cells[4].FindControl("Image1"));
string
index = GridView1.DataKeys[r.RowIndex].Values[1].ToString();
i.ImageUrl = i.ToString();
i.DataBind();
}
}
protected void Button2_Click(object
sender, EventArgs e)
{
string img = null;
DataSet
ds = new DataSet();
ds.ReadXml(Server.MapPath(@"\example of xmlfile\XMLFile.xml"));
if
(FileUpload1.HasFile)
{
// string
ext = Path.GetExtension(FileUpload1.FileName);
//if
(ext == ".jpg" || ext == ".png")
//{
//
img = new byte[FileUpload1.PostedFile.InputStream.Length + 1];
//FileUpload1.PostedFile.InputStream.Read(img,
0, img.Length);
//}
FileUpload1.SaveAs(Server.MapPath("~/image/"+FileUpload1.FileName));
img="~/image/"+FileUpload1.FileName;
}
int
emp_no = Convert.ToInt32(TextBox1.Text);
string
emp_nm=TextBox2.Text;
string
emp_add=TextBox3.Text;
int
emp_salary=Convert.ToInt32(TextBox4.Text);
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
//ds.Tables[0].Rows[ds.Tables[0].Rows.Count
- 1]["emp_no"] =
(Convert.ToInt32(ds.Tables[0].Rows[ds.Tables[0].Rows.Count -
2]["emp_no"]) + 1).ToString();
ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1]["emp_no"]=emp_no;
ds.Tables[0].Rows[ds.Tables[0].Rows.Count
- 1]["emp_nm"] = emp_nm;
ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1]["emp_add"]
= emp_add;
ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1]["emp_salary"]
= emp_salary;
ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1]["emp_image"]
=img;
ds.AcceptChanges();
ds.WriteXml(Server.MapPath(@"\example of xmlfile\XMLFile.xml"));
GridView1.DataSource = ds;
GridView1.DataBind();
cleardata();
}
protected void GridView1_RowEditing(object
sender, GridViewEditEventArgs e)
{
Label
l1 = (Label)GridView1.Rows[e.NewEditIndex].FindControl("Label1");
Label
l2 = (Label)GridView1.Rows[e.NewEditIndex].FindControl("Label2");
TextBox2.Text = l2.Text;
Label
l3 = (Label)GridView1.Rows[e.NewEditIndex].FindControl("Label3");
TextBox3.Text = l3.Text;
Label
l4 = (Label)GridView1.Rows[e.NewEditIndex].FindControl("Label4");
Label
l5 = (Label)GridView1.Rows[e.NewEditIndex].FindControl("Label9");
Session["photo"]
= l5.Text;
TextBox4.Text = l4.Text;
Session["no"]
= l1.Text;
Label5.Visible = false;
TextBox1.Visible = false;
Button2.Visible = false;
Button3.Visible = true;
}
protected void Button3_Click(object
sender, EventArgs e)
{
DataSet
ds = new DataSet();
ds.ReadXml(Server.MapPath(@"\example of xmlfile\XMLFile.xml"));
/*foreach(GridViewRow
r in GridView1.Rows)
{
index =
GridView1.DataKeys[r.RowIndex].Value.ToString();
}*/
string
emp_nm = TextBox2.Text;
string
emp_add = TextBox3.Text;
int
emp_salary = Convert.ToInt32(TextBox4.Text);
string
img = null;
if
(FileUpload1.HasFile)
{
FileUpload1.SaveAs(Server.MapPath("~/image/" + FileUpload1.FileName));
img = "~/image/"
+ FileUpload1.FileName;
}
else
{
img = Session["photo"].ToString();
}
System.Data.DataRow[]
dr = ds.Tables[0].Select("emp_no= '"
+Convert.ToInt32(Session["no"].ToString())+ "'");
if (dr !=
null && dr.Length > 0)
{
dr[0]["emp_nm"]
= emp_nm;
dr[0]["emp_add"]
= emp_add;
dr[0]["emp_salary"]
= emp_salary;
dr[0]["emp_image"]
= img;
}
ds.AcceptChanges();
ds.WriteXml(Server.MapPath(@"\example of xmlfile\XMLFile.xml"));
GridView1.DataSource = ds;
GridView1.DataBind();
Label5.Visible = true;
TextBox1.Visible = true;
Button3.Visible = false;
Button2.Visible = true;
cleardata();
}
protected void GridView1_RowDeleting(object
sender, GridViewDeleteEventArgs e)
{
DataSet
ds = new DataSet();
ds.ReadXml(Server.MapPath(@"\example of xmlfile\XMLFile.xml"));
string
index = GridView1.DataKeys[e.RowIndex].Value.ToString();
System.Data.DataRow[]
dr = ds.Tables[0].Select("emp_no= '"
+index + "'");
if (dr
!= null && dr.Length > 0)
{
ds.Tables[0].Rows.Remove(dr[0]);
}
ds.AcceptChanges();
ds.WriteXml(Server.MapPath(@"\example of xmlfile\XMLFile.xml"));
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Button4_Click(object
sender, EventArgs e)
{
DataSet
ds = new DataSet();
DataSet1
ds1 = new DataSet1();
ds.ReadXml(Server.MapPath("XMLFile.xml"));
DataTable
dt = new DataTable();
dt=ds.Tables[0];
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow
dr = ds1.pintu.NewRow();
dr["emp_no"]
= dt.Rows[i]["emp_no"].ToString();
dr["emp_nm"]
= dt.Rows[i]["emp_nm"].ToString();
dr["emp_add"]
= dt.Rows[i]["emp_add"].ToString();
dr["emp_salary"]
= dt.Rows[i]["emp_salary"].ToString();
string
s =Server.MapPath(dt.Rows[i]["emp_image"].ToString());
FileStream
fs = new FileStream(s,
FileMode.Open, FileAccess.Read);
Byte[]
imge = new byte[fs.Length];
fs.Read(imge, 0, System.Convert.ToInt32(fs.Length));
dr["emp_image"]
= imge;
ds1.pintu.Rows.Add(dr);
}
ReportDocument
reportDoc = new ReportDocument();
reportDoc.Load(Server.MapPath("CrystalReport.rpt"));
reportDoc.SetDataSource(ds1);
CrystalReportViewer1.ReportSource =
reportDoc;
CrystalReportViewer1.RefreshReport();
}
protected void select(object
sender, ImageClickEventArgs e)
{
ImageButton
imgb = sender as ImageButton;
GridViewRow
row = (GridViewRow)imgb.NamingContainer;
ReportDocument
reportDoc = new ReportDocument();
reportDoc.Load(Server.MapPath("CrystalReport.rpt"));
DataSet1
ds1 = new DataSet1();
Label
l1 = (Label)row.FindControl("Label1");
Label l2 = (Label)row.FindControl("Label2");
Label
l3 = (Label)row.FindControl("Label3");
Label
l4 = (Label)row.FindControl("Label4");
Image
i = (Image)row.FindControl("Image1");
int
emp_no = Convert.ToInt32(l1.Text);
string
emp_nm = l2.Text;
string
emp_add = l3.Text;
int
emp_salary = Convert.ToInt32(l4.Text);
DataRow
dr = ds1.pintu.NewRow();
dr["emp_no"]
= emp_no;
dr["emp_nm"]
= emp_nm;
dr["emp_add"]
= emp_add;
dr["emp_salary"]
= emp_salary;
string
s = Server.MapPath(i.ImageUrl);
FileStream
fs = new FileStream(s,
FileMode.Open, FileAccess.Read);
Byte[]
imge = new byte[fs.Length];
fs.Read(imge, 0, System.Convert.ToInt32(fs.Length));
dr["emp_image"]
= imge;
ds1.pintu.Rows.Add(dr);
ReportDocument
report = new ReportDocument();
report.Load(Server.MapPath("CrystalReport.rpt"));
report.SetDataSource(ds1);
CrystalReportViewer1.ReportSource =
report;
CrystalReportViewer1.RefreshReport();
}
}
0 comments :