Overview:
This article explains how to bind table data(records) in a grid view using the SQL store procedure in asp.net c#. then bind the data field in the grid view and show selected columns. these following steps here.
Step 1 : Create a SQL table
USE [Demo]
GO
/****** Object: Table [dbo].[tblDemoInsert] Script Date: 13-09-2020 09:48:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tblDemoInsert](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](20) NULL,
[Address] [nvarchar](50) NULL,
CONSTRAINT [PK_tblDemoInsert] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
Step 2: Create a data-bind store procedure in SQL
USE [Demo]
GO
/****** Object: StoredProcedure [dbo].[prListInsertDemo]
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[prListInsertDemo]
AS
BEGIN
select Id,Name,Address from tblDemoInsert
END
Step 3 : Source Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DemoInsert.aspx.cs" Inherits="DemoInsert.DemoInsert" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="Address"></asp:Label>
<asp:TextBox ID="txtaddress" runat="server"></asp:TextBox>
<asp:Button ID="btnsave" runat="server" OnClick="btnsave_Click" Text="Save" />
<asp:Button ID="btnClear" runat="server" OnClick="btnClear_Click" Text="Clear" />
<br />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="Orange" AutoGenerateColumns="False" Width="426px">
<Columns>
<asp:BoundField DataField="Id" HeaderText="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Address" HeaderText="Address" />
</Columns>
</asp:GridView>
<br />
</div>
</form>
</body>
</html>
Step 4 : Write the following program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace DemoInsert
{
public partial class DemoInsert : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constring"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
BindGridview();
}
public void BindGridview()
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand("prListInsertDemo", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
public void ClearFields()
{
txtname.Text = "";
txtaddress.Text = "";
}
}
}
Step 5 : Design View
Step 6 : After Running View
More Details:
Watch this video:
VIDEO
https://www.youtube.com/watch?v=ysgYv3aGoa4
No comments:
Post a Comment