Friday, October 31, 2008
Thursday, October 16, 2008
Getting Unique field of a column form a table( also get distinct count of column of datatable)
{
DataTable dt = new DataTable(TableName);
dt.Columns.Add(FieldName, SourceTable.Columns[FieldName].DataType);
object LastValue = null;
foreach (DataRow dr in SourceTable.Select("", FieldName))
{
if (LastValue == null || !(ColumnEqual(LastValue, dr[FieldName])))
{
LastValue = dr[FieldName];
dt.Rows.Add(new object[] { LastValue });
}
}
//if (ds != null)
// ds.Tables.Add(dt);
return dt;
}
private bool ColumnEqual(object A, object B)
{
// Compares two values to see if they are equal. Also compares DBNULL.Value.
// Note: If your DataTable contains object fields, then you must extend this
// function to handle them in a meaningful way if you intend to group on them.
if (A == DBNull.Value && B == DBNull.Value) // both are DBNull.Value
return true;
if (A == DBNull.Value || B == DBNull.Value) // only one is DBNull.Value
return false;
return (A.Equals(B)); // value type standard comparison
}
Sunday, September 14, 2008
Using Generic lists with crystal reports in .net
Public Sub ReportDocumentListDetails(ByVal rptPath As String, ByVal reportname As String, ByVal rptdoc As CrystalDecisions.CrystalReports.Engine.ReportDocument, _
ByVal arListName As System.Collections.ArrayList, ByVal arListValue As System.Collections.ArrayList)
Dim rptViewer As New CrystalDecisions.Windows.Forms.CrystalReportViewer()
Dim ds As DatasetReport = New DatasetReport
If (arListName.Count = arListValue.Count) Then
For i As Integer = 0 To arListName.Count - 1
rptdoc.SetParameterValue(arListName(i).ToString, arListValue(i))
Next
End If
Dim objExOpt As CrystalDecisions.Shared.ExportOptions
Dim objDiskOpt As New CrystalDecisions.Shared.DiskFileDestinationOptions
objDiskOpt.DiskFileName = "F:\nath\" & reportname & ".pdf"
objExOpt = rptdoc.ExportOptions
objExOpt.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile
objExOpt.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat
objExOpt.DestinationOptions = objDiskOpt
rptdoc.Export(objExOpt)
rptdoc.Close()
End Sub
Export options of crystal reports and passing parameters to crystal reports in .net
Public Sub ReportDocumentDetails(ByVal rptPath As String, ByVal reportname As String, ByVal dtReport As DataTable, _
ByVal arListName As System.Collections.ArrayList, ByVal arListValue As System.Collections.ArrayList)
Dim rptViewer As New CrystalDecisions.Windows.Forms.CrystalReportViewer()
Dim ds As DatasetReport = New DatasetReport
Dim rptdoc As CrystalDecisions.CrystalReports.Engine.ReportDocument = New CrystalDecisions.CrystalReports.Engine.ReportDocument
rptdoc.Load(rptPath)
rptdoc.SetDataSource(CType(dtReport, DataTable))
If (arListName.Count = arListValue.Count) Then
For i As Integer = 0 To arListName.Count - 1
rptdoc.SetParameterValue(arListName(i).ToString, arListValue(i))
Next
End If
Dim objExOpt As CrystalDecisions.Shared.ExportOptions
Dim objDiskOpt As New CrystalDecisions.Shared.DiskFileDestinationOptions
objDiskOpt.DiskFileName = "F:\nath\" & reportname & ".pdf"
objExOpt = rptdoc.ExportOptions
objExOpt.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile
objExOpt.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat
objExOpt.DestinationOptions = objDiskOpt
rptdoc.Export(objExOpt)
rptdoc.Close()
End Sub
Monday, September 8, 2008
Working with Attachments in Outlook Email using .net 2.0
Dim oNameSpace As Microsoft.Office.Interop.Outlook.NameSpace = olApp.GetNamespace("MAPI")
Dim oOutL As New Outlook.Application
Dim oMail As Outlook.MailItem
Dim objFolder As MAPIFolder = oNameSpace.GetDefaultFolder(OlDefaultFolders.olFolderInbox)
Dim oItems As Items = objFolder.Items
Dim twmp As Integer = 15
Do While (twmp >= 0)
oMail = CType(objFolder.Items((objFolder.Items.Count - twmp)), MailItem)
If (oMail.UnRead = True) Then
If (oMail.Subject = "Document") Then
oMail.Attachments(1).SaveAsFile("E:\VBABonds\kaushik\" & oMail.Attachments(1).FileName)
End If
End If
twmp = (twmp - 1)
Loop
Friday, September 5, 2008
Open word file using vb.net 2,0 in windows application

To open ms word file using vb.net 2.0 in windows application
Add reference microsoft.word 12.o object from com tab.

If (System.IO.File.Exists(txtBrowse.Text)) Then
Dim test As Microsoft.Office.Interop.Word.Application
test = New Microsoft.Office.Interop.Word.Application
test.Documents.Open(txtBrowse.Text)
test.OpenAttachmentsInFullScreen = True
test.Visible = True
End If
Thursday, September 4, 2008
Storing and Downloading ms word file in ms access file using VB.NET 2.0
Storing a ms word file in access database using VB.NET 2.0
Dim objcon As System.Data.OleDb.OleDbConnection = CommonFunctions.clsCommonFunctions.OleDbConnections()
Dim objcmd As System.Data.OleDb.OleDbCommand = New OleDb.OleDbCommand("", objcon)
Dim strText As String = "update bonds set Attachment=@Attachment where bondsid=" & txtDBaseRefNo.Text
objcon.Open()
Dim fs As New System.IO.FileStream _
(TextBox1.Text, System.IO.FileMode.OpenOrCreate, _
System.IO.FileAccess.Read)
Dim MyData(fs.Length) As Byte
fs.Read(MyData, 0, fs.Length)
fs.Close()
par.Value = MyData
objcmd.CommandText = strText
objcmd.Parameters.Add(par)
Dim i As Integer = objcmd.ExecuteNonQuery()
fs = Nothing
objcon.Close()
Downloading ms word file from ms access database using VB.NET 2.0
Dim command As System.Data.OleDb.OleDbCommand = New System.Data.OleDb.OleDbCommand( _
"select Attachment from bonds where bondsid=" & txtDBaseRefNo.Text, objcon)
' Writes the BLOB to a file (*.bmp).
Dim stream As System.IO.FileStream
' Streams the binary data to the FileStream object.
Dim writer As System.IO.BinaryWriter
' The size of the BLOB buffer.
Dim bufferSize As Integer = 100
' The BLOB byte() buffer to be filled by GetBytes.
Dim outByte(bufferSize - 1) As Byte
' The bytes returned from GetBytes.
Dim retval As Long
' The starting position in the BLOB output.
Dim startIndex As Long = 0
' The publisher id to use in the file name.
Dim pubID As String = ""
' Open the connection and read data into the DataReader.
objcon.Open()
Dim reader As System.Data.OleDb.OleDbDataReader = command.ExecuteReader(CommandBehavior.SequentialAccess)
Do While reader.Read()
' Get the publisher id, which must occur before getting the logo.
'pubID = reader.GetString(0)
' Create a file to hold the output.
stream = New System.IO.FileStream( _
"C:\Documents and Settings\Gopinath\Desktop\doccc1111.doc", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write)
writer = New System.IO.BinaryWriter(stream)
' Reset the starting byte for a new BLOB.
startIndex = 0
' Read bytes into outByte() and retain the number of bytes returned.
retval = reader.GetBytes(0, startIndex, outByte, 0, bufferSize)
' Continue while there are bytes beyond the size of the buffer.
Do While retval = bufferSize
writer.Write(outByte)
writer.Flush()
' Reposition start index to end of the last buffer and fill buffer.
startIndex += bufferSize
retval = reader.GetBytes(0, startIndex, outByte, 0, bufferSize)
Loop
' Write the remaining buffer.
writer.Write(outByte, 0, retval - 1)
writer.Flush()
' Close the output file.
writer.Close()
stream.Close()
Loop
' Close the reader and the connection.
reader.Close()
objcon.Close()