How To Put Commas in amount

Public Function CommaSeparatedAmt(ByVal Amount As String) As String
Dim strAmt As String = ""
Dim s As String = ""
Dim s1 As String = ""
Dim s2 As String = ""
Dim AmtString As String
Dim DecAmt As String
If Amount.Contains(".") Then
Dim strArr As String() = Split(Amount, ".")
AmtString = strArr(0)
DecAmt = strArr(1)
Else
AmtString = Amount
DecAmt = "00"
End If
Select Case AmtString.Length
Case Is < 4
strAmt = AmtString
Case 4
s = AmtString.ToString.Substring(0, 1)
s1 = AmtString.ToString.Substring(1, 3)
strAmt = s + "," + s1
Case 5
s = AmtString.ToString.Substring(0, 2)
s1 = AmtString.ToString.Substring(2, 3)
strAmt = s + "," + s1
Case 6
s = AmtString.ToString.Substring(0, 1)
s1 = AmtString.ToString.Substring(1, 2)
s2 = AmtString.ToString.Substring(3, 3)
strAmt = s + "," + s1 + "," + s2
Case 7
s = AmtString.ToString.Substring(0, 1)
s1 = AmtString.ToString.Substring(1, 3)
s2 = AmtString.ToString.Substring(4, 3)
strAmt = s + "," + s1 + "," + s2
Case 8
s = AmtString.ToString.Substring(0, 2)
s1 = AmtString.ToString.Substring(2, 3)
s2 = AmtString.ToString.Substring(5, 3)
strAmt = s + "," + s1 + "," + s2
Case 9
s = AmtString.ToString.Substring(0, 3)
s1 = AmtString.ToString.Substring(3, 3)
s2 = AmtString.ToString.Substring(6, 3)
strAmt = s + "," + s1 + "," + s2
Case Else
strAmt = AmtString
End Select
Return strAmt & "." & DecAmt
End Function

Convert amount in words

Public Class ClsRupees
Public Function Return_Rupee_String(ByVal Number As Double) As String
Dim IntNumber As Long
Dim FloatNumber As Decimal

If Int(Number) <> Number Then
IntNumber = Int(Number)
FloatNumber = Format((Number - IntNumber), "0.00") * 100
Return_Rupee_String = Convert_To_String(IntNumber)
Return_Rupee_String = UCase("Rs. " + " " + Return_Rupee_String + " AND " & " Paise " & " " + Convert_To_String(FloatNumber) + " Only/-")
Else
Return_Rupee_String = UCase("Rs. " + " " + Convert_To_String(Number) + " Only / -")
End If
End Function
Public Function Convert_To_String(ByVal Number As Double) As String
Dim Cr, La, Th, Hu, Te As Integer
Dim RString As String
Dim sFraction As Long
Dim Fracpos As Long

RString = ""

Number = Math.Abs(Number)

Fracpos = InStr(1, Trim(Number), ".")
If Fracpos > 0 Then
sFraction = Mid(Trim(Number), Fracpos + 1, Len(Trim(Number) - Fracpos))
End If

If Number >= 10000000 Then
Cr = Number \ 10000000
Number = Number Mod 10000000
RString = RString + Return_String(Int(Cr)) + " " + "CRORE "
End If
If Number >= 100000 Then
La = Number \ 100000
Number = Number Mod 100000
RString = RString + Return_String(Int(La)) + " " + "LAKH "
End If
If Number >= 1000 Then
Th = Number \ 1000
Number = Number Mod 1000
RString = RString + Return_String(Int(Th)) + " " + "THOUSAND "
End If
If Number >= 100 Then
Hu = Number \ 100
Number = Number Mod 100
RString = RString + Return_String(Int(Hu)) + " " + "HUNDRED "
End If
If Number < 100 Then
Te = Number
RString = RString + Return_String(Int(Te))
End If
If Val(sFraction) <> 0 Then
If Val(Left(sFraction, 1)) <> 0 Then
If Val(sFraction) < 10 Then
RString = RString + " AND " + Trim(Get_Paise(Val(sFraction * 10)))
Else
RString = RString + " AND " + Trim(Get_Paise(Val(sFraction)))
End If
Else
RString = RString + " AND " + Trim(Get_Paise(Val(sFraction)))
End If
End If

Convert_To_String = RString
End Function
Public Function Return_String(ByVal PassNum As Decimal) As String
Dim TaRupee As New DSRupeesTableAdapters.RupeesTableAdapter
Dim RupeeTable As New DSRupees.RupeesDataTable

RupeeTable = TaRupee.GetDataByAmount(PassNum)

If RupeeTable.Rows.Count > 0 Then
Return_String = RupeeTable(0).AmountString.ToString()
Else
Return_String = Under_Hundred_String(PassNum)
End If
RupeeTable.Dispose()
TaRupee.Dispose()
End Function
Private Function Under_Hundred_String(ByVal num As Integer) As String
Dim FirstString As String = ""
Dim SecondString As String = ""
Dim FirstPart As Integer
Dim SecondPart As Integer
Dim RsTable As New DSRupees.RupeesDataTable
Dim TARupees As New DSRupeesTableAdapters.RupeesTableAdapter

FirstPart = (Int(num / 10) * 10)
SecondPart = (num - FirstPart)
'-----------------
RsTable = TARupees.GetDataByAmount(Decimal.Parse(FirstPart))
If RsTable.Rows.Count > 0 Then
FirstString = RsTable(0).AmountString.ToString()
End If
'-----
RsTable = TARupees.GetDataByAmount(Decimal.Parse(SecondPart))
If RsTable.Rows.Count > 0 Then
SecondString = RsTable(0).AmountString.ToString()
End If
Under_Hundred_String = FirstString + " " + SecondString
End Function
Private Function Get_Paise(ByVal Number As Double) As String
Dim Cr As Double
Dim La As Double
Dim Th As Double
Dim Hu As Double
Dim Te As Double
Dim RString As String = ""

If Number >= 10000000 Then
Cr = Number / 10000000
Number = Number Mod 10000000
RString = RString + Trim(Return_String(Int(Cr))) + " " + "CRORE "
End If
If Number >= 100000 Then
La = Number / 100000
Number = Number Mod 100000
RString = RString + Trim(Return_String(Int(La))) + " " + "LAKH "
End If
If Number >= 1000 Then
Th = Number / 1000
Number = Number Mod 1000
RString = RString + Trim(Return_String(Int(Th))) + " " + "THOUSAND "
End If
If Number >= 100 Then
Hu = Number / 100
Number = Number Mod 100
RString = RString + Trim(Return_String(Int(Hu))) + " " + "HUNDRED "
End If
If Number < 100 Then
Te = Number
RString = RString + Trim(Return_String(Int(Te)))
End If
Get_Paise = RString
End Function
End Class

Directly Print Report

Public CrReport As Object
CrystalReportViewer1.ReportSource = CrReport

Dim Myform As New FrmSuppLedgerPreview
Dim DSPrint As New DSCLedgerPrint
DSPrint.Tables.Clear()

DSPrint.Tables.Add(Print_Detail())
Dim XReport As New CrSLDetail
XReport.SetDataSource(DSPrint)
Myform.CrReport = XReport
Myform.Show(Me)

How to apply transaction in typed dataset

Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Namespace DSBatchTableAdapters
Partial Public Class BatchMasterTableAdapter
Dim _Transaction As SqlTransaction
Public Property Transaction() As SqlTransaction
Get
Return _Transaction
End Get
Set(ByVal value As SqlTransaction)
_Transaction = value
_connection = _Transaction.Connection
If _adapter Is Nothing Then
Me.InitAdapter()
End If
For Each command As SqlCommand In Me.CommandCollection
command.Transaction = _Transaction
Next
Me.Adapter.InsertCommand.Transaction = _Transaction
Me.Adapter.UpdateCommand.Transaction = _Transaction
Me.Adapter.DeleteCommand.Transaction = _Transaction
End Set
End Property
Public Sub SetCommandTimeOut(ByVal PassTime As Integer)
For Each Command As SqlCommand In Me.CommandCollection
Command.CommandTimeout = PassTime
Next
End Sub
End Class
Partial Public Class BatchChildTableAdapter
Dim _Transaction As SqlTransaction
Public Property Transaction() As SqlTransaction
Get
Return _Transaction
End Get
Set(ByVal value As SqlTransaction)
_Transaction = value
_connection = _Transaction.Connection
If _adapter Is Nothing Then
Me.InitAdapter()
End If
For Each command As SqlCommand In Me.CommandCollection
command.Transaction = _Transaction
Next
Me.Adapter.InsertCommand.Transaction = _Transaction
Me.Adapter.UpdateCommand.Transaction = _Transaction
Me.Adapter.DeleteCommand.Transaction = _Transaction
End Set
End Property
Public Sub SetCommandTimeOut(ByVal PassTime As Integer)
For Each Command As SqlCommand In Me.CommandCollection
Command.CommandTimeout = PassTime
Next
End Sub
End Class
End Namespace

How to dictinct record from the data table by data view

Dim CustTable As New DataTable
Dim DTable As New DataTable
CustTable = Return_CustomerTable()
Dim DView As New DataView(CustTable)
DView.Sort = "Name"
DTable = DTV.ToTable()

How to track the checkbox status of the check list box

Dim Count As Decimal = 0
If ChkCC.CheckedItems.Count > 0 Then
For Count = 0 To ChkCC.Items.Count - 1
If ChkCC.GetItemChecked(Count) = True Then
Count = Count + 1
End If
Next
End If
MsgBox(Count & " items checked.")

How to dictinct record from the data table by data view

Dim Col(0) As String
Col(0) = "CustName"
Dim CustTable As New DataTable
Dim DTable As New DataTable

CustTable = Return_CustomerTable()
Dim DTV As New DataView(CustTable)
DTable = DTV.ToTable(True, Col)

How to Open image from byte array

Dim Gimage As Byte()
Dim TempPath As String
Dim FileName As String = "Image.jpg"
Gimage = Return_ImageByte()
TempPath = System.IO.Path.GetTempPath()
System.IO.File.WriteAllBytes(TempPath & FileName, Gimage)
System.Diagnostics.Process.Start(TempPath & FileName)

How to Read image from open file dialog in byte array

Dim GImage As Byte()
ReDim GImage(OpenFileDialog1.OpenFile.Length)
OpenFileDialog1.OpenFile.Read(GImage, 0, GImage.Length)

How to Show image in picture box from the byte array

Dim MStream As IO.MemoryStream
Dim GImage As Byte()
GImage = GetImageByte()
MStream = New System.IO.MemoryStream(GImage)
PictureBox1.Image = Image.FromStream(MStream)

How to track the check box of the data grid view

Dim Y As DataGridViewCheckBoxCell
Dim Count As Decimal = 0
For Each DGVRow As DataGridViewRow In DataGridView1.Rows
Y = CType(DGVRow.Cells("Chk"), DataGridViewCheckBoxCell)
If Y.Value = True Then
Count = Count + 1
End If
Next
MsgBox(Count & " items checked.")

How to Allow only numeric value in the text box

Private Sub Txtopening_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Txtopening.KeyPress
If Asc(e.KeyChar) > 47 And Asc(e.KeyChar) < 58 Or Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 46 Then
If Txtopening.Text.Contains(".") Then
If Asc(e.KeyChar) = 46 Then
e.Handled = True
End If
Else
e.Handled = False
End If
Else
e.Handled = True
End If
End Sub

How To bind the table with the combo box

Dim DTable As New DataTable
DTable = ReturnCustTable()
If DTable.Rows.Count > 0 Then
CmbBrand.DataSource = DTable
CmbBrand.ValueMember = DTable.Columns("CustID").ColumnName
CmbBrand.DisplayMember = DTable.Columns("CustName").ColumnName
End If

How Check all check box of list view on click Select all Check box

Private Sub ChkAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkAll.CheckedChanged
If ChkAll.CheckState = CheckState.Checked Then
For Each X As ListViewItem In ListView1.Items
X.Checked = True
Next
Else
For Each X As ListViewItem In ListView1.Items
X.Checked = False
Next
End If
End Sub

How to add sub Item in the list view

Dim W As New ListViewItem
For Each DRow As DataRow In SuppTable.Rows
W.Text = SuppTable.Rows(0).Item("Name").ToString()
W.SubItems.Add(SuppTable.Rows(0).Item("Address").ToString())
ListView1.Items.Add(W)
Next

How to Read value from cell of data grid view

Dim CustName As String = ""
For Each drRow As DataGridViewRow In DGVAmount.Rows
CustName = drRow.Cells("CustName"). FormattedValue
Next

How to set max length in data grid view or handle data grid view keypress event

Private Sub DGVAmount_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles DGVAmount.KeyPress
If DGVAmount.SelectedCells.Count > 0 Then
If DGVAmount.SelectedCells(0).ColumnIndex = DGVAmount.Columns("LfinalAmount").Index Then
Dim intMaxLength As Integer = 12 'Set your max length here
If dgvTxtBox Is Nothing = False Then
Dim str As String = dgvTxtBox.Text.ToString()
If Not str.Length < intMaxLength _
AndAlso Not e.KeyChar = ControlChars.Back Then
e.Handled = True
Beep()
Exit Sub
End If
End If
End If
End If
End Sub

How to allow only numeric value to type in data grid view or handle data grid view keypress event

Private dgvTxtBox As DataGridViewTextBoxEditingControl

Private Sub DGVAmount_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DGVAmount.EditingControlShowing

Dim ctrlName As String = e.Control.GetType.Name
If ctrlName = "DataGridViewTextBoxEditingControl" Then
dgvTxtBox = CType(e.Control, DataGridViewTextBoxEditingControl)
AddHandler dgvTxtBox.KeyPress, AddressOf DGVAmount_KeyPress
dgvTxtBox.Enabled = True
End If
End Sub

‘------------------------------------------------------------------

Private Sub DGVAmount_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles DGVAmount.KeyPress
If DGVAmount.SelectedCells.Count > 0 Then
If DGVAmount.SelectedCells(0).ColumnIndex = DGVAmount.Columns("LfinalAmount").Index Then
If dgvTxtBox Is Nothing = False Then
Dim str As String = dgvTxtBox.Text.ToString()
If Asc(e.KeyChar) > 47 And Asc(e.KeyChar) <= 57 Or e.KeyChar = "." Or Asc(e.KeyChar) = 8 Then
If str.Contains(".") Then
If Asc(e.KeyChar) = 46 Then
e.Handled = True
Else
e.Handled = False
End If
Else
e.Handled = False
End If
Else
e.Handled = True
End If
End If
End If
End If
End Sub

How to remove value from data grid view on press delete key or handle data grid view keydown event

Private Sub DGVAmount_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DGVAmount.KeyDown
If DGVAmount.Rows.Count > 0 Then
If DGVAmount.SelectedCells(0).ColumnIndex = DGVAmount.Columns("LfinalAmount").Index Then
If e.KeyCode = Keys.Delete Then
DGVAmount.SelectedCells(0).Value = ""
End If
End If
End If
End Sub

How to add row in the Data grid view one by one fron the datatable

For Each Dr As DataRow In RTable.rows
LCount = DGVAmount.Rows.Add()
DGVAmount.Rows(LCount).Cells("CustID").Value = Dr.Item("ID")
DGVAmount.Rows(LCount).Cells("CustName").Value = Dr.Item("Name")
Next

How to create Write only property

Dim DocID As Decimal
Public WriteOnly Property PassDocumentID()
Set(ByVal value)
DocID = value
End Set
End Property

Creating Thumb Images

Public Function Generate_Thumb(ByVal GImage As Byte()) As Byte()
Dim Mstream As New System.IO.MemoryStream
Mstream.Write(GImage, 0, GImage.Length)
Dim BIMage As Bitmap
Dim Thumb As Bitmap
BIMage = Bitmap.FromStream(Mstream)
Thumb = CreateThumbNail(BIMage, 120, 120)
Dim imageStream As New System.IO.MemoryStream
Thumb.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim imageContent As Byte()
imageContent = imageStream.GetBuffer()
Return imageContent
End Function

‘---------------------------------------------------------

Private Function CreateThumbNail(ByVal postedFile As Bitmap, ByVal width As Integer, ByVal height As Integer) As Bitmap
Dim bmpOut As System.Drawing.Bitmap
Dim Ratio As Decimal
Dim NewWidth As Integer
Dim NewHeight As Integer
'*** If the image is smaller than a thumbnail just return it
If postedFile.Width < width AndAlso postedFile.Height < height Then
Return postedFile
End If
If (postedFile.Width > postedFile.Height) Then
Ratio = Convert.ToDecimal(width / postedFile.Width)
NewWidth = width

Dim Temp As Decimal = postedFile.Height * Ratio
NewHeight = Convert.ToInt32(Temp)
Else
Ratio = Convert.ToDecimal(height / postedFile.Height)
NewHeight = height

Dim Temp As Decimal = postedFile.Width * Ratio
NewWidth = Convert.ToInt32(Temp)
End If
bmpOut = New Bitmap(NewWidth, NewHeight)

Dim g As Graphics = Graphics.FromImage(bmpOut)
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
g.FillRectangle(Brushes.White, 0, 0, NewWidth, NewHeight)
g.DrawImage(postedFile, 0, 0, NewWidth, NewHeight)
postedFile.Dispose()
Return bmpOut
End Function

Access the crystal report text object in the code

Dim XReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim LText As Object
XReport.Load(Server.MapPath(".\Reports\CrPrint.rpt"), CrystalDecisions.Shared.OpenReportMethod.OpenReportByDefault)
LText = XReport.ReportDefinition.Sections("Section5").ReportObjects("TxtCreatedBy")
LText.text = "Printed By : Gaurav Agrawal"

How to create datatable from sqlreader

Public Function ReaderToTable(ByVal LReader As SqlDataReader) As Data.DataTable
Dim LTable As New Data.DataTable
Dim I As Integer
For I = 0 To LReader.FieldCount - 1
LTable.Columns.Add(LReader.GetName(I).ToString(), LReader.GetFieldType(I))
Next
Do While LReader.Read()
Dim DRow As DataRow
DRow = LTable.Rows.Add()
For I = 0 To LReader.FieldCount - 1
DRow.Item(LTable.Columns(I).ColumnName) = LReader.Item(I)
Next
Loop
LTable.TableName = "Reader"
Return LTable
End Function

How to print crystal Report

Public Function ReturnPrintReport(ByVal DocID As Decimal, ByVal PrintType As Decimal) As Byte()
Dim MastTable As New DataTable
Dim XReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument()
Dim ArrReport() As Byte
Dim LDS As New DataSet
Dim FileAttach As System.IO.MemoryStream
MastTable = Write_MastTable(DocID)
If MastTable.Rows.Count > 0 Then
LDS.Tables.Add(MastTable)
XReport.Load(Server.MapPath(".\Reports\CrPrint.rpt"), CrystalDecisions.Shared.OpenReportMethod.OpenReportByDefault)
XReport.SetDataSource(LDS)
Select Case PrintType
Case 1 'For Crystal Report
FileAttach = XReport.ExportToStream(CrystalDecisions.Shared.ExportFormatType.CrystalReport)
Case 2 'For PDF File
FileAttach = XReport.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)
End Select
ArrReport = FileAttach.ToArray()
Else
ArrReport = Nothing
End If
Return ArrReport
End Function

How to write byte in the file

Dim Fs As System.IO.FileStream
Dim TempFileName As String
Dim RBytes() As Byte
TempFileName = System.IO.Path.GetTempFileName()
Fs = New System.IO.FileStream(TempFileName, IO.FileMode.Create, IO.FileAccess.Write)
Fs.Write(RBytes, 0, RBytes.Length)
Fs.Close()
Return TempFileName

How to set file size in KB,MB,Byte

Dim Size As Decimal = 0
Dim Title As String = ""
Dim TStr As String = ""
Dim FileSize As Decimal = 0
FileSize = PRow.Item("FileSize")
If FileSize > 1024 Then
If FileSize > 1024 * 1024 Then
Size = Decimal.Round(FileSize / (1024 * 1024), 2)
Title = " MB"
TStr = Size.ToString & Title
Else
Size = Decimal.Round(FileSize / 1024, 2)
Title = " KB"
TStr = Size.ToString & Title
End If
Else
Size = FileSize
Title = " Bytes"
TStr = Size.ToString() & Title
End If

How to insert the record in the table through AddWithValue

Dim SqlConn As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("MyConnectionString").ToString())
Dim OBjTrans As SqlTransaction = Nothing
Dim ObjCommand As New SqlCommand
SqlConn.Open()
ObjCommand.CommandText = "INSERT INTO CustMast(CustName) VALUES (@CustName)
ObjCommand.Parameters.AddWithValue("@CustName ","Gaurav Agrawal")
ObjCommand.Connection = SqlConn
ObjCommand.Transaction = OBjTrans
ObjCommand.ExecuteNonQuery()
ObjCommand.Dispose()
OBjTrans.Commit()
SqlConn.Close()

How to read only date from the sql database with time through Sql query

Select Count(*) From BillMast where Cast(Floor(Cast(BillDate as Float)) as datetime) = '" + Billdate.ToString("yyyy/MM/dd") + "'"

How to use Transaction for deleting the record

Deleting record in transaction

Dim SqlConn As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("MyConnectionString").ToString())
Dim OBjTrans As SqlTransaction = Nothing
Dim ObjCommand As New SqlCommand
SqlConn.Open()
OBjTrans = SqlConn.BeginTransaction()
ObjCommand.CommandText = "Delete From StockInMast WHERE RefNo =23 " ObjCommand.Connection = SqlConn
ObjCommand.Transaction = OBjTrans
ObjCommand.ExecuteNonQuery()
ObjCommand.Dispose()
OBjTrans.Commit()
SqlConn.Close()
SqlConn.Dispose()

How to get current date with time in AM/PM format

Dim CurDate As String
CurDate = System.DateTime.UtcNow.ToString("dd/MM/yyyy hh:mm tt")

Add DataTable in the Dataset

Dim LTable As New DataTable
Dim LDS As New DataSet
LDS.Tables.Add(LTable)

How to read data from SqlReader

Read data from database through sqlreader

Dim CReader As SqlDataReader
Do While CReader.Read()
Dr = CTable.Rows.Add
Dr("StyleID") = CReader.Item("StyleID").ToString()
Dr("StyleCode") = CReader.Item("StyleCode").ToString()
Loop

Set the column caption exist in the table

Set the Caption for the data column

Dim SizeTable As New DataTable("SizeTable")
SizeTable.Columns("Size").Caption = "Size Name"

How to Open connection where connection string exist in the web config file

Write the following code to open sqlconnection

Dim SqlConn As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("MyConnectionString").ToString())
SqlConn.Open()

How to clear the string Builder object

To clear the String Builder Object write the following code
Dim LSql As New System.Text.StringBuilder
LSql.Length = 0

How to add Column in the Data Table

Write the following code to add the column in the data table named as "SizeTable"

SizeTable.Columns.Add("Size", GetType(String))
SizeTable.Columns.Add("ID", GetType(Decimal))