Thursday, June 26, 2014

Hide / Show Custom Errors in Asp.net

If you are seeing "Runtime Error" in your asp.net page and would like to see detail and actual line where the error is happening, this is what we need to do:

In Web.config file, look for tag "customErrors" tag and set the value as:


<customErrors mode="On" />        THIS WILL HIDE ERROR AND SHOW RUNTIME MESSAGE
<customErrors mode="Off" />        THIS WILL SHOW REAL MESSAGE


HTH

Monday, June 9, 2014

Install Assembly in GAC

How to install assembly in GAC?

The very simple way would be to use command prompt. Sounds difficult but it's not.

To have Gacutil installed, either need some version of Windows SDK or
if you cannot install SDK, then copy following 3 files from the machine where Visual Studio or SDK is already installed. I copied them from  C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools

The files are:
  • gacutil.exe
  • gacutil.exe.config
  • 1033\gacutlrc.dll     (can be placed in the same dir as gacutil.exe on the server)
Then go to cmd and get your directory set where gacutil.exe is placed.
Run following:

 gacutil.exe -I <dllfilename>     to install the DLLs in the GAC.


Assembly Locations:
For FW 2.0, its C:\Windows\assembly

For FW 4.0, its C:\Windows\Microsoft.NET\assembly\

In folder above, you can look in 32 or 64 or MSIL folder.
 

Wednesday, June 4, 2014

How to pass NULL to SelectParameters in SQLDatasource?

Here is the problem
 
How to pass NULL to SelectParameters in SQLDatasource from .aspx or codebehind file?
 
.aspx Method

Two things:
1. First set the SqlDataSource Property CancelSelectOnNullParameter = false
2. Second set the parameter property ConvertEmptyStringToNull="true"
 
Example:

<asp:SqlDataSource ID="ds1" runat="server" ConnectionString='<%$ ConnectionStrings:dbConnString %> 'SelectCommand="someSelect @param1=@param1" CancelSelectOnNullParameter="false">

<SelectParameters>

<asp:Parameter Name="param1" Type="String"  ConvertEmptyStringToNull="true" />

</SelectParameters>

</asp:SqlDataSource>

-------------------------------------------------------------------------------------------------------------------

Codebehind Method

To pass from codebehind, the parameter need to be set in the proper event of Datasource.
Also no other properties of parameter need to set in aspx. Just declare a parameter.
In example below, I am passing null during "Select" call, so the code will look as:

Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting
        If e.Command.Parameters("@param1").Value = "" Then
            e.Command.Parameters("@param1").Value = System.DBNull.Value
        End If    
End Sub


HTH
 

Monday, June 2, 2014

Replace Single Quotes in SQL


You need to use double quotes to represent single quote for SQL to understand:

For instance, to replace single quote with empty space:
(I have colored red for input quotes and non red for replacements for easy reading)

Replace(@string, '''','')

To replace single quote with two single quotes, it would be

Replace(@string,'''','''''')

Just remember, 2 quotes for each single quote, plus opening and closing quotes.

HTH