Question -
I need a example of how to create a gridview in .cs versus just dragging and dropping the gridview on the aspx page?
I created a dataset and called a stored procedure using data adapter, but i do not know how to data bind the data set into the grid view I have.
It seems everywhere I search the gridview examples they have is just dragging and dropping to the page and configuring a datasource.
Answer - I created a dataset and called a stored procedure using data adapter, but i do not know how to data bind the data set into the grid view I have.
It seems everywhere I search the gridview examples they have is just dragging and dropping to the page and configuring a datasource.
//connect to database via dataset & dataadapte
SqlConnection con = new SqlConnection("server=localhost;Initial Catalog=databasename;uid=;pwd=;");
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("stored proc name", con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.Fill(ds);
//create gridview by code & bind dataset to it
GridView g = new GridView();
Page.Controls.Add(g);
g.DataSource = ds;
Page.DataBind();
SqlConnection con = new SqlConnection("server=localhost;Initial Catalog=databasename;uid=;pwd=;");
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("stored proc name", con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.Fill(ds);
//create gridview by code & bind dataset to it
GridView g = new GridView();
Page.Controls.Add(g);
g.DataSource = ds;
Page.DataBind();
How to use this code for connect to remorte database server.
ReplyDeleteIs very easy,
ReplyDeleteSqlConnection con = new SqlConnection("server=localhost;Initial Catalog=databasename;uid=;pwd=;");
set your database sever address for server property.