Click here to Skip to main content
11,580,580 members (71,591 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: .NET2.0 C#2.0
This below code i had followed to import XML data in to DB.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Insert_data_from_Gridview_database._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" >
<head  runat="server">
    <title>Untitled Page</title>
    <form id="form1"  runat="server">
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <asp:GridView ID="GridView1" runat="server">
    <asp:Button ID="btn_insert" runat="server" onclick="btn_insert_Click" 
        Text="Insert Records" />
    <asp:Button ID="btn_show" runat="server" onclick="btn_show_Click" 
        style="height: 26px" Text="Show Records" />

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();
            GridView1.DataSource = reportData;
        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);
            Label1.Text = "Records inserted successfully";
One more Help in this.

student(sid,sname,smarks,saddress) //Here we insert from XML to DB
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 means then 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
Posted 4-Mar-12 22:28pm
Edited 5-Mar-12 17:56pm
Nithin Sundar at 5-Mar-12 23:56pm
Changed tags from C# and VB .NET to C# and .NET.
saqibrajput at 15-Aug-12 21:09pm
We Show data in Grid view as String but we insert data in database as int(ID). HOW??

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

just use select query in sqlcommand.
and use dataReader.
sqldatareader dr = cmd.executereader(); //(cmd is object of Sqlcommand)
if (
//use your code here

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 1,088
1 Sergey Alexandrovich Kryukov 750
2 Abhinav S 638
3 Suvendu Shekhar Giri 433
4 F-ES Sitecore 420

Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 4 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100