the above is the source code i have.
Code behind
protected void btnview_Click(object sender, EventArgs e)
{
GridView1.Visible = true;
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=TNVKP_8;Integrated Security=True");
con.Open();
DataSet reportData = new DataSet();
//reportData.ReadXml(Server.MapPath("test.xml"));
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");
}
}
the above code i have used to view the xml data in the gridview now i want to insert the gridview data in to database. Post solution for this
Thanks in advance
Satyapriya NayakPosted Dec 5, 2011, 2:38 AM
Try this...
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
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;
//SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=TNVKP_8;Integrated Security=True");
//con.Open();
DataSet reportData = new DataSet();
//reportData.ReadXml(Server.MapPath("test.xml"));
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";
}
}
}
Thanks
If this post helps you mark it as answer
Rajkumar RPosted Dec 5, 2011, 3:05 AM
TulasiPosted Dec 5, 2011, 1:09 AM
check the below link . i hope that u will get an idea
http://www.c-sharpcorner.com/Blogs/7125/bulk-insert-into-sql-database-using-xml-file.aspx