简体   繁体   中英

How to retrieve and display values in GridView from SQL Server database in ASP.NET using C#

I am creating a web application in Microsoft Visual Studio 2010. I want to retrieve data from a SQL Server database and display it in a GridView in ASP.NET using C#.

I want to retrieve data on button onclick event.

protected void submit_click(object sender, EventArgs e) {

connection1.open();

SqlCommand sq= new SqlCommand("select student_id, student_name from student);

SqlDataReader dr= new SqlDataReader();

...

// Gridview

}

Refer for description: Microsoft Visual Studio-how-to-retrieve-and-display-values-from-database-in -ASP.NET-using-C#.

protected void Button1_Click ( object sender, EventArgs e) {

//connection string

SqlConnection con = new SqlConnection ("Data Source=.\SQLEXPRESS; AttachDbFilename='C:\Users\HP\Documents\Visual Studio 2010\Projects \assignment\assignment\App_Data\pokedex.mdf';Integrated Security=True; User Instance=True");

con.Open();

SqlCommand cmd = new SqlCommand ("Select * from pokedex;");

SqlDataAdapter sda = new SqlDataAdapter (cmd);

DataTable dt = new DataTable ();

sda.Fill(dt);

GridView1.DataSource = dt;

GridView1.DataBind();

con.Close();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM