Home > ASP.Net > insert multiple records at a time with Gridview

insert multiple records at a time with Gridview


try this example:

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="insertusinggridview.aspx.cs" Inherits="GRidviews_insertusinggridview" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    No Of Records : <asp:TextBox ID="txtNo" runat="server"></asp:TextBox> <asp:Button ID="btnSubmit" runat="server"  Text="Submit" OnClick="btnSubmit_Click"/>
    <br />
    <asp:GridView ID="GridView1" runat="server">
            <asp:TemplateField HeaderText="Name">
                      <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
            <asp:TemplateField HeaderText="No">     
                      <asp:TextBox ID="txtNo" runat="server"></asp:TextBox>
    <asp:Button ID="btnInsert" runat="server" Text="Insert" OnClick="btnInsert_Click" />

in code behind :-

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class GRidviews_insertusinggridview : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)

    protected void btnSubmit_Click(object sender, EventArgs e)
        int[] nos = new int[Convert.ToInt32(txtNo.Text)];
        for (int i = 0; i < nos.Length; i++)
            nos[i] = i+1;
        GridView1.DataSource = nos;
    protected void btnInsert_Click(object sender, EventArgs e)
        foreach (GridViewRow gvRow in GridView1.Rows)
            TextBox txtName = (TextBox)gvRow.FindControl("txtName");
            TextBox txtNo = (TextBox)gvRow.FindControl("txtNo");
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["masterconnectionstring2"].ConnectionString);
            SqlCommand cmd = new SqlCommand("insert into Names values (@Name,@No)" ,con);
            cmd.Parameters.Add("Name", txtName.Text);
            cmd.Parameters.Add("No", txtNo.Text);


Good Luck

Categories: ASP.Net Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: