Translate This Blog

Basic Coding

Coding used in the web services

1- How to add Column in the Data Table

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

2- How to clear the string Builder object

Dim LSql As New System.Text.StringBuilder
LSql.Length = 0

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

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

4- Set the column caption exist in the table
Dim SizeTable As New DataTable("SizeTable")
SizeTable.Columns("Size").Caption = "Size Name"

5- How to read data from SqlReader

Dim Ctable as new dataTable
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

Coding used in the web services

1- How to add Column in the Data Table
SizeTable.Columns.Add("Size", GetType(String))
SizeTable.Columns.Add("ID", GetType(Decimal))

2- How to clear the string Builder object
Dim LSql As New System.Text.StringBuilder
LSql.Length = 0

3- How to Open connection where connection string exist in the web config file
Dim SqlConn As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("MyConnectionString").ToString())
SqlConn.Open()

4- Set the column caption exist in the table

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

5- How to read data from SqlReader
Dim Ctable as new dataTable
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

6- Add DataTable in the Dataset

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

7- 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")

8- How to use Transaction for deleting the record

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()

9- 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") + "'"

10- 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()

11- 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





11- 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


12- How to print Report

Public Function ReturnPrintReport(ByVal DocID As Decimal, ByVal PrintType As Decimal) As Byte()
Dim MastTable As New DSCTPrint.MastTableDataTable
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


13- 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

14- 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"


15- 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






Previous
Next Post »
Thanks for your comment