Friday, December 19, 2008

How to disable close button from window form using .NET application

How to disable window close button ("X") in .NET


VB.NET

                       

To disable close button from the window form using vb.net needs some Dlls from system library. Call DisableCloseButton(me.Handle) function from your window form button click event or from where you want to disable "x" button.

Code-1

 

Private Const MF_BYPOSITION As Int32 = &H400

Private Const MF_REMOVE As Int32 = &H1000


Private Declare Auto Function GetSystemMenu Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal bRevert As Int32) As IntPtr


Private Declare Auto Function GetMenuItemCount Lib "user32.dll" (ByVal hMenu As IntPtr) As Int32


Private Declare Function DrawMenuBar Lib "user32.dll" (ByVal hwnd As IntPtr) As Boolean


Private Declare Auto Function RemoveMenu Lib "user32.dll" (ByVal hMenu As IntPtr, ByVal nPosition As Int32, ByVal wFlags As Int32) As Int32


'This method will be used to disable the Form "X" button

Public Shared Sub DisableCloseButton(ByVal hwnd As IntPtr)

Dim hMenu As IntPtr, n As Int32

hMenu = GetSystemMenu(hwnd, 0)

If Not hMenu.Equals(IntPtr.Zero) Then

              n = GetMenuItemCount(hMenu)

     If n > 0 Then

                   RemoveMenu(hMenu, n - 1, MF_BYPOSITION Or MF_REMOVE)

         RemoveMenu(hMenu, n - 2, MF_BYPOSITION Or MF_REMOVE)

                   DrawMenuBar(hwnd)

     End If

End If

End Sub

Code-2

Another way to disable close button from window form , override  "CreateParams" property. You have to Include following code in your in your window form.

     Private Const CP_NOCLOSE_BUTTON As Integer = &H200

     Protected Overloads Overrides ReadOnly Property CreateParams() As    CreateParams

         Get

             Dim myCp As CreateParams = MyBase.CreateParams

    myCp.ClassStyle = myCp.ClassStyle Or CP_NOCLOSE_BUTTON

              Return myCp

End Get

     End Property

C#.NET

In C#.net its simple just override "CreateParams" property, that's all.

Including following code in your in your window form.

private const int CP_NOCLOSE_BUTTON = 0x200;

protected override CreateParams CreateParams

{

   get

   {

      CreateParams myCp = base.CreateParams;

      myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;

      return myCp;

    }

 }

Monday, December 15, 2008

How to play sound with .NET

Play different sound with .NET

You can make some noise in .NET application using VB/C# .NET code. There are multiple ways to do this, I am giving one of them; hope it's fulfill your requirement.

VB.NET

'You can add some more sound as per your requirement

Public Enum MessageBeepType

[Default] = -1

OK = &H0

[Error] = &H10

Question = &H20

Warning = &H30

Information = &H40

End Enum

' This allows you to 'Beep' the sounds declared in the MessageBeepType

<Runtime.InteropServices.DllImport("USER32.DLL", setlasterror:=True)> _

Public Shared Function MessageBeep(ByVal type As MessageBeepType) As Boolean

End Function

' Allowing you to specify the frequency and duration of the sound.
<Runtime.InteropServices.DllImport("KERNEL32.DLL")> _
Public Shared Function Beep(ByVal frequency As Integer, ByVal duration As Integer) As Boolean
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

MessageBeep(MessageBeepType.Default)

End Sub

C#.NET

Public Enum MessageBeepType

{

Default = -1,

OK = 0x0,

Error = 0x10,

Question = 0x20,

Warning = 0x30,

Information = 0x40

}

//This allows you to 'Beep' the sounds declared in the MessageBeepType

[Runtime.InteropServices.DllImport("USER32.DLL", setlasterror = true)]

public static bool MessageBeep(MessageBeepType type)

{

}

' Allowing you to specify the frequency and duration of the sound.

[Runtime.InteropServices.DllImport("KERNEL32.DLL")]
public static bool Beep(int frequency, int duration)

{

}

Button1_Click(object sender, System.EventArgs e)

{

MessageBeep(MessageBeepType.Default);

}

Tuesday, December 02, 2008

How to add Image in the DatagridView using VB.NET

How to add image or icon in the DatagridView column using vb.net or c#.net

VB.NET

Dim dgImage As New DataGridViewImageColumn
dgImage.Image = Drawing.Image.FromFile("C:\FixedOption.ico")
dgImage.ReadOnly = True
dgImage.ImageLayout = DataGridViewImageCellLayout.Normal
dgDataGridView.Columns.Add(dgImage)

'After This you can bind the data grid with any dataset,if you want
dgDataGridView.DataSource = DataSet

C#.NET

dgDataGridView.Rows.Clear();

DataGridViewImageColumn dgImage = new DataGridViewImageColumn();

dgImage.Image = Drawing.Image.FromFile("C:\\IconLocation\\FixedOption.ico");

dgImage.ReadOnly = true;

dgImage.ImageLayout = DataGridViewImageCellLayout.Normal;

dgDataGridView.Columns.Add(dgImage);

//After This you can bind the data grid with any dataset

dgDataGridView.DataSource = BindDataSet;

Thursday, November 20, 2008

Error: The type 'Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging' cannot be resolved. Please verify the spelling is correct or that the full type

Error: The type 'Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging' cannot be resolved. Please verify the spelling is correct or that the full type name is provided.

This error occurs when you work with Microsoft Exception and Logging block.

I got this error with VS2005 and Microsoft Exception and Logging 3.1.0.0,

I resolve this error while adding following reference into User Interface (where my app.config file exists)

Right click on the project and say Add Reference and select this reference from "EntLib3Src\App Blocks\bin" folder

Add Reference: "Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.dll"

Note: This folder create, when you install "Enterprise Library 3.1" in your system

http://www.microsoft.com/downloads/details.aspx?FamilyID=4c557c63-708f-4280-8f0c-637481c31718&displaylang=en

Wednesday, September 10, 2008

Error: ora-12638 credential retrieval failed

Error: ora-12638 credential retrieval failed

If you try to connect Oracle from .NET code (in my case its Oracle XE
with VB.NET code) and if you are getting error "ora-12638 credential retrieval failed"
then try following to resolve this issue.

Open sqlnet.ora file and change the following entry

Existing Entry - SQLNET.AUTHENTICATION_SERVICES= (NTS)

Modified Entry - SQLNET.AUTHENTICATION_SERVICES= (NONE)

Monday, September 01, 2008

Sending XML string as an input parameter to Oracle Stored Procedure from VB.NET

Sending XML string as an input parameter to Oracle Stored Procedure from VB.NET

Overview:

This document will guide you to insert or update multiple rows into the oracle stored database using VB.NET code. This is common requirement across any project when you have to insert or update multiple rows in one shot from the code. This is especially true when you are working on web application where you have a restriction not to hit database multiple time. Using this document you can create VB/C#.NET program to achieve this. This document will also gives sample Oracle Store procedure.

Introduction:

Passing XML string as an input parameter to Oracle Stored procedure is always need some research. This way we have to spend lots of time to really understand the architecture and flow. To save research and other POC development time, I am trying to give very simple way to describe how we can pass XML string as an input parameter to oracle stored procedure using VB.NET code. You can convert this code in C#.NET also.
Assume you have a requirement; to insert 10 rows into the database; how you do? Normally you execute stored procedure 10 times to insert 10 rows into the database table from the code. While doing this your application will hit the performance and slow down the process especially in case of web applications.
Here I am giving the best way to pass multiple rows into the database table from VB.NET; you have to call oracle stored procedure only one time. This method you can use for insert/update multiple rows/data into the database table.
Microsoft Application Block use "System.Data.OracleClient"for oracle stored procedure execution and this namespace not allow to use "OracleDbType.XmlType" datatype, that's requires for sending XML string as a input parameter into the oracle stored procedure.
You require "Oracle.DataAccess.Client" namespace to use "OracleDbType.XmlType" data type and pass XML value into the Oracle Stored procedure. Using this document you can modify application block using following lines of code.

VB.NET 2.0 Source Code


Name Space
Imports Oracle.DataAccess.Client
Imports System.Text
VB.NET Code
Dim oraCmd As New OracleCommand

Dim oraCon As New OracleConnection

Dim recordEffected As Integer

Dim XMLString As String


try

{


'Generate XML String, here I am taking 2 rows in the xml

'This XML can be taken from XML file or can be generated using .NET 'Serialization

XMLString.Append("<?xml version='1.0' encoding='utf-8' ?>")

XMLString.Append("<EmployeeData>")

XMLString.Append("<Root>")

XMLString.Append("<EmpName>Ritesh</EmpName>")

XMLString.Append("<EmpSal>10000</EmpSal>")

XMLString.Append("<DeptNo>1010</DeptNo>")

XMLString.Append("<JoinDate>03-Apr-2006</JoinDate>")

XMLString.Append("</Root>")

XMLString.Append("<Root>")

XMLString.Append("<EmpName>Rajiv</EmpName>")

XMLString.Append("<EmpSal>20000</EmpSal>")

XMLString.Append("<DeptNo>2020</DeptNo>")

XMLString.Append("<JoinDate>05-May-2007</JoinDate>")

XMLString.Append("</Root>")

XMLString.Append("</EmployeeData>")

'Create the connection String

oraCon.ConnectionString = "Data Source=DNSName;UserID=UID;Password=PWD;"

oraCon.Open()

oraCmd.CommandType = CommandType.StoredProcedure

oraCmd.CommandText = "INSERT_EMPLOYEE_DATA" ' Oracle SP Name

oraCmd.Connection = oraCon

oraCmd.Parameters.Add("ip_emp_details", OracleDbType.XmlType,

XMLString, ParameterDirection.Input) ' Oracle SP Input Parameter name

'Execute the Stored Procedure

recordEffected = oraCmd.ExecuteNonQuery()

catch

{

throw;

}

}

Finally

{

oraCon.Close()

oraCon.Dispose()

}
Oracle Stored Procedure

Oracle stored procedure will be different for different XML format,but the logic for facthing the XML node value will be the same.According to above XML format used the oracle stored procedure will be as following
Oracle Database Table
CREATE TABLE employeeDetails

(

EmpName VARCHAR2(50),

EmpSal INTEGER,

DeptNo INTEGER,

JoinDate DATE

)
Oracle Stored Procedure for Insert data
CREATE OR REPLACE PROCEDURE "INSERT_EMPLOYEE_DATA"

(

ip_emp_details IN XMLTYPE

)

IS


BEGIN


FOR i IN
(

SELECT XMLTYPE.EXTRACT (VALUE (a),

'/Root/EmpName/text()').getstringval() AS ipEmpName,

XMLTYPE.EXTRACT (VALUE (a),

'/Root/EmpSal/text()').getstringval() AS ipEmpSal,

XMLTYPE.EXTRACT (VALUE (a),

'/Root/DeptNo/text()').getstringval () AS ipDeptNo,

XMLTYPE.EXTRACT (VALUE (a),

'/Root/JoinDate/text()').getstringval () AS ipJoinDate

FROM TABLE

(XMLSEQUENCE (ip_emp_details.EXTRACT

('/EmployeeData/Root')

)

) a )


LOOP


INSERT INTO employeeDetails

(EmpName, EmpSal, DeptNo,JoinDate)

VALUES

(i.ipEmpName, i.ipEmpSal, i.ipDeptNo,i.ipJoinDate);


END LOOP;


END INSERT_EMPLOYEE_DATA;

/
Oracle Stored Procedure for Update data
CREATE OR REPLACE PROCEDURE "UPDATE_EMPLOYEE_DATA"

(

ip_emp_details IN XMLTYPE

)

IS


BEGIN


FOR i IN


(

SELECT XMLTYPE.EXTRACT (VALUE (a),

'/Root/EmpName/text()').getstringval() AS ipEmpName,

XMLTYPE.EXTRACT (VALUE (a),

'/Root/EmpSal/text()').getstringval() AS ipEmpSal,

XMLTYPE.EXTRACT (VALUE (a),

'/Root/DeptNo/text()').getstringval () AS ipDeptNo,

XMLTYPE.EXTRACT (VALUE (a),

'/Root/JoinDate/text()').getstringval () AS ipJoinDate

FROM TABLE

(XMLSEQUENCE (ip_emp_details.EXTRACT

('/EmployeeData/Root')

)

) a )


LOOP


UPDATE employeeDetails SET EmpName = i.ipEmpName,

EmpSal= i.ipEmpSal,

JoinDate = i.ipJoinDate

WHERE DeptNo = i.ipDeptNo;
END LOOP;

END UPDATE_EMPLOYEE_DATA;

/
SQL Server Database
If you are working with SQL Server as a Database and want to do same operation as above then see the following link
http://www.riteshkk2000.blogspot.com/2005/06/sent-multiple-data-to-storeprocedure.html