Monday, October 13, 2014

Bind Gridview in Asp c#. Net

Introduction

Here I will explain how to implement Grid binding in c#.net. 
We will work in 3 tier architecture.

Description
 
In this articles we will learn binding of Gridview with dataset/datatable. Gridview control is the successor to the DataGrid and extends it in a number of ways. With this Gridview control, you could display an entire collection of data, and easily add sorting, paging and inline editing.


Solution:

Create a .cs file which will interact with database (Data Access Layer DAL), create another .cs file where all variables are defined/declared and logic (Business Logic Layer BLL), and last and least is your .aspx page where you can interact with your controls and bind those controls with DAL and BAL.

Below is Data Access Layer class DAL.cs:-


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Globalization;

public class DAL
{
    SqlConnection sqlcon;
    SqlCommand sqlcmd;
    SqlDataAdapter sqlad;
    DataSet dsDetails;
    DataTable dt;

//below connection string saved in web.config file and accessing this to .cs
public static string DatabaseCredientials = ConfigurationManager.ConnectionStrings["XYZ"].ToString();


public DataSet userdetails()
    {
        try
        {
            sqlcon = new SqlConnection(DatabaseCredientials);
            sqlcon.Open();

            sqlcmd = new SqlCommand();
            sqlcmd.Connection = sqlcon;
//here you can give store procedure name or directly text of sql Query.
            sqlcmd.CommandType = CommandType.Text;
            sqlcmd.CommandText = "SELECT * FROM UserDetails;
            dsDetails = new DataSet();
            sqlad = new SqlDataAdapter(sqlcmd);
            sqlad.Fill(dsDetails);


        }
        catch (Exception e)
        {
            throw e;

        }
        finally
        {

        }
        return dsDetails;
    }
}

Below is Business Logic Layer class BLL.cs :-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Summary description for BLL
// It contains, your variables declaration with get set property and logic.
/// </summary>

public class BLL
{

    public DataSet getUserDetails()
    {
        DAL Databaselayer = new DAL();
        
        try
        {
            return Databaselayer.userdetails();
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            Databaselayer = null;
        }
    }
}

Below is .aspx page which will interact with BLL and DAL :-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.IO;
using System.Text;
using System.Xml;
using System.Data;
using System.IO;
using System.Globalization;
using System.Web.Configuration;
using System.Net.Mail;
using System.Data.SqlClient;
using System.Configuration;

public partial class MasterUC_BackLogTask : System.Web.UI.UserControl
{

   DataSet ds;
   BLL BusinessLogicLayer;

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
             GridBind();
        }
     }
  #region grid binding
    public void GridBind()
    {
        BusinessLogicLayer = new BLL();

     
        ds = new DataSet();
        ds = BusinessEntityLayer.getUserDetails();

        griduser.DataSource = dt;
        griduser.DataBind();
    }
    #endregion
}

Finally F5 for debug result shown below :