ASP.NET Gridview control example | Using Gridview

Needed Stored Procedure

create procedure sp_GetAllCandidateRecords
as
begin
       select CandName,EmailID,DateOfBirth as DOB,Age,JoinDate 
       from tabCandRegistration


end

web.config file

<?xml version="1.0" encoding="utf-8"?>

<!-- For more information on how to configure your ASP.NET application, please visit https://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
  <connectionStrings>
    <add name="SampleDBConStr" 
         connectionString="Data Source=PC377553;
          Initial Catalog=SampleDB;
          Integrated Security=True;"/>
  </connectionStrings>

  <system.web>
    <compilation debug="true" targetFramework="4.5.2"/>
    <httpRuntime targetFramework="4.5.2"/>
  </system.web>
  <system.codedom>
    <compilers>
    </compilers>
  </system.codedom>
</configuration>

RecordDisplay.aspx 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RecordDisplay.aspx.cs" Inherits="WebDemo01.RecordDisplay" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>
                        <h3>Candidate Records</h3>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:GridView ID="GridView1" 
                               runat="server" Width="600px">
                        </asp:GridView>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

RecordDisplay.aspx.cs

using System;
//added...
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace WebDemo01
{
    public partial class RecordDisplay : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
                DisplayCandidateRecords();
        }

        private void DisplayCandidateRecords()
        {
            //Step1 (Connection estab)
            string conStr = ConfigurationManager
                   .ConnectionStrings["SampleDBConStr"].ConnectionString;
            SqlConnection con = new SqlConnection(conStr);
            con.Open();

            //step2 (Initialize command object with a query)
            SqlCommand cmd = new SqlCommand
                            ("sp_GetAllCandidateRecords", con);
            cmd.CommandType = CommandType.StoredProcedure;

            //Step3 (Initialize adapter with cmd object)
            SqlDataAdapter da = new SqlDataAdapter(cmd);

            //Step4: Fill dataset
            DataSet ds = new DataSet();
            da.Fill(ds);

            //Step5 : populate Gridview with records...
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
    }
}

No comments:

Post a Comment