Tuesday, August 26, 2008

Export data to csv file

Use the following code to export data to the csv file:

string attachment = "attachment; filename=MyFilename.csv";
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "text/csv";
Response.AddHeader("Pragma", "public");
Response.Write("hello world"); //data goes here;
Response.End();

Clear the current page (window)

In order to clear out the current page (make the page as a blank page), use following code:

Response.Flush();//will send the current response stream to the browser

Response.End() ;// will end the response,

Friday, August 22, 2008

Change the table schema

To change the schema of the tables, use this script command:

Alter schema "new_schema_name" transfer "old_schema_name.table_name"

Thursday, August 14, 2008

Retrieve images from Database using ASHX

Most of articles (which I found) about retrieve images from database are using Response.writebanary(...) to aspx file, then point the image's url to this aspx file. It worked fine for the most asp.net website. The luck ran out for me when I tried to implement a module for a dnn site. Because DNN only allows the user control (ascx) files, I had trouble to imped the image on my user control inside the DNN. The DNN will log me out every time after I displayed an image from the database.

The solution is quite simple, but took me a while to found it. Instead of using the aspx page as the image url to display the image on the page, use ASHX (HTTPHandle) file instead. The code used inside the ASHX file is almost the same as in the ASPX page.

For more detail on how to retrieve the image from database using ASHX: Go here

Tuesday, August 12, 2008

Restart IIS

To restart IIS

  • Click Start, click Run type IISReset, and then click OK.

    A Command Prompt window opens displaying the status of the IISReset command. You should read the status at the command prompt to make sure that IIS stops and restarts.

http://msdn.microsoft.com/en-us/library/ms957500.aspx

Thursday, August 07, 2008

ASP.NET UploadFile control

I am currently working on a project requires uploading the image to the database. It also requires to have a confirmation screen before saving the image to the database. What I found out is the UploadFile control's PostedFile object will get lost every time after the postback. So, there is no file on the confirmation screen to be saved to the database.

Only workaround I found for this issue is to save the PostedFile object to the Session state.

Monday, August 04, 2008

Create Javascript inside the ASP.net code behind page

I often ran into the situation that need to create the Javascript function dynamically instead of having it on the Aspx page. For example, if there is a text box inside one of the view in the MultiView, the Javascript function to detect the Enter Key press will not work when the view with text box is not showing (the rendered html code does not contain this specific text box).

In case like this, we need to create the Javascript code dynamically. The Javascript function will only show up if the perticular view is showing. We use the "Page.ClientScript.RegisterClientScriptBlock" to accomplish this problem.

First, use ServerSideControl.FindControl("controlName").ClientId to find unique client side id for this control.

Then use the Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ScriptId", "my Javascript code") to register the Javascript code.

To find this control inside the Javascript function: var btn = document.getElementById('" + strMyControlId + "');"

We can put above code inside a function and call it before making the view containing the text box visible to true.

MSDN: Page.ClientScript.RegisterClientScriptBlock
JavaScript with ASP.NET 2.0 Pages - part I