You need to get the data into the UI. The GridView control makes this easy. It can automatically generate an HTML table based on data that is bound to it. We're going declare a GridView in the markup, select our data into a DataTable, then bind the DataTable to the GridView.
<asp:GridView runat="server" id="CustomerDetailsGV" AutoGenerateColumns="true" />
Code behind:
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
string detailsQuery = "select * FROM [Customer] where Customer_No ='" + Session["New"] + "'";
SqlCommand com = new SqlCommand(detailsQuery, conn);
DataTable dt = new DataTable();
dt.Load(com.ExecuteReader());
CustomerDetailsGV.DataSource = dt;
CustomerDetailsGV.DataBind();
conn.Close();
You should also wrap your SqlConnection in a using statement.
string detailsQuery = "select * FROM [Customer] where Customer_No ='" + Session["New"] + "'";
SqlCommand com = new SqlCommand(detailsQuery);
DataTable dt = new DataTable();
using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)
{
com.Connection = conn;
conn.Open();
dt.Load(com.ExecuteReader());
}
CustomerDetailsGV.DataSource = dt;
CustomerDetailsGV.DataBind();
You may find this a useful function to keep around:
/// <summary>
/// Executes a database command with the specified connection and returns a data table synchronously.
/// </summary>
/// <param name="command">The command to execute.</param>
/// <param name="connection">The connection to use.</param>
/// <returns>A DataTable representing the command results.</returns>
public static DataTable GetDataTable(SqlCommand command, SqlConnection connection)
{
DataTable dt = new DataTable();
command.Connection = connection;
using (connection)
{
connection.Open();
dt.Load(command.ExecuteReader());
}
return dt;
}
Using the above function, your code can be simplified to this:
string detailsQuery = "select * FROM [Customer] where Customer_No ='" + Session["New"] + "'";
SqlCommand com = new SqlCommand(detailsQuery);
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
CustomerDetailsGV.DataSource = GetDataTable(com, con);
CustomerDetailsGV.DataBind();