Posts Tagged ‘.net’

Extension method in .net

Extension methods enable developers to add a method to an existing class without deriving or affecting the existing class. This extension methods only support a sub routine or a function.

Example if we want to extend the functionality of the String type

VB.NET


  1. Imports System.Runtime.CompilerServices
  2.  
  3. Module StringExtensions
  4.  
  5.     <Extension()>
  6.     Public Sub Print(ByVal aString As String)
  7.         Console.WriteLine(aString)
  8.     End Sub
  9.  
  10.     <Extension()>
  11.     Public Sub PrintAndPunctuate(ByVal aString As String,
  12.                                  ByVal punc As String)
  13.         Console.WriteLine(aString & punc)
  14.     End Sub
  15.  
  16. End Module

To use that function

  1.     Sub Main()
  2.  
  3.         Dim example As String = "Example string"
  4.         example.Print()
  5.  
  6.         example = "Hello"
  7.         example.PrintAndPunctuate(".")
  8.         example.PrintAndPunctuate("!!!!")
  9.  
  10.     End Sub

C#.NET
If you notice the parameter there is a this keyword, that is how it done.

  1. namespace ExtensionMethods
  2. {
  3.     public static class StringExtensions
  4.     {
  5.         public static int Print(<strong>this </strong>String str)
  6.         {
  7.             Console.WriteLine(String );
  8.         }
  9.     }  
  10. }
  11.  
  12. string s = "Hello Extension Method";
  13. s.Print();

, , ,

No Comments


Namespace or type specified in the project-level Imports ‘System.Xml.Linq’ doesn’t contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn’t use any aliases

There are 2 ways to remove this warning

First, you may need to convert the current .Net Framework used by the application to higher version, let’s say from .net framework 2 to 3.5

  1. Goto Property Pages of the web site project
    • View Menu->Property Pages or press shift+F4
  2. Click the Build option from the left side
    • Make sure the Target Framework is .Net Framework 3.5

Second, remove the namespace that shown in the warning in this case the “System.Xml.Linq” from the web.config

namespacelinq

namespace linq


, , , ,

No Comments


Error 1001. Exception occurred while initializing the installation. System.IO.FileNotFoundException


This error occurs when the CustomActionData has a supplied value,

Example:

The value of the CustomActionData is /TARGETDIR=[TARGETDIR], what you need to do is to update it to /TARGETDIR=”[TARGETDIR]\”

, ,

No Comments


How to get msi setup file location using installer class

To get the current msi setup path,

  1. Goto the setup project
  2. Right click and select view->custom actions
  3. Under Install Node, click the “primary output from….”
  4. Press F4 to show its property
  5. Add the /Source=[SourceDir] to the CustomActionData property
  6. Can then be access through the installer class

Context.Parameters(“Source”)

Source Path Custom Action

Source Path Custom Action


, , ,

No Comments


How to calculate Time span in .Net

Here’s how to code the time span;

Function ExecuteLargeRec() {

DateTime d1 = DateTime.Now

DateTime d2

TimeSpan ts

‘ Your query here / processing

d2 = DateTime.Now

ts = d1.Subtract(d2)

MessageBox.Show(ts.ToString())

}

, , ,

No Comments


The install location for prerequisites has not been set to

Follow this steps to solve this problem:
Update the Package Data

  1. Open the [Program Files]\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\DotNetFx35SP1 folder or %ProgramFiles(x86)%\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\DotNetFx35SP1 on x64 operating systems
  2. Edit the Product.xml file in Notepad.
  3. Paste the following into the < PackageFiles > element:
  4. Find the element for < PackageFile Name=”dotNetFX30\XPSEPSC-x86-en-US.exe” and change the PublicKey value to:
    3082010A0282010100A2DB0A8DCFC2C1499BCDAA3A34AD23596BDB6CBE2122B79
    4C8EAAEBFC6D526C232118BBCDA5D2CFB36561E152BAE8F0DDD14A36E284C7F163
    F41AC8D40B146880DD98194AD9706D05744765CEAF1FC0EE27F74A333CB74E5EFE
    361A17E03B745FFD53E12D5B0CA5E0DD07BF2B7130DFC606A2885758CB7ADBC85E
    817B490BEF516B6625DED11DF3AEE215B8BAF8073C345E3958977609BE7AD77C13
    78D33142F13DB62C9AE1AA94F9867ADD420393071E08D6746E2C61CF40D5074412
    FE805246A216B49B092C4B239C742A56D5C184AAB8FD78E833E780A47D8A4B2842
    3C3E2F27B66B14A74BD26414B9C6114604E30C882F3D00B707CEE554D77D208557
    6810203010001
  5. Find the element for < PackageFile Name=”dotNetFX30\XPSEPSC-amd64-en-US.exe” and change the PublicKey value to the same as in step 4 above
  6. Save the product.xml file

Download and Extract the Core Installation Files

  1. Navigate to the following URL: http://go.microsoft.com/fwlink?LinkID=118080
  2. Download the dotNetFx35.exe file to your local disk.
  3. Open a Command Prompt window and change to the directory to which you downloaded dotNetFx35.exe.
  4. At the command prompt, type:

dotNetFx35.exe /x:.

This will extract the Framework files to a folder named “WCU” in the current directory.
Copy the contents of the WCU\dotNetFramework folder and paste them in the %Program Files%\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\DotNetFx35SP1 folder (%ProgramFiles(x86)%\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\DotNetFx35SP1 on x64 operating systems).

Note: Do not copy the WCU\dotNetFramework folder itself. There should be 5 folders under the WCU folder, and each of these should now appear in the DotNetFx35SP1 folder. The folder structure should resemble the following: DotNetFx35SP1 (folder)

    • dotNetFX20 (folder
    • dotNetFX30 (folder)
    • dotNetFX35 (folder)
    • dotNetMSP (folder)
    • TOOLS folder)
    • en (or some other localized folder)
    • dotNetFx35setup.exe (file)

You may now delete the files and folders you downloaded and extracted in steps 2 and 4.

You can read more on the attached readme file.

VS 2008 SP1 Readme

, , , ,

No Comments


Cannot write to the registry key in .Net

Public Function SetValue(ByVal Value As String, ByVal k As String) As String
Dim mReg As RegistryKey = Registry.LocalMachine.OpenSubKey(“Software”, RegistryKeyPermissionCheck.ReadWriteSubTree, Security.AccessControl.RegistryRights.FullControl)
Try

mReg.OpenSubKey(My.Application.Info.CompanyName & “\” & My.Application.Info.Description, True).SetValue(k, Value)

Return True
Catch ex As Exception
Throw ex
End Try

End Function

, , ,

No Comments


How to get Month Name in .net


dim dt as Datetime

dt = Datetime.Today

Messabox.show(dt.ToString(“MMM”))

, ,

No Comments


Export to Excel with error Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046}

Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154.

This error occurs when the Microsoft office has not installed in the target machine.

You can export the file to csv.

, ,

No Comments


Could not load file or assembly ‘ADODB,Version’

Solution 1:

go to a machine that has this folder and copy it (or the adodb.dll file if you have the folder) to the machines that cannot find the adodb file

Go into this folder and into the primary interop folder

copy this folders contents to the GAC (c:\windows\assembly)

your app should find the adodb.dll now

Solution 2

,

No Comments



SetPageWidth