UIC BSCS ‘07

Code and Snippets for vb.net 2005
RSS icon Email icon Home icon
  • Loading (.csv) file to listview

    Posted on July 29th, 2009 shyguy 2 comments

    Well, somebody is asking me for a code that will load up a (.csv) format to listview. In response to what he wanted, I decided to put it in here based on how I load a .csv file to listview. This article will be in relation to the previous post that I made. I hope this will help. Enjoy!


    Private Sub LoadData()
    Dim fileLookUp As New OpenFileDialog
    fileLookUp.Title = "Get File"
    fileLookUp.Filter = "Files (*.csv)|*.csv|All Files (*.*)|*.*"
    If fileLookUp.ShowDialog() = Windows.Forms.DialogResult.OK Then
    Me.txtFile.Text = fileLookUp.FileName
    mFileName = System.IO.Path.GetFileName(Me.txtFile.Text)
    mFolder = System.IO.Path.GetDirectoryName(Me.txtFile.Text)
    End If

    Dim ConnectionString, CommandText As String
    Dim conn As OleDb.OleDbConnection
    Dim Command As OleDbCommand
    Dim myString As String = txtFile.Text
    Dim myIndex As Integer = myString.LastIndexOf("\") + 1
    myString = myString.Substring(myIndex, myString.Length - myIndex)

    Try

    ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mFolder & "\;Extended Properties='text;HDR=Yes'"

    CommandText = "select * from " & myString & ""

    conn = New System.Data.OleDb.OleDbConnection(ConnectionString)
    Command = New System.Data.OleDb.OleDbCommand(CommandText, conn)

    conn.Open()

    Dim t As New DataTable
    ' initializing its column to complement on how many fields I want to return in my query command
    t.Columns.Add("v7#3")
    t.Columns.Add("F2")
    t.Columns.Add("F3")

    ' a method to access read-only the result set.
    Dim reader As OleDbDataReader = Command.ExecuteReader()
    While reader.Read()
    ' create new row
    Dim r As DataRow = t.NewRow()
    r(0) = reader("v7#3")
    r(1) = reader("F2")
    r(2) = reader("F3")

    ' add a row to a datatable
    t.Rows.Add(r)
    End While

    ' close reader
    reader.Close()
    ' close the connection

    conn.Close()

    For i As Integer = 0 To t.Rows.Count - 1
    Dim li As ListViewItem = ListView1.Items.Add(t.Rows(i)("v7#3").ToString())
    li.SubItems.Add(t.Rows(i)("F2").ToString())
    li.SubItems.Add(t.Rows(i)("F3").ToString())

    Next

    Catch ex As Exception
    MsgBox("Invalid Format! Please try another.")
    End Try
    End Sub

  • Saving Listview as (.csv) file

    Posted on July 16th, 2009 shyguy 3 comments

    It was easy for me to save a file on the database but saving a file as a (.csv) format is another issue for me before. Until I discover saving as a (.csv) format is just as simple as saving file on a database. All you just need to do on this is that to call the streamwriter to save the data as (.csv) format. I have here a sample codes below where the data the loaded on my listview was saved as (.csv) format. Hope you enjoy.

    Private Sub SaveAsFile()

    ‘They want to do a SaveAs, so find out what they want to name the file

    Dim saveFileDialog As SaveFileDialog = New SaveFileDialog()

    saveFileDialog.Title = “Save File”

    saveFileDialog.Filter = “Files (*.csv)|*.csv|All Files (*.*)|*.*”

    saveFileDialog.DefaultExt = “csv”

    saveFileDialog.AddExtension = True

    If saveFileDialog.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then

    filename = saveFileDialog.FileName

    End If

    Try
    Dim csvFileContents As New System.Text.StringBuilder
    Dim currentLine As String = String.Empty

    ‘Write out the column names as headers for the csv file.
    For columnIndex As Int32 = 0 To 0
    currentLine &= (String.Format(”mycolumn_name”))
    Next

    ‘Remove trailing comma

    csvFileContents.AppendLine(currentLine.Substring(0, currentLine.Length))
    currentLine = String.Empty

    ‘Write out the data.
    For Each item As ListViewItem In ListView1.Items

    For Each subItem As ListViewItem.ListViewSubItem In item.SubItems

    currentLine &= (String.Format(”"”{0}”",”, subItem.Text))

    Next
    ‘Remove trailing comma

    csvFileContents.AppendLine(currentLine.Substring(0, currentLine.Length - 1))
    currentLine = String.Empty

    Next

    ‘Create the file.
    Dim sw As New System.IO.StreamWriter(filename)
    sw.WriteLine(csvFileContents.ToString)
    sw.Flush()
    sw.Dispose()
    Catch ex As Exception

    End Try

    End Sub

  • Introducing Ultidev Cassini

    Posted on July 14th, 2009 shyguy No comments

    It’s been a big confusion on my mind before since I was thinking on how do I make my web application to run on cd. Well, while searching and looking around for a stand alone web server on the google I saw a Ultidev Cassini that really fits to what really I need and now I was using it. It was really a great product and free for everyone. If you want to explore more on how to used it just visit http://ultidev.com and explore how it works.

  • How to add groups on listview

    Posted on May 28th, 2009 shyguy No comments

    I have here a simple sample on how to group in a listview. I hope this simple code can help to someone needed this.

    listview1

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim group1 As New ListViewGroup(”Group 123″, HorizontalAlignment.Left)
    group1.Name = “Group 123″
    ListView1.Groups.Add(group1)

    With ListView1.Items
    .Add(”123″)
    .Item(.Count - 1).SubItems.Add(”ABC”)
    .Item(.Count - 1).SubItems.Add(”1/11/05″)
    .Item(.Count - 1).SubItems.Add(”1″)
    .Item(.Count - 1).SubItems.Add(”TV”)
    .Item(.Count - 1).Group = group1
    .Add(”123″) .Item(.Count - 1).SubItems.Add(”DEF”)
    .Item(.Count - 1).SubItems.Add(”1/11/05″)
    .Item(.Count - 1).SubItems.Add(”1″)
    .Item(.Count - 1).SubItems.Add(”VCD”)
    .Item(.Count - 1).Group = group1
    .Add(”123″)
    .Item(.Count - 1).SubItems.Add(”GHI”)
    .Item(.Count - 1).SubItems.Add(”1/11/05″)
    .Item(.Count - 1).SubItems.Add(”1″)
    .Item(.Count - 1).SubItems.Add(”TVRACK”)
    .Item(.Count - 1).Group = group1

    Dim group2 As New ListViewGroup(”Group 234″, HorizontalAlignment.Left)
    group2.Name = “Group 234″

    ListView1.Groups.Add(group2)
    .Add(”234″)
    .Item(.Count - 1).SubItems.Add(”KLM”)
    .Item(.Count - 1).SubItems.Add(”1/12/06″)
    .Item(.Count - 1).SubItems.Add(”1″)
    .Item(.Count - 1).SubItems.Add(”MIC”)
    .Item(.Count - 1).Group = group2
    End With
    End Sub

    End Class

  • How to make borderless form draggable?

    Posted on May 7th, 2009 shyguy 7 comments

    Making a form without a border is nice to see especially when the user don’t want his form to minimize and maximize. I have here the following code that will make a borderless form draggable.

     

    ‘Declare these 2 variables with class scope
    Private Const WM_NCLBUTTONDOWN As Integer = &HA1S
    Private Const HTCAPTION As Integer = 2

    ‘Then handle your form.MouseDown event
    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
            Me.Capture = False
            Dim msg As Message = Message.Create(Me.Handle, WM_NCLBUTTONDOWN, New IntPtr(HTCAPTION), IntPtr.Zero)
            Me.DefWndProc(msg)
        End Sub

  • How to send email using SMTP

    Posted on May 7th, 2009 shyguy 2 comments

    Sending email using vb.net 2.0 framework is quite different from 1.1. The 1.1 framework is using a System.Web.Mail while 2.0 uses System.Net.Mail and it makes really different. There are some codes that are obsolete in 2.0 framework already that exist in 1.1 framework.  In my case, since I am using vb.net 2.0 framework then I will share my code here  that i used in my program that will send emails.  I hope this will help to the people who are seeking codes that will send an email using vb.net 2.0 framework. As a default I am using gmail for my demo but it can be used to any email server as long as you know the configuration.

     

    Set iMsg = CreateObject(”CDO.Message”)
    Set iConf = CreateObject(”CDO.Configuration”)
    Set Flds = iConf.Fields

    schema = “http://schemas.microsoft.com/cdo/configuration/”
    Flds.Item(schema & “sendusing”) = 2
    Flds.Item(schema & “smtpserver”) = “smtp.gmail.com”
    Flds.Item(schema & “smtpserverport”) = 465
    Flds.Item(schema & “smtpauthenticate”) = 1
    Flds.Item(schema & “sendusername”) = “myemail@gmail.com”
    Flds.Item(schema & “sendpassword”) =  “mypassword”
    Flds.Item(schema & “smtpusessl”) = 1
    Flds.Update

    With iMsg
    .To = “admin@uiccs07.net”
    .From = “admin@uiccs07.net>”
    .Sender = “uiccs07.net”
    .ReplyTo = “admin@uiccs07.net”
    .Subject = “This is a subject”
    .HTMLBody = “This is sample html email.”
    .Organization = “uiccs07.net”
    Set .Configuration = iConf
    SendEmailGmail = .Send
    End With

    set iMsg = nothing
    set iConf = nothing
    set Flds = nothing

  • Welcome

    Posted on May 2nd, 2009 shyguy 1 comment

    Welcome everyone. I hope you enjoy upon visiting this site. Please leave a comment or email the administrator if there is any question regarding to this site. Have a nice day and God Bless!