German Wear Discount Shop - Click Here Write for Dotnet-friends and earn for your each submission [Dot]Net-Friends
Skip Navigation Links
Home
Latest
Fast Code
Articles
Tutorials
Online Resources
Forums
Login   | Hi, Guest


Multiple ResultSets in ADO.NET with Multiple DataGridViews in ASP .NET 2.0

Written by omerkamal on Dec 29, 2006
We will have a look on ADO .NET capabillity of multiple execution of TSQL

Introducation:

Its works almost the same way you get single result or Multiple results from you Sql Quries. But, some time we really need to have compact return of Data Tables, just on the cost of single connection to the DataBase.

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.

Visitors/Readers Comments
(for questions please use The Forum)



Anoop Agrawal

very helpful and inovative for me.

good !

16/04/2008 22:51:54 UTC




Add your Comments

Name:  
Message:
Note: For faster response please use Forums >> for your questions instead of the comments area! (Admin)