How to use Gridview Control with database and paging in Gridview asp.net c#
In this article I will explain how to display records in Gridview control after insert into database and how to do paging with gridview. I have created database name dbemp and table name tbemp. Just copy and paste the following database script and run it on your sql server.
*set the gridview property AllowPaging=”True” and PageSize=”2″>
Or paste the source code . You can increase the page size according to your requirements
*you can use validations according to your need
Following the link in which I have described how to create database and table and how to make a connection with sql server.
*
DATABASE SCRIPT
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id =OBJECT_ID(N‘[dbo].[tbEmployee]‘) AND type in (N‘U’))
BEGIN
CREATE TABLE [dbo].[tbEmployee](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NULL,
[age] [int] NULL,
[gender] [varchar](50) NULL,
[salary] [int] NULL,
[designation] [varchar](50) NULL
) ON [PRIMARY]
END
*for Paging in gridview following the steps
Select the gridview press F4 key click on Event icon and the double click onPageIndexChanging
Following code will appear it and type the code in it.
OR paste the Source code.
SOURCE CODE
<%@ Page Language=”C#” AutoEventWireup=”true”CodeFile=”Default.aspx.cs” Inherits=”_Default” %>
<!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>
<style type=”text/css”>
.style1
{
width: 100%;
}
.style2
{
height: 30px;
}
</style>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<table class=”style1″>
<tr>
<td>
Name</td>
<td>
<asp:TextBox ID=”txt_name” runat=”server” Height=”22px”Width=”125px”></asp:TextBox>
</td>
</tr>
<tr>
<td>
Age</td>
<td>
<asp:TextBox ID=”txt_age” runat=”server”></asp:TextBox>
</td>
</tr>
<tr>
<td>
Salary</td>
<td>
<asp:TextBox ID=”txt_salary” runat=”server”></asp:TextBox>
</td>
</tr>
<tr>
<td valign=”top”>
Address</td>
<td>
<asp:TextBox ID=”txt_address” runat=”server” Height=”67px”TextMode=”MultiLine”
Width=”183px”></asp:TextBox>
</td>
</tr>
<tr>
<td class=”style2″>
</td>
<td class=”style2″>
<asp:Button ID=”btn_submit” runat=”server” Text=”Submit”
onclick=”btn_submit_Click” />
</td>
</tr>
<tr>
<td class=”style2″>
</td>
<td class=”style2″>
<asp:GridView ID=”GridView1″ runat=”server” AllowPaging=”True”
onpageindexchanging=”GridView1_PageIndexChanging” PageSize=”2″>
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
DESING VIEW
FOLLOWING THE CODE
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; // sqlconnection, sqlcommand, dataset, datatable
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection();
SqlCommand cmd;
DataTable dt;
SqlDataAdapter adp;
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString =ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
con.Open();
if (con.State == ConnectionState.Closed)
{
con.Open();
}
con.Close();
if (IsPostBack == false)
{
grdview(); // Call the grdview method
}}
// gridview function
private void grdview()
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
adp = new SqlDataAdapter(“select * from tbemp order by id desc”, con); // fetching the records from table.
dt = new DataTable();
adp.Fill(dt);
adp.Dispose();
if (dt.Rows.Count == 0)
{
Response.Write(“No record found”);
}
else
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
con.Close();
}
protected void btn_submit_Click(object sender, EventArgs e)
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
// insert into table tbemp
cmd = new SqlCommand(“insert into tbemp values(@ename,@eage,@esalary,@eaddress)”, con);
cmd.Parameters.AddWithValue(“@ename”, txt_name.Text);
cmd.Parameters.AddWithValue(“@eage”, txt_age.Text);
cmd.Parameters.AddWithValue(“@esalary”, txt_salary.Text);
cmd.Parameters.AddWithValue(“@eaddress”, txt_address.Text);
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
txt_clear(); //calling function name txt_clear() will clear the textfields
grdview();
}
private void txt_clear()
{
txt_name.Text = String.Empty;
txt_age.Text = String.Empty;
txt_salary.Text = String.Empty;
txt_address.Text = String.Empty;
Response.Write(“Record successfully submitted”);
}
protected void GridView1_PageIndexChanging(object sender,GridViewPageEventArgs e)
{
// for paging the gridview
GridView1.PageIndex = e.NewPageIndex;
grdview();
}
}
*Will post how can we edit and update the records using gridview control continue…
No comments:
Post a Comment