<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!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> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True" PageSize="5" onpageindexchanging="GridView1_PageIndexChanging" > <columns> <asp:CommandField ShowSelectButton="True" ShowEditButton="True" /> <asp:BoundField DataField="Id" HeaderText="Id" /> <asp:BoundField DataField="Name" HeaderText="Name" /> <asp:BoundField DataField="Phone_No" HeaderText="Phone_No" /> </columns> <table> <tr> <td> <asp:Label ID="Label5" runat="server" Text="Id"> </td> <td> <asp:TextBox ID="TextBox1" runat="server"> </td> </tr> <tr> <td> <asp:Label ID="Label1" runat="server" Text="Name"> </td> <td> <asp:TextBox ID="TextBox2" runat="server"> </td> </tr> <tr> <td> <asp:Label ID="Label2" runat="server" Text="Phone No"> </td> <td> <asp:TextBox ID="TextBox3" runat="server" > </td> </tr> <tr> <td colspan="2" align="center"> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add" /> </td> </tr> <tr> <td colspan="2" align="center"> </td> </tr> </table> </div> </div> </form> </body> </html>
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.Collections; using System.Data.SqlClient; public partial class Default2 : System.Web.UI.Page { DataSet ds; SqlConnection conn = new SqlConnection(); SqlCommand cmd = new SqlCommand(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { fillgrid(); } } public void fillgrid() { conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["conname1"].ConnectionString; conn.Open(); SqlCommand cmd = new SqlCommand("select * from Crud", conn); SqlDataAdapter da = new SqlDataAdapter(cmd); ds = new DataSet(); da.Fill(ds, "Crud"); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); conn.Close(); } protected void Button1_Click(object sender, EventArgs e) { conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["conname1"].ConnectionString; conn.Open(); cmd.Connection = conn; cmd.CommandText = "INSERT INTO Crud (Id,Name,Phone_No) VALUES('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')"; cmd.ExecuteNonQuery(); conn.Close(); fillgrid(); //DataTable dtable = new DataTable(); //dtable.Columns.Add(new DataColumn("Id")); //dtable.Columns.Add(new DataColumn("Name")); //dtable.Columns.Add(new DataColumn("Phone_No")); //object[] RowValues = { "", "", "" }; //RowValues[0] = TextBox1.Text; //chaged here.... //RowValues[1] = TextBox2.Text; //RowValues[2] = TextBox3.Text; //DataRow dRow; //dRow = dtable.Rows.Add(RowValues); //dtable.AcceptChanges(); //GridView1.DataSource = dtable;//added here.... //GridView1.DataBind(); } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; fillgrid(); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)