Wednesday, March 31, 2010

Validate Email ID

Private Function ValidateEmailID(ByVal GetEmailID As String) As Boolean
If GetEmailID.IndexOf("") > -1 And GetEmailID.LastIndexOf(".") > -1 And GetEmailID.LastIndexOf(".") <> (GetEmailID.Length - 1) Then
Return True
Else
Return False
End If
End Function

Friday, February 5, 2010

List all Server name and their database names on local machine using VB.NET

CODE:

Imports System.Data.SqlClient
Imports System.Net
Imports System.IO
Imports Microsoft.SqlServer.Management.Smo ' Add Reference
Imports Microsoft.SqlServer ' Add Reference

Public Class Form3

Dim ConnectionStringBuilder As String

Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ServerTable = SmoApplication.EnumAvailableSqlServers(True)
Server_Name_LB.ValueMember = "Name"
Server_Name_LB.DataSource = ServerTable
End Sub

Public Function SaveConnectionStringToFile(ByVal ConnectionStr As String, ByVal FilePath As String, Optional ByVal ErrerMsg As String = "") As Boolean
Dim answer As Boolean = False
Dim ConnectionStringReader As StreamWriter
Try
ConnectionStringReader = New StreamWriter(FilePath)
ConnectionStringReader.Write(ConnectionStr)
ConnectionStringReader.Close()
answer = True
Catch Ex As Exception
ErrerMsg = Ex.Message
End Try
Return answer
End Function

Private Sub Server_Name_LB_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Server_Name_LB.SelectedIndexChanged
Database_Name_CB.Items.Clear()
If Server_Name_LB.SelectedIndex <> -1 Then
Dim serverName As String = Server_Name_LB.SelectedValue.ToString()
Dim server As Server = New Server(serverName)
Try
For Each database As Database In server.Databases
Database_Name_CB.Items.Add(database.Name)
Next
Catch ex As Exception
Dim exception As String = ex.Message
End Try
End If
End Sub

Private Sub Build_String_Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Build_String_Btn.Click
Try
ConnectionStringBuilder = "Data Source=" & Server_Name_LB.SelectedValue.ToString() & ";Initial Catalog=" & Database_Name_CB.Text & ";Integrated Security=True"
SaveConnectionStringToFile(ConnectionStringBuilder, System.Windows.Forms.Application.StartupPath & "\ConnectionString.txt", "Error Found")
MsgBox("Connection String: " & ConnectionStringBuilder)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

End Class

OUTPUT:







How to Print Crystal Report using PrintDocument control in VB.NET?

Try
Dim strReportPath As String = System.Windows.Forms.Application.StartupPath & "\CR.rpt"
If Not IO.File.Exists(strReportPath) Then
Throw (New Exception("Unable to locate report file:" & vbCrLf & strReportPath))
End If
rptDocument.Load(strReportPath)
CrystalReportViewer1.ReportSource = rptDocument
CrystalReportViewer1.Zoom(25)
Dim custompos As Integer
Dim customnumber As Integer
For custompos = 0 To PrintDocument1.PrinterSettings.PaperSizes.Count - 1
If PrintDocument1.PrinterSettings.PaperSizes.Item(custompos).PaperName = "US Std Fanfold" Then
customnumber = PrintDocument1.PrinterSettings.PaperSizes.Item(custompos).RawKind
Exit For
End If
Next
rptDocument.PrintOptions.PaperSize = customnumber
rptDocument.PrintToPrinter(1, True, 0, 0)
Catch ex As Exception
MsgBox(ex.Message)
End Try