ASP.NET interview questions: – Explain how to merge two DataSet in to a Single GridView in ASP.NET?

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 nd 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 just go 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
.

//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[0];
GridView1.DataBind();
}
}
}

If you have done with all the above steps now simply run your application and will see the result like below diagram.

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

Regards,

View author’s other  ASP.NET interview questions

Advertisements

About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , , , , . Bookmark the permalink.

One Response to ASP.NET interview questions: – Explain how to merge two DataSet in to a Single GridView in ASP.NET?

  1. Rahul says:

    Thats pretty cool and nice

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s