Category Archives: .Net

ODAC Assigning DBNull.Value to paramater

Here we go, null is not an instance of any type, it is an invalid reference.Whereas, System.DbNull.Value, is a valid reference to an instance of System.DbNull
that represents nonexistent (e.g. NULL) values in the database.

(System.DbNull is a singleton and System.DbNull.Value gives you a reference to the single instance of that class)

The keyword null represents an invalid reference. The class System.DbNull represents a nonexistent value in a database field.

// Assigning Null value to Procedure Parameter

OracleParameter pram_IN_1 = new OracleParameter("pPACKAGE_NAME", OracleDbType.Varchar2);
pram_IN_1.Direction = ParameterDirection.Input;
pram_IN_1.Value = strPackageName != null ? strPackageName : (object)DBNull.Value;
cmd.Parameters.Add(pram_IN_1);

//

C#.Net Common Variable Types

The chart below details most of the common variable types, as well as their size and possible values.

C# Type .Net Framework (System) type Signed? Bytes Occupied Possible Values
sbyte System.Sbyte Yes 1 -128 to 127
short System.Int16 Yes 2 -32768 to 32767
int System.Int32 Yes 4 -2147483648 to 2147483647
long System.Int64 Yes 8 -9223372036854775808 to 9223372036854775807
byte System.Byte No 1 0 to 255
ushort System.Uint16 No 2 0 to 65535
uint System.UInt32 No 4 0 to 4294967295
ulong System.Uint64 No 8 0 to 18446744073709551615
float System.Single Yes 4 Approximately ±1.5 x 10-45 to ±3.4 x 1038 with 7 significant figures
double System.Double Yes 8 Approximately ±5.0 x 10-324 to ±1.7 x 10308 with 15 or 16 significant figures
decimal System.Decimal Yes 12 Approximately ±1.0 x 10-28 to ±7.9 x 1028 with 28 or 29 significant figures
char System.Char N/A 2 Any Unicode character (16 bit)
bool System.Boolean N/A 1 / 2 true or false

Making F1 Google Search for Visual Studio

Start Visual Studio 2008 OR Visual Studio 2010

  1. From Menu bar select “Tools->Macros->New Macro Project
  2. Name the project “MyGoogleSearch
  3. Rename the default Module1 to “GoogleSearch” (right click on the module name in the left hand pan, select “Rename”)
  4. Copy following code and paste into the module body

Sub doGoogleSearch()

    Dim url As String
    Dim search As TextSelection = DTE.ActiveDocument.Selection()

    If search.Text <> "" Then
        url = "www.google.com/search?q=" + search.Text
    Else
        url = "www.google.com/"
    End If

    ' Run Command: "firefox.exe -new-tab http://www.google.com"

    Dim cmd As System.Diagnostics.Process = New System.Diagnostics.Process()

    cmd.StartInfo.Arguments += " -new-tab " + url

    cmd.StartInfo.FileName = "firefox"

    cmd.Start()

End Sub
  1. Build and save your “GoogleSearch” module.
  2. From menu bar  “Tools->Options->Environment->Keyboard
  3. In the “Show commands containing:” input textbox, type “Google” (to search your newly created macro).
  4. Go to the “Press shortcut keys” box, and press F1 and then Assign button
  5. Press OK.

Select text in your Visual Studio Environment that you want to google and press F1, now google searches for whatever you highlighted.
Your search result will be appeared by open New Tab in Firefox (no matter if firefox is already running ).

enjoy your development and google search interactive format.  Save your time of text copy/paste or typing the terms you want to google.


Using C#.Net Create Zip File

From C# code, files can be easily compressed in Zip format by getting the advantage of  Zip functionality available in J#. First of all we have to add reference of the J# .NET library in our project. Physically, it resides as a file named vjslib.dll

Add Reference vjslib (J# .Net Library):

In Solution Explorer Right click your project on References and click on “Add Reference” -> select the .NET tab -> scroll down and select “vjslib” -> click OK.
After adding reference, the Java library classes could be refered  within your application.

C# Code:


using java.util;
using java.util.zip;
using java.io;



 private void CreateZipFile(string DestinationZipFileName, string SourceFileToZip)
 {

 FileOutputStream fileOutStream;
 fileOutStream = new FileOutputStream(DestinationZipFileName);
 ZipOutputStream zipOutStream ;
 zipOutStream = new ZipOutputStream(fileOutStream);

 FileInputStream fileInStream ;
 fileInStream = new FileInputStream(SourceFileToZip);

 ZipEntry zipEntry;
 zipEntry = new ZipEntry(Path.GetFileName(SourceFileToZip));

 zipOutStream.putNextEntry(zipEntry);

 sbyte[] buffer = new sbyte[1024];
 int len = 0;
 while ((len = fileInStream.read(buffer)) >= 0)
 {
 zipOutStream.write(buffer, 0, len);
 }

 zipOutStream.closeEntry();
 fileInStream.close();
 zipOutStream.close();
 fileOutStream.close();

 }



I hope this simple example will help you a lot.


Play Audio Files with SoundPlayer Class

Here is simplest way to play Audio Files and System Sounds with SoundPlayer class in .Net


//Namespace:  System.Media
//Assembly:  System (in System.dll)

using System.Media;

// Create an instance of the SoundPlayer class and attaches the specified .wav file

SoundPlayer soundplayer= new SoundPlayer("audio file path");

// Load the .wav file.
soundplayer.LoadAsync();

//Check if loading of a .wav file has successfully completed.

if (soundplayer.IsLoadCompleted)
{

//plays the selected .wav file

soundplayer.Play();
}

// play system sounds
SystemSounds.Exclamation.Play();

NOTE: The SoundPlayer class cannot play other file types, such as .wma or .mp3. If you want to play other file types, you can use the Windows Media Player control. For more information, see Using the Windows Media Player Control in a .NET Framework Solution and Windows Media Player Object Model Reference for Visual Basic .NET and C# in the Windows Media Player SDK.


Add Custom Key tag in web.config / aap.config appSettings

Very often it is required to add custom tags or key in app.config or in web.config. Here is a simple way to add key in <appSettings> and then access it.

Find section <appSettings> or <appSettings/> in .config file and add your required keys and values, the following example is same for both  .config files. here we go

An appSettings element in the root Web.config / App.config file that looks like the following:

<appSettings>

<add key=myKeyNamevalue=my custom setting value/>

appSettings>

The <appSettings> element is a direct child of the <configuration> element and a peer of the system.web element.

Values read from the appSettings element of the .config file are always of type String. If the specified key does not exist in the .config file, no error occurs. Instead, an empty string is returned

C# Code:


using System.Collections.Specialized;

using System.Configuration;

string strKeysAndValues = "";

// get all keys and values in AppSettings element

NameValueCollection sAll = ConfigurationManager.AppSettings;

foreach (string s in sAll.AllKeys)

{

 strKeysAndValues += "Key" + s + " value:"+ sAll.Get(s)+" ";

}

//OR

string strKeysAndValues= ConfigurationSettings.AppSettings["myKeyName"].ToString();


World’s Ever Simplest Example to Understand Thread Pool

1. Step

//creating class that contain method which would be used for threads start method

class ContaingThreadStartMethod{

private ManualResetEvent _doneEvent;

public ContaingThreadStartMethod(ManualResetEvent doneEvent)

{

_doneEvent = doneEvent;

}

public void StartMethod(object obj){

try{

int i = (int)obj;

}

catch (Exception ex)

{

string error_msg = ex.message;

}

finally

{

_doneEvent.Set();

}

}

}

2. Step

//Creating threads and

//maintain thread pool

ThreadCount = 20; // threads to be initiated

int number = 1; // object to be passed for manupulation

ManualResetEvent[] doneEvents = new ManualResetEvent[ThreadCount];

for (int i = 0; i &lt; ThreadCount; i++)

{

doneEvents[i] = new ManualResetEvent(false);

ContaingThreadStartMethod ctsm = new ContaingThreadStartMethod(doneEvents[i]);

ThreadPool.QueueUserWorkItem(ctsm.StartMethod, number);

}

// Wait for all threads in pool to calculation…

WaitHandle.WaitAll(doneEvents);

.


Uninstall .Net Window Service

here is command for uninstall .net window service.

C:Program FilesMicrosoft Visual Studio 9.0VC>installutil/u “physical path of service .exe file