1 .
Drag and drop the grid view in the page
2 .
View on the code
And
Write the connection code just after the
start of the partal class and above the page load method and keep its scope as
global
SqlConnection cn = new
SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
SqlCommand cmd = new
SqlCommand();
Here we make an sqlconnection to connect
our application with the database
We have to find the path from web.config
file so
We write ConfigurationManager this states that
the config file and then we have to find the connetionstring do we have written
ConnectionStrings
[] denotes index or name of the string because we
can place as many as we want connection string in config file so in bracket
write the name of the connection string in my case it is testConnectionString
then we have to select connection string of that name so
.connectionstring
Now we make an object of the sqlcommand
For that we need to import the namespace
using System.Configuration;
for ConfigurationManager
using System.Data.SqlClient;
for sqldatabase related
object
now write the code in the page load
cn.Open();
cmd.Connection = cn;
cmd.CommandText = "select * from
p12";
SqlDataAdapter adp = new
SqlDataAdapter();
adp.SelectCommand = cmd;
DataSet ds = new DataSet();
adp.Fill(ds);
gv.DataSource = ds;
gv.DataBind();
cn.Close();
cn.Open();
we have open an connection
cmd.Connection = cn;
give
a connection to the command
cmd.CommandText = "select * from
p12";
specify
the text or query which we want to perform
SqlDataAdapter adp = new
SqlDataAdapter();
Create
an dataadapter object to adapt the data which is return by the query
adp.SelectCommand = cmd;
specify
the command for adapter
DataSet ds = new DataSet();
Create
a virtual dataset
adp.Fill(ds);
the
data which is adapted by the adapter if filled in the dataset
gv.DataSource = ds;
gives
datasourse to gridview
gv.DataBind();
databind()
is userd to bind the data of the dataset to the gridview
cn.Close();
close
the connection after finish the task
now run the page and you will see the data over there in the browser
No comments:
Post a Comment