Code to check whether word,excel has been indtalled in system

This is code to check whether microsoft word or microsoft excel has been installed in your system..

import namespace microsoft.win32 'namespace to acess the Registry classes.

Dim RegAppli As RegistryKey
RegAppli = Registry.ClassesRoot.OpenSubKey("excel.application")
'in case of checking word installed,the above code should be RegAppli = 'Registry.ClassesRoot.OpenSubKey("word.application")
If RegAppli Is Nothing Then
MsgBox("Microsoft Excel has not been installed")
Else
MsgBox("Microsoft Excel has been installed")
RegAppli.Close()
End If

Query to show the connection as per user and database

the query to find the users having how how many connections are present in which database.

Code:

SELECT LOGINAME AS 'LOGIN NAME',COUNT(DBID) AS 'NO OF CONNECTIONS EXIST',
DB_NAME(DBID) AS 'DATABASE NAME'
FROM SYS.SYSPROCESSES
WHERE DBID > 0
GROUP BY DBID, LOGINAME

Very Simple Query to Print All the Months

just run the below query to print all the months from January to December(without using loop)
code:

;WITH santosh
AS
(
SELECT 1 AS [Month]
UNION ALL
SELECT [Month] +1 FROM santosh WHERE [Month]<12
)
SELECT [Month] [Month], datename(month,dateadd(month, [Month] - 1, 0)) [Month Name] FROM santosh
GO

how to insert the records using sqlBulkCopy(bulk Insert)

This Example will help you to insert the records in a datatbse using BulkCopy.
If you have huge amount of data to be entered at a time then this bulk copy concept is very easy and faster to insert the records...

Step 1: drag all the textboxes like Id,Name,Address etc. into your form.
Step 2: drag a button and gridview into your form..
Step 3:Dim _dtUserdtls As New DataTable 'public declaration
Step 4: in page load write the below code
'adding the columns dynamically
_dtUserdtls.Columns.Add("Id")
_dtUserdtls.Columns.Add("Name")
_dtUserdtls.Columns.Add("Address")
_dtUserdtls.Columns.Add("Salary")
_dtUserdtls.Columns.Add("Email")
_dtUserdtls.Columns.Add("DOB")
_dtUserdtls.Columns.Add("State")
_dtUserdtls.Columns.Add("Country")
Step 5: write the below code in button click event.
Dim dr As DataRow
dr = _dtUserdtls.NewRow()
dr("Id") = txtId.Text.ToString()
dr("Name") = txtName.Text.ToString()
dr("Address") = txtAddress.Text.ToString()
dr("Salary") = txtSalary.Text.ToString()
dr("Email") = txtEmail.Text.ToString()
dr("DOB") = dtpDOB.Text
dr("State") = cmbStatus.Text
dr("Country") = cmbCountry.Text
DgvDisplay.DataSource = _dtUserdtls
Step 6 :
Dim bcp As New SqlBulkCopy(DataLayer.ConnectionString(), SqlBulkCopyOptions.UseInternalTransaction)
bcp.DestinationTableName = "tb_UserMaster" 'table name has to be insert
'all the below columns mapping should match with source column(datatable column name) and destination column(table column name)
bcp.ColumnMappings.Add("Id", "Id")
bcp.ColumnMappings.Add("Name", "Name")
bcp.ColumnMappings.Add("Address", "Address")
bcp.ColumnMappings.Add("Salary", "Salary")
bcp.ColumnMappings.Add("Email", "Email")
bcp.ColumnMappings.Add("DOB", "DOB")
bcp.ColumnMappings.Add("State", "State")
bcp.ColumnMappings.Add("Country", "Country")
bcp.WriteToServer(_dtUserdtls)

Note:if you want to insert all the gridview rows into table then assign all the rows ivalues into datatable using datasource property and use this datatable for the above code
_dtUserdtls = Gridview1.datasource

hope this code will help you....
any doubts plz free to ask..

Code to get some important folder path and OS Version

Below code will help to get the important folder path and OS Version
VB.NET Code
Dim PersonalDir As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
Dim CookiesDir As String = Environment.GetFolderPath(Environment.SpecialFolder.Cookies)
'Path of the Exe
Dim Location As String = System.Reflection.Assembly.GetExecutingAssembly().Location
Dim os As OperatingSystem
os = Environment.OSVersion
MessageBox.Show(os.Version.ToString()) 'OS Version
MessageBox.Show(os.Platform.ToString()) 'OS Platform

C#.NET Code
string PersonalDir = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string CookiesDir = Environment.GetFolderPath(Environment.SpecialFolder.Cookies);
//Path of the Exe
string Location = System.Reflection.Assembly.GetExecutingAssembly().Location;
OperatingSystem os = default(OperatingSystem);
os = Environment.OSVersion;
MessageBox.Show(os.Version.ToString()); //OS Version
MessageBox.Show(os.Platform.ToString()); //OS Platform

Add/Remove Columns of a DataTable Dynamically/RunTime

if u want to add some of the columns in ur existing datatable in runtime then the below code will be very usefull....
let ur datatable columns will be (ID,Name) before adding the columns into the datattable(dttable)
and now u want add two more columns into the datatable(dttable) then below will the code for this
VB.NET Code
Dim CreatedDate As DataColumn
Dim Address As DataColumn

CreatedDate = New DataColumn("CreatedDate", System.Type.GetType("System.DateTime"))
'if u want to set the same values for all the rows then write the below code other 'wise just remove the below line
CreatedDate.DefaultValue = Now.Date 'set the default value for all the rows
If dttable.Columns.Contains("CreatedDate") = True Then
dttable.Columns.RemoveAt("CreatedDate")
dtTable.Columns.Add(CreatedDate)
Else
dtTable.Columns.Add(CreatedDate)
End If

Address = New DataColumn("Address", System.Type.GetType("System.String"))
Address.DefaultValue = "India" 'set the default value for all the rows
If dtTable.Columns.Contains("Address") = True Then
dtTable.Columns.Remove("Address")
dtTable.Columns.Add(Address)
Else
dtTable.Columns.Add(Address)
End If
Now ur datatable(dttable) columns will be (ID,Name,CreatedDate,Address)

C#.NET Code
DataColumn CreatedDate = null;
DataColumn Address = null;

CreatedDate = new DataColumn("CreatedDate", System.Type.GetType("System.DateTime"));
CreatedDate.DefaultValue = DateAndTime.Now.Date;
if (dttable.Columns.Contains("CreatedDate") == true) {
dttable.Columns.RemoveAt("CreatedDate");
dtTable.Columns.Add(CreatedDate);
} else {
dtTable.Columns.Add(CreatedDate);
}

Address = new DataColumn("Address", System.Type.GetType("System.String"));
//if u want to set the same values for all the rows then write the below code other //wise just remove the below line
Address.DefaultValue = "India"; 'set the default value for all the rows
if (dtTable.Columns.Contains("Address") == true) {
dtTable.Columns.Remove("Address");
dtTable.Columns.Add(Address);
} else {
dtTable.Columns.Add(Address);
}

Code to Download the Files from Internet

Below Code will help you download files from internet.
it's very simple code just follow the few steps.
Step 1:Drag two textboxes a two button to ur form.
Step 2 : Just enter the Download Link in Download Location textbox
Step 3 : Just enter the path where u want to save the file.
Note:Download Link path and save as path should be correct.
Step 4 :Just Click the save as Button
Refer the Image Below
VB.NET Code
Private Sub btnSaveAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveAs.Click
Try
If txtLocation.Text.Trim = String.Empty Then
MessageBox.Show("Please Enter the Location from where you want to download", "San Test", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtLocation.Focus()
Exit Sub
ElseIf txtSaveas.Text.Trim.Trim = String.Empty Then
MessageBox.Show("Please Enter the Location to where you want to Save teh File", "San Test", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtSaveas.Focus()
Exit Sub
Else
My.Computer.Network.DownloadFile(txtLocation.Text, txtSaveas.Text & "\Santosh.jpg")
End If
Catch ex As Exception
MessageBox.Show("Please Enter the Correct Path", "San Test", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtSaveas.Focus()
End Try
End Sub

Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
C#.NET Code
private void btnSaveAs_Click(System.Object sender, System.EventArgs e)
{
try {
if (txtLocation.Text.Trim == string.Empty) {
MessageBox.Show("Please Enter the Location from where you want to download", "San Test", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtLocation.Focus();
return;
} else if (txtSaveas.Text.Trim.Trim == string.Empty) {
MessageBox.Show("Please Enter the Location to where you want to Save teh File", "San Test", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtSaveas.Focus();
return;
} else {
My.Computer.Network.DownloadFile(txtLocation.Text, txtSaveas.Text + "\\Santosh.jpg");
}
} catch (Exception ex) {
MessageBox.Show("Please Enter the Correct Path", "San Test", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtSaveas.Focus();
}
}

private void btnClose_Click(System.Object sender, System.EventArgs e)
{
this.Close();
}