How To display table in GridView: Dynamically filling data in GridView from DataTable in ASP .Net

I had a datatable and i needed to show in a GridView in my ASP .Net page. Following is the code which help me to do so.

//get table to be shown in grid view;
DataTable dataTable = GetRequiredTable();
//assing table to the DataSource property of GridView
myGridView.DataSource=dataTable;
//bind data from a data source to the GridView control
myGridView.DataBind();

Here GetRequiredTable() is a user(example) function which return a table to display in to gridview.
You can also use DataSet in place of DataTable.

Advertisements
Published in: on November 17, 2009 at 10:27 am  Leave a Comment  
Tags: , ,

DCOM Error : Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005.

When we tried to use word automation from .Net in ASP .Net application earlier it was working but then it stopped working in some of the machine giving error
“Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005.”.
We had DCOM setting in place, IIS setting was also ok. Then I got following solution:

  • I changed the AppID Registry key under HKEY_CLASSES_ROOT\AppID\WINWORD.EXE from old entry to {00020906-0000-0000-C000-000000000046}.

(for some it will work after this, but if still its not working go for second.)

  • 2. Also changed the DCOM setting.
    • a. Open the Component Service Console.(Either by running command: dcomcnfg OR by opening it from Control Panel->Administrative Tool -> Component Services)
    • b. Now Select ‘Microsoft Word Document’, Under Component Services->Computers->My Computer -> DCOM Config.
    • c. Do Right click and select Properties.
      it will open Microsoft Word Document Properties dialog.
    • d. In Microsoft Word Document Properties dialog, select Identity tab.
    • e. In Identity tab, if ‘The launching user.’, in our case third option was selected, so we selected 2nd option.

This may solve out problem, hope it would be helpful for you also. If you need to say something please feel free to speak through comments.

 

References:

http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/006cbbac-101a-40ea-ae16-d59a7a2da8b1

Published in: on November 3, 2009 at 4:17 pm  Comments (7)  
Tags: , , , , , , ,

ASP .NET: Dynamically creating/generating excel file at server and making it available to download

If you want that when user click on a particular link, based on his preferences an excel file should be generated dynamically and should be downloaded automatically at client. Here is small example how you can do it.

Trick is create a html file and present it as excel file ;).

for this follow the steps:

1.  Add a web form say xlImage.aspx, in your project.

2. Delete every thing from xlImage.aspx page except first line.

3. Now in code behind page, i.e., in xlImage.aspx.cs add following line on,

Page_Load function

Response.Clear();
Response.ClearHeaders();
Response.ClearContent();

Response.Buffer = true;
Response.AddHeader(“content-disposition”, “attachment;filename=file.xls”);

Response.ContentType = “application/vnd.ms-excel”;

String imagepath = “<img src=’http://localhost:2290/Excel%20Export/pic/1.jpeg&#8217; width=’70’ height=’80’ style:’position: absolute;left:200px;’/>”;

Response.Output.Write(“\n<body>\n<html>”);

Response.Output.Write(“<center><table width=’800′ align=’center’ style=’text-align:center’ border=’1px’>”);
Response.Output.Write(“<tr><td></td><td></td><td></td><td></td><td colspan=’2′ align=’center’><div>” + imagepath + “</div></td></tr>”);
Response.Output.Write(“</table></center>”);
Response.Output.Write(“\n</body>\n</html>”);

Response.Flush();
Response.End();

same thing could be done for word file also.