At first you need to have the proper database.
Take the example of Northwind Database.
Link:Install Microsoft Northwind and Pubs Sample databases in SQL Server Management Studio
Now If you want to see the Customers Orders By his CustomerId then you need to write the query this way.
This is a test Query.
DECLARE @CustomerId VARCHAR(30)
SET @CustomerId = 'ALFKI'
SELECT OrderId, OrderDate
FROM Orders WHERE CustomerId = @CustomerId
You need to Bind the GridView this way
HTML
<asp:TextBox ID="txtCustomerId" runat="server" Width="140" />
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="OrderId" HeaderText="OrderId" ItemStyle-Width="30" />
<asp:BoundField DataField="OrderDate" HeaderText="OrderDate" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
Namespaces
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT OrderId, OrderDate FROM Orders WHERE CustomerId = @CustomerId"))
{
cmd.Parameters.AddWithValue("@CustomerId", this.txtCustomerId.Text.Trim());
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}