11 Aug 2017

How to bind GridView in asp.net with c# using Dataset

 (gridview_demo) you want to show selected column show in grd view then.


Step 1.  Data base code-

create database databasename

use databasename

create table Employee
(
Empid int primary key identity(1,1),
Name varchar(50),
Age int,
Address varchar(50),
Password varchar(50),
CPassword varchar(50),
Phone varchar(11),
Salary int
)

select * from Employee

create proc usp_employee_insert
@Name varchar(50),
@Age int,
@Address varchar(50),
@Password varchar(50),
@CPassword varchar(50),
@Phone varchar(11),
@Salary int
as
begin
insert into Employee(Name,Age,Address,Password,CPassword,Phone,Salary) values(@Name,@Age,@Address,@Password,@CPassword,@Phone,@Salary)
end

Grid Bind Procedure-

create proc usp_employeeget
as
begin
select * from Employee
end

step2. axps page code -

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

<!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>Name:</td>
            <td><asp:TextBox ID="TextName" runat="server"></asp:TextBox></td>
        </tr>

         <tr>
            <td>Age:</td>
            <td><asp:TextBox ID="TextAge" runat="server"></asp:TextBox></td>
        </tr>

         <tr>
            <td>Address:</td>
            <td><asp:TextBox ID="TextAddress" runat="server"></asp:TextBox></td>
        </tr>

         <tr>
            <td>Password:</td>
            <td><asp:TextBox ID="TextPassword" runat="server"></asp:TextBox></td>
        </tr>

        <tr>
            <td>CPassword:</td>
            <td><asp:TextBox ID="TextCPassword" runat="server"></asp:TextBox></td>
        </tr>

        <tr>
            <td>Phone:</td>
            <td><asp:TextBox ID="TextPhone" runat="server"></asp:TextBox></td>
        </tr>

        <tr>
            <td>Salary:</td>
            <td><asp:TextBox ID="TextSalary" runat="server"></asp:TextBox></td>
        </tr>

        <tr>
            <td></td>
            <td><asp:Button ID="btnsave" runat="server" Text="Save" OnClick="btnsave_Click" /></td>
        </tr>

        <asp:GridView ID="grd" runat="server" AutoGenerateColumns="false" OnRowCommand="grd_RowCommand">
            <Columns>
                <asp:TemplateField HeaderText="Emp Name">
                    <ItemTemplate>
                        <%#Eval("Name") %>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Emp Age">
                    <ItemTemplate>
                        <%#Eval("Age") %>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Emp Address">
                    <ItemTemplate>
                        <%#Eval("Address") %>
                    </ItemTemplate>
                </asp:TemplateField>


                <asp:TemplateField HeaderText="Emp Phone">
                    <ItemTemplate>
                        <%#Eval("Phone") %>
                    </ItemTemplate>
                </asp:TemplateField>


                <asp:TemplateField HeaderText="Emp Salary">
                    <ItemTemplate>
                        <%#Eval("Salary") %>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </table>
    </div>
    </form>
</body>
</html>

step3. Project .cs page code.

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 Registration2
{
    public partial class Registration : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                fill_grd();
            }
        }
        public void fill_grd()
        {
            SqlCommand cmd = new SqlCommand("usp_employeeget", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                grd.DataSource = ds;
                grd.DataBind();
            }
            else
            {
                grd.DataSource = null;
                grd.DataBind();
            }

        }


        protected void btnsave_Click(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("usp_employee_insert", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Name", TextName.Text);
            cmd.Parameters.AddWithValue("@Age", TextAge.Text);
            cmd.Parameters.AddWithValue("@Address", TextAddress.Text);
            cmd.Parameters.AddWithValue("@Password", TextPassword.Text);
            cmd.Parameters.AddWithValue("@CPassword", TextCPassword.Text);
            cmd.Parameters.AddWithValue("@Phone", TextPhone.Text);
            cmd.Parameters.AddWithValue("@Salary", TextSalary.Text);
            cmd.ExecuteNonQuery();
            con.Close();
            fill_grd();
        }

        protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }
    }
}
Step.4. Config  file connection string web.config file

<?xml version="1.0"?>

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

<configuration>
    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>
<connectionStrings>
  <add name="DBCS" connectionString="data source=Shiva;integrated security=true;initial catalog=databasename"/>
</connectionStrings>

</configuration>







No comments:
Write comments

Contact Form

Name

Email *

Message *

© 2014 Rock Programmer . Designed by Bloggertheme9
Powered by Rock Programmer.