I have a gird view in that i bind XML data in the grid view, Then i want to insert the grid view data in to database
Thanks In advance
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Mar 3, 2012, 2:33 AM EST
Try this...
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Insert_data_from_Gridview_database._Default" %>
using System;
using System.Collections;
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;
namespace Insert_data_from_Gridview_database
{
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
protected void btn_show_Click(object sender, EventArgs e)
{
DataSet reportData = new DataSet();
reportData.ReadXml(Server.MapPath("student.xml"));
GridView1.DataSource = reportData;
GridView1.DataBind();
}
protected void btn_insert_Click(object sender, EventArgs e)
{
foreach (GridViewRow g1 in GridView1.Rows)
{
SqlConnection con = new SqlConnection(connStr);
com = new SqlCommand("insert into student(sid,sname,smarks,saddress) values ('" + g1.Cells[0].Text + "','" + g1.Cells[1].Text + "','" + g1.Cells[2].Text + "','" + g1.Cells[3].Text + "')", con);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
Label1.Text = "Records inserted successfully";
}
}
}
Thanks
If this post helps you mark it as answer
Rajkumar R175 reputationPosted Mar 4, 2012, 7:48 PM EST
Tables
student(sid,sname,smarks,saddress)
master_table(sid,sname,age,gender) these r the two tables
Here i want to check one more thing, i.e., if sid in XML file is not in the master_table i want to show those in different colour in the grid view while i click the show button.
Post Solution For this post
Thanks in Advance
Satyapriya NayakRank #47 · 31.5K reputationPosted Mar 5, 2012, 1:52 AM EST
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Insert_data_from_Gridview_database._Default" %>
using System;
using System.Collections;
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;
namespace Insert_data_from_Gridview_database
{
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
protected void btn_show_Click(object sender, EventArgs e)
{
DataSet reportData = new DataSet();
reportData.ReadXml(Server.MapPath("student.xml"));
GridView1.DataSource = reportData;
GridView1.DataBind();
}
protected void btn_insert_Click(object sender, EventArgs e)
{
foreach (GridViewRow g1 in GridView1.Rows)
{
SqlConnection con = new SqlConnection(connStr);
com = new SqlCommand("insert into student(sid,sname,smarks,saddress) values ('" + g1.Cells[0].Text + "','" + g1.Cells[1].Text + "','" + g1.Cells[2].Text + "','" + g1.Cells[3].Text + "')", con);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
Label1.Text = "Records inserted successfully";
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "sid")) == "")
{
e.Row.BackColor = System.Drawing.Color.Red;
}
else
{
e.Row.ForeColor = System.Drawing.Color.Black;
}
}
}
}
}
Thanks
If this post helps you mark it as answer