Following code sample helps to add new record into GridView and also in DataTable without modifying datatabase's content.
With the help of "sqlbukcopy" all the newly added record in DataTable are inserted into table.
Here newly added record are identified by DataTable.GetChanges(DataRowState.Added) method.
Design Part should look as follows:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
<!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:Panel ID="pnlgrid" runat="server" GroupingText=" Grid data "> <table> <tr> <td colspan="5"> <asp:GridView ID="grdview1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" onrowcreated="grdview1_RowCreated" style="position: relative; top: 0px; left: 47px" onpageindexchanging="grdview1_PageIndexChanging"> <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#999999" /> <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> <Columns> <asp:TemplateField> </asp:TemplateField> </Columns> </asp:GridView></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td> </td> <td> <asp:Button ID="btnadd" runat="server" onclick="btnadd_Click" style="position: relative; top: 0px; left: 13px" Text="InsertRecord" /> <asp:Button ID="btndbadd" runat="server" onclick="btndbadd_Click" Text="Add to Db" style="position: relative; top: 0px; left: 16px" /> </td> <td></td> <td></td> </tr> <tr> <td> <asp:Label ID="Label1" runat="server" Text="Firstname"></asp:Label> </td> <td> <asp:TextBox ID="txtname" runat="server"></asp:TextBox> </td> <td></td> <td></td> </tr> <tr> <td> <asp:Label ID="Label2" runat="server" Text="Empaddress"></asp:Label> </td> <td colspan="3"> <asp:TextBox ID="txtaddress" runat="server" Height="22px" ontextchanged="TextBox2_TextChanged" Width="306px"></asp:TextBox> </td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> </tr> </table> </asp:Panel> </div> </form> </body> </html>
The code behind for the above .aspx page is :
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; using System.IO;
public partial class _Default : System.Web.UI.Page { SqlConnection con; SqlCommand cmd; string constr = "Data Source=D-1058\\SQLEXPRESS;Initial Catalog=Task;Integrated Security=True"; DataTable dt = new DataTable(); DataTable dt2 = new DataTable(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { doload(); grdview1.DataSource = Session["data"]; grdview1.DataBind(); dt = (DataTable)Session["data"]; } } public void doload() { con = new SqlConnection(constr); con.Open(); cmd = new SqlCommand("Select * from testTable", con); SqlDataReader dr = cmd.ExecuteReader(); dt.Load(dr); Session["data"] =dt ; Session["index"] = 7;//here i am hard coding id value for this example, it is not nice practise con.Close();
}
protected void btnadd_Click(object sender, EventArgs e) { dt = (DataTable)Session["data"]; DataRow dr = dt.NewRow();
dr["Sno"] = Session["index"]; dr["Fname"] = txtname.Text; dr["Address"] = txtaddress.Text; dt.Rows.Add(dr); dt2 = dt.GetChanges(DataRowState.Added); Session["data1"] = dt2; Session["data"] =(DataTable)dt; grdview1.DataSource = Session["data"]; grdview1.DataBind(); Session["index"] = Convert.ToInt32(Session["index"]) + 1; txtname.Text = ""; txtaddress.Text = ""; Session["newdata"] = (DataTable)dt2; } protected void btndbadd_Click(object sender, EventArgs e) { //string connection = ConfigurationManager.AppSettings["conString"]; SqlBulkCopy copy = new SqlBulkCopy(constr); //insert(); dt2=(DataTable)Session["newdata"]; copy.DestinationTableName = "testTable"; copy.WriteToServer(dt2); } protected void grdview1_PageIndexChanging(object sender, GridViewPageEventArgs e) { grdview1.PageIndex = e.NewPageIndex; } }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|