In this blog we will know how to display xml file records in
a gridview. We can also insert selected records to the database by selecting
the checkbox.
<% @ Page Language ="C#" AutoEventWireup ="true"
CodeFile ="Default.aspx.cs"
Inherits ="_Default"
%>
<! 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 > Untitled Page</ title >
</ head >
< body >
< form
id ="form1"
runat ="server">
< div >
< asp : GridView ID ="GridView1"
runat ="server"
Width ="96%" >
< Columns >
< asp : TemplateField HeaderText ="Select">
< ItemTemplate >
< asp : CheckBox ID ="chkbox" runat ="server"></ asp : CheckBox >
</ ItemTemplate >
</ asp : TemplateField >
< asp : TemplateField HeaderText ="Remark">
< ItemTemplate >
< asp : Label ID ="lblremark" runat ="server"></ asp : Label >
</ ItemTemplate >
</ asp : TemplateField >
</ Columns >
</ asp : GridView >
</ div >
< asp : Button ID ="btnview"
runat ="server"
onclick ="btnview_Click"
Text ="Show
Records " />
< asp : FileUpload ID ="FileUpload1"
runat ="server"
/>
< asp : Button ID ="btninsert"
runat ="server"
onclick ="btninsert_Click"
Text ="Insert
Records" />< br />
< asp : Label ID ="Label1" runat ="server"
Text ="Label"></ asp : Label >
</ form >
</ body >
</ html >
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial
class _Default
: System.Web.UI.Page
{
string connStr = ConfigurationManager .ConnectionStrings["ConnectionString" ].ConnectionString;
string filename;
string str;
protected
void Page_Load(object
sender, EventArgs e)
{
}
protected void btnview_Click(object
sender, EventArgs e)
{
SqlConnection con = new SqlConnection (connStr);
con.Open();
GridView1.Visible = true ;
DataSet reportData = new DataSet ();
filename = System.IO.Path .GetExtension(FileUpload1.FileName.ToString());
if (filename == ".xml" )
{
reportData.ReadXml(Server.MapPath(FileUpload1.FileName));
GridView1.DataSource = reportData;
GridView1.DataBind();
con.Close();
}
else
{
GridView1.Visible = false ;
Response.Write("Invalid
File" );
}
con.Close();
}
protected void btninsert_Click(object
sender, EventArgs e)
{
SqlConnection con = new SqlConnection (connStr);
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row =
GridView1.Rows[i];
CheckBox chkbox = (CheckBox )row.FindControl("chkbox" );
if (chkbox.Checked == true )
{
con.Open();
str = "insert into
employee
(empid,empname,empaddress,empcity,empstate,empzip,emplic,empstatus,empp,empi)
values('" + row.Cells[2].Text + "','"
+ row.Cells[3].Text + "','" +
row.Cells[4].Text + "','" +
row.Cells[5].Text + "','" +
row.Cells[6].Text + "','" +
row.Cells[7].Text + "','" +
row.Cells[8].Text + "','" +
row.Cells[9].Text + "','" +
row.Cells[10].Text + "','" +
row.Cells[11].Text + "')" ;
SqlCommand com=new SqlCommand (str,con);
com.ExecuteNonQuery();
con.Close();
}
Label1.Text = "Records
inserted" ;
}
}
}
Join the conversation! Your thoughts help the community grow.