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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Anupam SinghRank #643 · 3.7K reputationPosted May 22, 2014, 4:18 AM EDT
try this :
SqlCommand cmd = new SqlCommand("select * from tablename",con);
hope it helps
Bhabani PrasadRank #8,369 · 223 reputationPosted May 22, 2014, 4:26 AM EDT
{
// 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();
}
}