Micro Niche Builder
Closed Thread
Results 1 to 8 of 8

Thread: How to bind data to grid?

  1. #1

    Default How to bind data to grid?

    I am using asp.net with c#, I am trying to bind data to a datagrid but unable to do so. Can anyone tell me how can I bind data to a datagrid? I am using asp.net and coding in c# and I am usign mssql server 2000 as a database server.

    Thanks in advance.


  2. #2

    Default

    to bind data to a datagrid, use follwing code :

    firstly import the 2 namespace :-
    using system.data
    using system.data.sqlclient

    and the write following code in the page load event : -
    page_load()
    {
    dim cn as new sqlconnection("server=;uid=;pwd=;database;")
    dim adp as new sqldataadapter("select * from tablename",cn)
    dim ds as new dataset
    adp.fill(ds)
    gridview1.datasource=ds.tables(0)
    gridview1.databind()
    }

  3. #3

    Default

    through the databind property of the grid in asp.net.
    follow above code:
    imports system.data.sqlclient
    dim system.data
    public void form_load
    dim cn as new sqlconnnection("connection string")
    dim adp as new sqldataadapter("select * from services",cn)
    dim ds as new dataset
    adp.fill(ds)
    datagrid1.datasourse=ds.tables("scr")
    datagrid1.databind()

  4. #4

    Talking

    Quote Originally Posted by Amar View Post
    to bind data to a datagrid, use follwing code :

    firstly import the 2 namespace :-
    using system.data
    using system.data.sqlclient

    and the write following code in the page load event : -
    page_load()
    {
    dim cn as new sqlconnection("server=;uid=;pwd=;database;")
    dim adp as new sqldataadapter("select * from tablename",cn)
    dim ds as new dataset
    adp.fill(ds)
    gridview1.datasource=ds.tables(0)
    gridview1.databind()
    }
    hey i tried the things which you have posted to the nikon..
    but not getting satisfied answer for that...
    is there any mistake from your side or might be made by me but still finding solution for that....

  5. #5

    Default

    Follow this code:
    void Page_Load(object sender, EventArgs e)
    {

    if (!Page.IsPostBack)
    {

    // Binds the data

    BindData();

    }

    }

    public void BindData()
    {

    // you can use this line

    string connectionString = myDataSource.ConnectionString;

    // you can also use this line since by using the SqlDataSource it makes an entry in the
    // web.config file if that is what choose to do
    //string connectionString = (string)ConfigurationSettings.AppSettings["ConnectionString"];
    // if you are using Enterprise Library you can also use
    //string connectionString = (string)ConfigurationSettings.ConnectionStrings["ConnectionString"];

    SqlConnection myConnection = new SqlConnection(connectionString);

    SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Categories", myConnection);

    DataSet ds = new DataSet();

    ad.Fill(ds, "Categories");

    myGridView.DataSource = ds;

    myGridView.DataBind();

    }

  6. #6

    Default

    For databinding with datagirdview there is a method named databind(). Simply use it with your gridview.

  7. #7

    Default

    You should specify more details too. Which database you are using for connectivity and are you using c# or vb.net?

  8. #8

    Default

    You can bind data using this method....

    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!Page.IsPostBack)
    {
    BindData();
    }
    }


    As you see the BindData method is called when the page is not posted back. Now lets see the BindData method in details. public void BindData()
    {
    SqlCommand myCommand = new SqlCommand("SP_SELECT_PERSONS",myConnection);
    myCommand.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
    DataSet ds = new DataSet();
    myAdapter.Fill(ds,"tblPerson");
    myConnection.Open();
    myCommand.ExecuteNonQuery();
    myDataGrid.DataSource = ds;
    myDataGrid.DataBind();
    myConnection.Close();
    }

Closed Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts