You must Sign In to post a response.
  • Category: ASP.NET

    How can bind the gridview in c#?


    Are you looking for the way to bind the GridView on search button using c#? Read this thread to get help.



    I want to bind the grid on search button. I am using 3 tier application. Please tell me how can bind the grid on search button click?
  • #724695
    Hi sania,

    Prepare stored procedure for bind the data to gridview. Here you pass a parameter called search for searching purpose and in your sp itself you just check that condition like below.



    CREATE PROCEDURE Get_Result
    (
    @Search VARCHAR(100)=NULL
    )
    AS
    BEGIN
    IF @Search IS NOT NULL
    BEGIN
    SELECT * FROM TABLE WHERE search=@Search
    END
    ELSE
    SELECT * FROM TABLE
    END
    END


    After that in your page itself you just bind your grid like below


    string Result;
    protected void Bind_GV()
    {
    BLayer obj=new BLayer();
    Result = TxtSearch.Text;
    DataSet ds=new DataSet();
    ds=obj.Get_Result(Result);
    GV.DataSource=ds;
    GV.DataBind();
    }


    Hope this information is helpful to you.
    If you have any doubt feel free to ask me..

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #724699
    Refer this sample code for binding the grid.

    If it is a 3 tier then use the reference DLL to call methods.


    SqlConnection con = new SqlConnection(ConnStr);
    string strQuery = "select Name from emp where Id>100";

    SqlDataAdapter ada = new SqlDataAdapter(strQuery, con);
    DataSet lds = new DataSet();
    ada.Fill(lds);
    gvSales.DataSource = lds;
    gvSales.DataBind();

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #724705
    i am getting an error in this line

    gvSales.DataBind();
    here its not showing me databind
    its give me databingings.

  • #724720
    Hi,

    Post your code and error description also ..

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #724743
    Hi,

    try like this code as per your requirement

    DataAcesss Layer

    using System.Data.SqlClient;
    using System.Data;

    public class DAL
    {

    string strconnection;
    SqlConnection sqlcon = new SqlConnection();
    SqlCommand sqlcmd = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    DataTable dt = new DataTable();
    public DAL()
    {

    }
    public void connect()
    {
    strconnection = ConfigurationManager.ConnectionStrings["Con"].ToString();
    sqlcon = new SqlConnection(strconnection);
    sqlcon.Open();
    }
    public void disconnect()
    {
    if (sqlcon.State == ConnectionState.Open)
    {
    sqlcon.Close();
    sqlcon.Dispose();
    }
    }
    public DataTable ReadData(string query)
    {
    try
    {
    connect();
    sqlcmd = new SqlCommand(query, sqlcon);
    da = new SqlDataAdapter(sqlcmd);
    dt = new DataTable();
    da.Fill(dt);
    }
    catch (Exception ex)
    {
    throw ex;
    }
    finally
    {
    disconnect();
    }
    return dt;
    }
    }


    Business access layer

    using System.Data;
    public class BAL
    {

    DataTable dt = new DataTable();
    public DataTable GetData(string query)
    {

    DAL obj = new DAL();
    try
    {
    return obj.ReadData(query)
    }
    catch
    {
    throw;
    }
    finally
    {
    obj = null;
    }
    }
    }


    code behind code

    BAL obj = new BAL();
    DataTable dt = new DataTable();
    protected void btnSearch_Click(object sender, EventArgs e)
    {
    dt = obj.GetData("select * from emp where eno='" + txteno.text + "'");
    if (dt.Rows.Count > 0)
    {
    Response.Redirect("Success.aspx");
    }
    else
    {
    Response.Write("Invalid Login");
    }
    }

    Regards
    N.Ravindran
    Your Hard work never fails

  • #724769
    hi

    Post Your Code
    try his code

    Your Dataset
    ===========
    ds = MasFunt.ListDs("test1");

    Your Query
    ==============
    Select * from Mas where Nams like '%aa%'

    Bind GridView
    =================
    GridView.DataSource = ds ;
    GridView.DataBind();

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.