Monday 27 February 2012

Fill Combobox using dataset in VB.net


                         If You have a combobox1 in a form (“Form 1”) and a database in SQL server  with a table,  named “tblCountry”  having  fields “countryID”, ”countryName” with data then  you can strat from here..... :)
Because, we fill combobox with Country with its id,


For database connection using dsn

We are using odbc connection , So we must import odbc class file first into the form(“Form 1”),

Imports System.Data.Odbc

Create a sub function in your form

Public Sub FillMaComboBox()

        Dim sqlQury As String
        sqlQury = "select countryID, countryName from tblCountry" 

        Dim ds As New DataSet
        Dim odbda As New OdbcDataAdapter(sqlQury, conDb) ‘conDb is connection object
        odbda.Fill(ds) ‘Fill selected data to ds(our dataset) using OdbcDataAdapter

        ComboBox1.DataSource = ds.Tables(0).DefaultView
        ComboBox1.DisplayMember = “countryName” ‘We want to display country
        ComboBox1.ValueMember = “countryID” ‘We store id as valueMember of ComboBox

End Sub



Now you did it bro.,
You can use this “ FillMaComboBox() into any event handler that you want….:)




Thursday 16 February 2012

Thread in VB.net

This is a simple App for Demonstrate threading in VB.net
   Add
          1) ListBox 1,ListBox 2
          2)Button1(Start Thread)
                                       To our Form (Form1)
Now we are ready for coding ..... :)


  • Import "System.threading" to our thread App
        Import System.Threading
  • Create  Sample functions in app called "FillList1" and "FillList2"
        Private Sub FillList1()
       for i as integer=0 to 25
           ListBox1.Items.Add(i)
           Threading.Thread.Sleep(1000) 'To visible working of threads
       next i
     End Sub
     Private Sub FillList2()
       for j as integer=0 to 25
           ListBox2.Items.Add(j)
           Threading.Thread.Sleep(1000)
       next i
     End Sub
  • Now we call our sample threads from our start Button (Button1)
     Private Sub Button1_Click(ByVal sender As System.Object,_
                        ByVal e As System.EventArgs)Handles Button1.Click

        Dim thrd1 As Threading.Thread   'Declare thread1 named as thrd1

        Dim thrd2 As Threading.Thread   'Declare thread2 named as thrd2

        Control.CheckForIllegalCrossThreadCalls=False 'Avoid Illegal thread call

        thrd1=New Threading.Thread(AddressOf FillList1)  

        thrd2=New Threading.Thread(AddressOf FillList2)

        thrd1.Start()

        thrd2.Start()
End Sub

Now run your App.......:D