c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
ADO.NET

How to Load two tables into a dataset and bind to gridview in asp.net c#?

| | ASP-NET , CSharp

In this article we will discuss, How to Load two tables into a dataset and bind to gridview in asp.net c#. We want to show employee data in one gridview and prductdetail to another gridview using a collection property of dataset object.

For tables and code refer the below link:

http://www.infinetsoft.com/Post/How-to-retrieving-two-or-more-result-sets-using-SqlDataReader-object-s-NextResult-method-in-ASP-Net-c/1183#.Vy2QmtJ97cs

 Copy and replace the following code in Default.aspx.cs.

Default.aspx.cs:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string ConnString = ConfigurationManager.ConnectionStrings["ShoppingZone"].ConnectionString;
            using (SqlConnection connection = new SqlConnection(ConnString))
            {
               connection.Open();
                string str="Select * from ProductDetail;select * from Employee";
                SqlDataAdapter dataAdapter = new SqlDataAdapter(str, connection);
               dataAdapter.SelectCommand.CommandType = CommandType.Text;
                DataSet dataset = new DataSet();
               dataAdapter.Fill(dataset);
               dataset.Tables[0].TableName = "ProductDetails";
               dataset.Tables[1].TableName = "Employees";
              GridView1.DataSource = dataset.Tables["ProductDetails"];
               GridView1.DataBind();
                GridView2.DataSource = dataset.Tables["Employees"];
               GridView2.DataBind();
           }
        }
   }
}