Wednesday, October 9, 2013

Log File in VB.Net

Saved Log File

Imports System.IO.StreamWriter

Private Sub log_file()
        Try
            Dim s As IO.StreamWriter
            Dim portfolioPath As String = My.Application.Info.DirectoryPath
            If Not IO.Directory.Exists("C:\Users\Arka\Desktop\Arka_emp") Then
                IO.Directory.CreateDirectory("C:\Users\Arka\Desktop\Arka_emp")
                IO.File.Create("C:\Program Files\log.txt").Close()
                s = New IO.StreamWriter("C:\Program Files\log.txt", True)
                s.WriteLine(Date.Now & " Insert ERROR in Name " & dt.Rows(x)(1))
                s.Flush()
                s.Close()
            Else
                s = New IO.StreamWriter("C:\Program Files\log.txt", True)
                s.WriteLine(Date.Now & " Insert ERROR in Name " & dt.Rows(x)(1))
                s.Flush()
                s.Close()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

Runtime Log File

Private Sub log_file()

        System.Diagnostics.Process.Start("notepad")
        Dim found As Boolean = False
        Do Until found = True
            System.Threading.Thread.Sleep(100)
            For Each p As Process In Process.GetProcesses
                If p.ProcessName = "notepad" Then
                    found = True
                End If
            Next
        Loop
        SendKeys.Send("Whatever you want to say..." & Environment.NewLine & "And you can make new lines too!")
    End Sub

Friday, October 4, 2013

Importing data from csv to sql server using vb.net

Imports System
Imports System.IO.File

Dim dlg As New OpenFileDialog
Dim ocon As System.Data.OleDb.OleDbConnection
Dim ocmd As System.Data.OleDb.OleDbDataAdapter
Dim ds As System.Data.DataSet
Dim path As String = "My Filename"
Dim path1 As String
Dim dt As New DataTable

Private Sub Upload_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Upload_btn.Click
        Dim dt As New DataTable
        dlg.Filter = "CSV Fiels|*.csv|Any Files|*.*"
        If dlg.ShowDialog = System.Windows.Forms.DialogResult.OK Then
            path = dlg.FileName
            path1 = dlg.FileName
            path = System.IO.Path.GetDirectoryName(path)
            path1 = System.IO.Path.GetFileName(path1)
            ocon = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & path & ";Extended Properties='Text;HDR=Yes;IMEX=1;FMT=Delimited(,)';")
            ocmd = New System.Data.OleDb.OleDbDataAdapter("select * from " & path1 & "", ocon)
            ocmd.Fill(dt)
            ocon.Close()
***********************FOR EXCEL PURPOSE**************************
'Imports Microsoft.Office.Interop
'Imports Microsoft.Office.Interop.Excel
            'ocon = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data source='" & path & "';" & "Extended Properties= Excel 12.0;")
            'ocmd = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", ocon)
            'ds = New System.Data.DataSet
            'ocmd.Fill(ds)
            'ocon.Close()
            'dt = ds.Tables(0)
*********************************************************************************
        End If

        Try
            Dim x As Integer
            i = dt.Rows.Count
            While (x <= i - 1)
                Dim a As String = dt.Rows(x)(3)
                Dim dat As String = a.Replace("'", "")
                dat = Mid(dat, 5, 4) + Mid(dat, 3, 2) + Mid(dat, 1, 2)
                sql = "Insert into ABCD values(@id, @name, @add, @dob)"
                cmd = New SqlCommand(sql, con)
                cmd.Parameters.Add(New SqlClient.SqlParameter("@id", SqlDbType.Int)).Value = dt.Rows(x)(0)
                cmd.Parameters.Add(New SqlClient.SqlParameter("@name", SqlDbType.VarChar)).Value = dt.Rows(x)(1)
                cmd.Parameters.Add(New SqlClient.SqlParameter("@add", SqlDbType.VarChar)).Value = dt.Rows(x)(2)
                cmd.Parameters.Add(New SqlClient.SqlParameter("@dob", SqlDbType.VarChar)).Value = dat
                cmd.ExecuteNonQuery()
                x = x + 1
            End While
            MsgBox("Insert Data Successful", MsgBoxStyle.Information, "Data Upload")
            dt = Nothing
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub