vml19
vml19

Reputation: 3864

How to avoid duplicate entries of a record when Exporting SQL server data to an Excel in C#?

I have requirement to export SQL Script data to an excel using C#.Net. Currently I face issue in avoiding duplicat values for columns, when a record has multiple values for a column it should look like as in the bellow, the entire record should not repeated for each values of the column.

should I handle it in application level or in Sql script. please advice me.

Currently I get data set and bind it to a grid view and then do the export to excel function.

Excel format

for example Sql Data could be

   select B.BID, B.BName, M.Year
   from tbl_B B
   inner join tbl_Master M ON B.BID = fm.BIDFK
   where B.BName = 'B9'
   Group By B.BID, B.BName, M.Year

And the Result

enter image description here

Upvotes: 0

Views: 1230

Answers (3)

vml19
vml19

Reputation: 3864

I have cleared out this issue on C# side, I use nested grid-view to bind multiple valued columns for a record, when exporting to excel clear controls inside the parent grid-view to export data from all grid-view.

Upvotes: 1

Larry Smithmier
Larry Smithmier

Reputation: 2731

I would seriously look at using the Report Viewer control and a local report rather than trying to build the Excel sheet by hand. Check out GotReportViewer.com and the examples on the right cover exporting to Excel, and using the control from a console application.

Upvotes: 0

Gregory Nozik
Gregory Nozik

Reputation: 3372

I think it need to be done in SQL script. It's not complicated logic to apply .

Upvotes: 0

Related Questions