Explanation:
This way we reduce the number of connections to our Database and also reduce the cost of executions on the web and data server.
Let us see our Three Gridviews;
Our .aspx Page Code:
For Design purpose of course we can change the location and fix the sizes according to our needs. Here, this is a secoundry activity in our main concept Explanation.
Now lets see how we connect to the Database;
Our Page behind code:
using (SqlConnection connection =
new SqlConnection(ConfigurationManager.ConnectionStrings["webConfigConnectionstringhere"].ConnectionString))
{
string Query= "SELECT * FROM [Forums]; SELECT * FROM [Topics]; SELECT * FROM [TopicViews]";
SqlCommand command = new SqlCommand(Query, connection);
command.CommandType = CommandType.Text;
connection.Open();
SqlDataReader reader = command.ExecuteReader();
DataTable Table1 = new DataTable("Table1");
Table1.Load(reader);
GridView1.DataSource = Table1;
GridView1.DataKeyNames = new string[] { "ForumID" };
GridView1.DataBind();
DataTable Table2 = new DataTable("Table2");
Table2.Load(reader);
GridView2.DataSource = Table2;
GridView2.DataKeyNames= new string[] {"TopicID"};
GridView2.DataBind();
DataTable Table3 = new DataTable("Table3");
Table3.Load(reader);
GridView3.DataSource = Table3;
GridView3.DataKeyNames = new string[] { "ViewID" };
GridView3.DataBind();
}
Our TSQL Queries are separated by " ; " . After execution of the reader we use use the Primary kies of each Table as "DataKeyNames" . You also noticed that we used DataTable and DataTable.Load() object and its function. This is new in ASP .NET 2.0 earlier Developers were forced to use Dataset instead.
Any type of Question would be welcome.
|