How To get data from database into datatable ?
how we get data from database and showing it into a gridview?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Bhabani PrasadPosted May 22, 2014, 8:26 AM
{
// Get the connection string from Web.config.
// When we use Using statement,
// we don't need to explicitly dispose the object in the code,
// the using statement takes care of it.
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"].ToString()))
{
// Create a DataSet object.
DataSet dsPerson = new DataSet();
// Create a SELECT query.
string strSelectCmd = "SELECT PersonID,LastName,FirstName FROM Person";
// Create a SqlDataAdapter object
// SqlDataAdapter represents a set of data commands and a
// database connection that are used to fill the DataSet and
// update a SQL Server database.
SqlDataAdapter da = new SqlDataAdapter(strSelectCmd, conn);
// Open the connection
conn.Open();
// Fill the DataTable named "Person" in DataSet with the rows
// returned by the query.new n
da.Fill(dsPerson, "Person");
// Get the DataView from Person DataTable.
DataView dvPerson = dsPerson.Tables["Person"].DefaultView;
// Bind the GridView control.
gvPerson.DataSource = dvPerson;
gvPerson.DataBind();
}
}
Anupam SinghPosted May 22, 2014, 8:18 AM
try this :
SqlCommand cmd = new SqlCommand("select * from tablename",con);
hope it helps