This article shows how to get data from a database using select ids from a dropdown list, where you select the ids from the dropdown list and when you click on the button you get the id's necessary information (well I include only name and city :P).
INITIAL CHAMBER
Step 1
Open Your Visual Studio 2010 and create an empty website. Provide a suitable name (gridview_demo).
Step 2
In Solution Explorer you get your empty website, then add a Web Form and SQL Server Database. By going like this.
For Web Form:
gridview_demo (your empty website) then right-click then select Add New Item -> Web Form. Name it as -> gridviewid_demo.aspx.
For SQL Server Database:
gridview_demo (your empty website) then right-click then select Add New Item -> SQL Server database. (Add the database inside the App_Data_folder).
DATABASE CHAMBER
Step 3
In Server Explorer, click on your database (Database.mdf) then select Tables -> Add New Table. Make the table like this.
Table -> tbl_data (Don't forget to make the ID as IS Identity -- True.)
Figure 1: Data Table
Make some entries in the database by going to Table -> tbl_data then right-click then Show Table Data. Don't copy my entries, make yours something else.
Figure 2: Database
DESIGN CHAMBER
Step 4
Now make some design for your application by going to gridviewid_demo.aspx and try the code like this.
Gridviewid_demo.aspx
Your design will look like this:
Figure 3: Get Data from database
CODE CHAMBER
Step 5
Now it's time for server-side coding so that our application works. Open your gridviewid_demo.aspx.cs file and code it as in the following.
Gridviewid_demo.aspx.cs
Don't forget the namespaces as in the following:
Figure 4: Namespace
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- using System.Data.SqlClient;
- public partial class _Default: System.Web.UI.Page {
- protected void Page_Load(object sender, EventArgs e) {
- if (!Page.IsPostBack) {
- SqlConnection con = new SqlConnection(@
- "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
- SqlCommand cmd = new SqlCommand("select * from tbl_data", con);
- SqlDataAdapter sda = new SqlDataAdapter(cmd);
- DataTable dt = new DataTable();
- sda.Fill(dt);
- DropDownList1.DataSource = dt;
- DropDownList1.DataBind();
- }
- }
- protected void Button1_Click(object sender, EventArgs e) {
-
- SqlConnection con = new SqlConnection(@
- "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
- SqlCommand cmd = new SqlCommand("select * from tbl_data where id=" + DropDownList1.SelectedItem.Value, con);
- SqlDataAdapter sda = new SqlDataAdapter(cmd);
- DataTable dt = new DataTable();
- sda.Fill(dt);
- GridView1.DataSource = dt;
- GridView1.DataBind();
- }
- }
OUTPUT CHAMBERFigure 5: Output
Check out the database, that who is ID=2. We will get ID=2 (purnima's data in GridView).
Figure 6: Get Data
Figure 7: Complete output
I hope you Like it, thank you for reading. Have a good day.