ASP.NET interview questions: - Can we merge two DataSet in to a Single GridView in ASP.NET?
- By Shiv Prasad Koirala in ASP.NET
- Oct 7th, 2011
This is not a much asked ASP.NET Interview questions but this question will definitely boost your practical knowledge.
Merge: - merge is one the method of DataSet. The merge function is basically used when we have a parent child relationship between the tables.
So let's create a simple example to see how exactly dataset merge function helps us to merge two dataset table in to a single GridView.
Assume that we have the following two tables with their respective data type and name.
Now, add some data to both the tables like below diagrams.
Now, let's create a small and simple ASP.NET application to understand the function of DataSet merge practically.
In order to see it practically just follow the following steps.
Step1: - create an ASP.NET Empty Web Application for that justgo to >> File >> New >> Project >> Windows >> Select ASP.NET Empty Web Application.
Step2: - now, just add a Web Form in to your Application for that just go to >> Solution Explorer >> Right Click on the project name >> Add >> New Item >> Select ASP.NET Empty Web Application.
Step3: - Now simply add a GridView Control on the WebForm1.aspx page.
Step4: - Now, just simply add the below code snippet in to your WebForm1.aspx.cs file.
If you have done with all the above steps now simply run your application and will see the result like below diagram.
//Import the below namespaces first. using System.Data; using System.Data.SqlClient; using System.Configuration; namespace MergeTablesInDataSet public partial class WebForm1 : System.Web.UI.Page //Define your own Connectionstring here. string str = ConfigurationManager.AppSettings["ConnectionString"]; protected void Page_Load(object sender, EventArgs e) //Just call the LoadData() method here. LoadData(); } //Created a LoadData() method public void LoadData() SqlConnection objConnection = new SqlConnection(str); objConnection.Open(); SqlCommand objCommand = new SqlCommand(); objCommand.CommandText = "select * from [dbo].[Customer]"; objCommand.Connection = objConnection; objCommand.ExecuteNonQuery(); SqlDataAdapter objadapter = new SqlDataAdapter(objCommand); //Create a DataSet which has [dbo].[Customer] table data. DataSet objdataset = new DataSet(); objadapter.Fill(objdataset); SqlCommand objcommand1 = new SqlCommand(); objcommand1.CommandText = "select * from [dbo].[Order]"; objcommand1.Connection = objConnection; objcommand1.ExecuteNonQuery(); SqlDataAdapter adap1 = new SqlDataAdapter(objcommand1); //Create a DataSet which has [dbo].[Order] table data. DataSet dataset1 = new DataSet(); adap1.Fill(dataset1); //Below is how exactly DataSet Merge Function is used. objdataset.Merge(dataset1); GridView1.DataSource = objdataset.Tables; GridView1.DataBind(); } } }
In the above diagram you can clearly see that both table's Customer and the Order table are now merged in to single GridView.
View the following video on EO Fundamentals of Function Point Analysis:
See our more ASP.NET interview questions
View author's other ASP.NET interview questions
Shiv Prasad Koirala
Visit us @ www.questpond.com or call us at 022-66752917... read more