Friday, May 29, 2009

how to check if Global.asax is functioning

After deployment many times Global.asax stops working.The below code can be used to check if it is working or not. More details can be found at

http://bytes.com/groups/net-asp/501901-web-deployment-projects-global-asax-problem

==========
protected void Page_Load(object sender, EventArgs e)
{
        Response.Write("ApplicationInstance: " + Context.ApplicationInstance.GetType().FullName);
}
==========

It will output something like: "ApplicationInstance: ASP.global_asax" if the global.asax is loaded,

OR "ApplicationInstance: System.Web.HttpApplication" if the global.asax is NOT loaded.

Thursday, April 23, 2009

Problems while creating Image from a PDF

Error: The machine-default permission settings do not grant Local Activation permission for the COM Server application with CLSID.

Solution:
Grant the correct permissions to the Network Service account

To grant the correct permissions to the Network Service account, follow these steps:
  1. Click Start, click Run, type dcomcnfg in the Open box, and then click OK.
  2. In Component Services, double-click Component Services, and then double-click Computers.
  3. Right-click My Computer, and then click Properties.
  4. Click the COM Security tab.
  5. In the Launch and Activation Permissions area, click Edit Default.
  6. Click Add, type Network Service, and then click OK.
  7. While Network Service is selected, click to select the Allow check boxes for the following items:
  • Local Launch
  • Remote Launch
  • Local Activation
  • Remote ActivationClick OK two times.
Please refer http://support.microsoft.com/kb/899965 for further details.

Dependency: Do not forget to get full and complete version (licensed version) of software when you are dealing with COM components like Acrobat, etc.

Note: This is generally caused due to permission restrictions to Network Service Account. You can see Event Log (type eventvwr.msc in Run Menu) in System Part and you will find error like: "The machine-default permission settings do not grant Local Activation permission for the COM Server application with CLSID {FF76CB60-2E68-101B-B02E-04021C009402} to the user NT AUTHORITY\NETWORK SERVICE SID (S-1-5-20). This security permission can be modified using the Component Services administrative tool."

Tuesday, March 31, 2009

Using sp_executesql with parameters

The code I will share today is a cool trick when using sp_executesql. In some case, when you want to retreive only one record from a db, it could be interesting to have the result (the fields) or a part (some fields) of the result directly as variables instead of using another table to store the result and then re query to retreive the fields you want.

Imagine you have a Table called myContacts that have 6 columns:

id|Field1|Field2|Field3|Field4|Field5

And you want to query this table to retreive the record where id=4 and directly set some fields as variables, so you will be able to use them right after in you code.

Here is the trick :

declare
--The variables that will house the fields
@myField1 int,
@myField3 nvarchar(max),
@myField5 nvarchar(max),

--query variables
@sql nvarchar(max),
@ParamDef nvarchar(max)

--The query
select @sql = 'select @myField1=Field1, @myField3=Field3, @myField5=Field5 FROM myContacts WHERE id = 4'

--The parameters
select @ParamDef = '@myField1 int OUTPUT, @myField3 nvarchar(max) OUTPUT, @myField5 nvarchar(max) OUTPUT'

--Execute de sql statement
exec sp_executesql @sql, @paramDef, @myField1 OUTPUT, @myField3 OUTPUT, @myField5 OUTPUT

Print 'Field #1 = '+ convert(nvarchar,@myField1)+ ' Fields 3 & 5 : '+@myField3+',' +@myField5


OUTPUT : Field #1 = 1 Fields 3 & 5 : This is the Content 3, And this is Content 5

That’s an easy way to initiate variable on fly with from a sp_executesql result.

Ref. Link:
http://proofofconcepts.wordpress.com/2008/06/18/t-sql-multi-output-when-using-sp_executesql/#


Friday, February 6, 2009

Exception: Filestream will not open win32 devices.... don't use \ "\\\.\\\" in the path

Dim tw As System.IO.StreamWriter = System.IO.File.AppendText("C:\NUL.txt")

The above statement will generate error like: "Filestream will not open win32 devices.... don't use \ "\\\.\\\" in the path"

Reason and Rectification:

Avoid using "CON" or "NUL" like filenames as they are reserved windows device names, you cannot use such words within a filename.

Thursday, February 5, 2009

Find Operating System Information from your VB.NET 2005 application

Ref URL: http://blogs.msdn.com/rahulso/archive/2006/03/26/find-operating-system-information-from-your-vb-net-2005-application.aspx

Also See: http://blogs.msdn.com/rahulso/archive/2007/03/07/using-system-management-to-get-information-about-the-operating-system-from-your-vb-net-application.aspx

First Method:


Sub DisplayOperatingSystemInformation()
Dim osInfo As OperatingSystem
osInfo = Environment.OSVersion
Dim verInfo As Version
verInfo = osInfo.Version
'Display Version Information
MessageBox.Show( _
"Major Version = " & verInfo.Major & vbCrLf & _
"Minor Version = " & verInfo.Minor & vbCrLf & _
"Revision = " & verInfo.Revision & vbCrLf & _
"Build = " & verInfo.Build & vbCrLf & _
"Platform = " & osInfo.Platform & vbCrLf & _
"Platform String = " & osInfo.VersionString, _
"Operating System Information", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
End Sub


Second Method:
1. Add Reference to
System.Management

2.
Imports System.Management

Public Class ClientInfo

Private objOS As ManagementObjectSearcher
Private objCS As ManagementObjectSearcher
Private objMgmt As ManagementObject
Private m_strComputerName As String
Private m_strManufacturer As String
Private m_StrModel As String
Private m_strOSName As String
Private m_strOSVersion As String
Private m_strSystemType As String
Private m_strTPM As String
Private m_strWindowsDir As String

Public Sub New()
objOS = New ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem")
objCS = New ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem")

For Each objMgmt In objOS.Get
m_strOSName = objMgmt("name").ToString()
m_strOSVersion = objMgmt("version").ToString()
m_strComputerName = objMgmt("csname").ToString()
m_strWindowsDir = objMgmt("windowsdirectory").ToString()
Next

For Each objMgmt In objCS.Get
m_strManufacturer = objMgmt("manufacturer").ToString()
m_StrModel = objMgmt("model").ToString()
m_strSystemType = objMgmt("systemtype").ToString
m_strTPM = objMgmt("totalphysicalmemory").ToString()
Next
End Sub

Public ReadOnly Property ComputerName()
Get
ComputerName = m_strComputerName
End Get
End Property

Public ReadOnly Property Manufacturer()
Get
Manufacturer = m_strManufacturer
End Get
End Property

Public ReadOnly Property Model()
Get
Model = m_StrModel
End Get
End Property

Public ReadOnly Property OsName()
Get
OsName = m_strOSName
End Get
End Property

Public ReadOnly Property OSVersion()
Get
OSVersion = m_strOSVersion
End Get
End Property

Public ReadOnly Property SystemType()
Get
SystemType = m_strSystemType
End Get
End Property

Public ReadOnly Property TotalPhysicalMemory()
Get
TotalPhysicalMemory = m_strTPM
End Get
End Property

Public ReadOnly Property WindowsDirectory()
Get
WindowsDirectory = m_strWindowsDir
End Get
End Property

End Class



Third Method:

Ref URL: http://www.devasp.net/net/articles/display/420.html

lblOsVersion.Text = Environment.OSVersion.ToString
lblUser.Text = Environment.UserName.ToString
lblRuntime.Text = Environment.Version.ToString
lblUptime.Text = Mid((Environment.TickCount / 3600000), 1, 5) & " :Hours"
lblSystemRoot.Text = Environment.SystemDirectory.ToString
lblCompName.Text = Environment.MachineName.ToString
lblCurrentDirectory.Text = Environment.CurrentDirectory.ToString
lblUserDomainName.Text = Environment.UserDomainName.ToString
lblWorkingSet.Text = Environment.WorkingSet.ToString
lblCommandLine.Text = Environment.CommandLine.ToString
lblGetLogicalDrives.Text = ""

For i As Integer = 0 To Environment.GetLogicalDrives.Length - 1
lblGetLogicalDrives.Text += Environment.GetLogicalDrives(i)
lblGetLogicalDrives.Text += " "
Next

Thursday, January 22, 2009

Errors (501, 403 and 401) while using AppUpdater for a .Net Windows Application

If the error is:
"System.Net.WebException: The remote server returned an error: (501) Not Implemented."
1) Go to IIS and click on Web Server Extentsions and then click on WebDAV and give it the
allow permissions.
2) Check the "directory browsing" checkbox for the update folder from IIS.

If the error is:
"System.Net.WebException: The remote server returned an error: (403) Forbidden."
You have to grant permission on the foll:
1) Script Source Access permission to the update folder.

If the error is:
"System.Net.WebException: The remote server returned an error: (401) Unauthorized."
Cause: Problem can be App.config which will be "ApplicationName.exe" web configuration file.

Solution: Try removing the App.config from updates folder. It should work !! :)

Removing the .config file "works" but may not be the best solution becuase .config files need to be sent as application updates at times. Here's a little theory as to why this error occurs followed by a solution: Files with extentions like .asp, .aspx, .config are treated as scripts. something which IIS is supposed to run/interpret and not just allow users to download in it's raw form. so if you visit an asp you see html returned by asp. Similary if you visit a .config using your browser you see xml output of the .config -however IIS does NOT let you download these files and in a typical web scenario there's no reason why a user should. However, in our case .config is a part of the client application we're trying to download using appupdater and not a typical web/IIS script. So, The solution? You need to do is enable "Script Source Access" on the virtual directory that's hosting your update using IIS admin! That's all you need to do :).


For further details:
Also refer link: http://social.msdn.microsoft.com/Forums/en-US/winformssetup/thread/0b069264-167b-47a4-b312-64aa9bd4c86a/

Wednesday, January 21, 2009

Problems while writing EventLog from ASP.NET Web Application

Problems while Event Log for a Business Logic Layer (BLL) from ASP.NET Web Application

Error:
System.Security.SecurityException: Requested registry access is not allowed.

Grant permission to create a custom event log

  1. Log on to the computer as an administrator.
  2. Click Start, click Run, type regedit in the Open box, and then click OK. The Registry Editor window appears.
  3. Locate the following registry subkey:
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog
  4. Right-click Eventlog, and then click Permissions. The Permissions for Eventlog dialog box appears.
  5. Click Advanced. The Advanced Security Settings for Eventlog dialog box appears.
  6. In the Name column, double-click the Users group. The Permission Entry for Eventlog dialog box appears.
  7. Select the Set Value check box, select the Create Subkey check box, and then click OK.
  8. Quit Registry Editor, and then log off from the administrator account.
  9. Log on to the computer as a regular user.
  10. Try to create a custom event log by using Visual Studio .NET, and then try to write to this event log by using an application that is built on Visual Studio .NET.

Ref URL:- http://support.microsoft.com/kb/842795