VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 306,813 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,686 people online right now. Registration is fast and FREE... Join Now!




selected index changed

 

selected index changed, populating textbox from selected index changed

melodimoeti

5 Jun, 2009 - 03:50 AM
Post #1

New D.I.C Head
*

Joined: 20 Apr, 2009
Posts: 39

hi,
i have a combobox which when selected it has to populate other text boxes from the ms access database. but instead of it populating it gives the error mesage:An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll
on line : Dim objReader As OleDbDataReader = cmd.ExecuteReaderplease assist!

CODE
Private Sub devstaffid_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles devstaffid.SelectedIndexChanged
        strSQL = "SELECT * FROM employees WHERE Persal_Number = " & devstaffid.Text & " "
        oledbcon.Open()
        Dim cmd As New OleDbCommand(strSQL, oledbcon)

        Dim objReader As OleDbDataReader = cmd.ExecuteReader
        objReader.Read()
        TextBox2.Text = objReader("Component_Description")
        TextBox1.Text = objReader("Surname" & "Initials")
        objReader.Close()
        oledbcon.Close()

    End Sub


User is offlineProfile CardPM
+Quote Post


woodjom

RE: Selected Index Changed

5 Jun, 2009 - 04:44 AM
Post #2

D.I.C Regular
Group Icon

Joined: 8 May, 2008
Posts: 366



Thanked: 15 times
My Contributions
vb.net

strSQL = "SELECT * FROM employees WHERE Persal_Number = " & devstaffid.Text & " "


try doing this
CODE

strSQL = "SELECT * FROM employees WHERE Persal_Number = " & cint(devstaffid.Text) & " "


Thats the first bug i saw. when concatenating query statements, you will need to explicitely change the variable location to the data type of the field.

Other than that, it all looks good.
User is offlineProfile CardPM
+Quote Post

melodimoeti

RE: Selected Index Changed

5 Jun, 2009 - 06:25 AM
Post #3

New D.I.C Head
*

Joined: 20 Apr, 2009
Posts: 39

QUOTE(woodjom @ 5 Jun, 2009 - 01:44 PM) *

vb.net

strSQL = "SELECT * FROM employees WHERE Persal_Number = " & devstaffid.Text & " "


try doing this
CODE

strSQL = "SELECT * FROM employees WHERE Persal_Number = " & cint(devstaffid.Text) & " "


Thats the first bug i saw. when concatenating query statements, you will need to explicitely change the variable location to the data type of the field.

Other than that, it all looks good.

hi, it still gives the same error
User is offlineProfile CardPM
+Quote Post

noorahmad

RE: Selected Index Changed

5 Jun, 2009 - 09:32 PM
Post #4

Webmaster
Group Icon

Joined: 12 Mar, 2009
Posts: 2,086



Thanked: 142 times
Dream Kudos: 1350
My Contributions
type your code in SelectionChangeCommitted event of combo box

CODE
   Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted
#your code here
    End Sub

User is online!Profile CardPM
+Quote Post

sonia.sardana

RE: Selected Index Changed

6 Jun, 2009 - 12:18 AM
Post #5

D.I.C Head
**

Joined: 1 Jun, 2008
Posts: 132



Thanked: 5 times
My Contributions
Ur probs is solved or not?If not,so dat i can answer it?Reply!
User is offlineProfile CardPM
+Quote Post

melodimoeti

RE: Selected Index Changed

6 Jun, 2009 - 01:27 AM
Post #6

New D.I.C Head
*

Joined: 20 Apr, 2009
Posts: 39

QUOTE(noorahmad @ 6 Jun, 2009 - 06:32 AM) *

type your code in SelectionChangeCommitted event of combo box

CODE
   Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted
#your code here
    End Sub


hi, the code is still giving the same error message after the above changes.

QUOTE(sonia.sardana @ 6 Jun, 2009 - 09:18 AM) *

Ur probs is solved or not?If not,so dat i can answer it?Reply!

no, sorry but my problem is not solved i still get the same error message.
User is offlineProfile CardPM
+Quote Post

noorahmad

RE: Selected Index Changed

6 Jun, 2009 - 01:30 AM
Post #7

Webmaster
Group Icon

Joined: 12 Mar, 2009
Posts: 2,086



Thanked: 142 times
Dream Kudos: 1350
My Contributions
change your query
CODE
strSQL = "SELECT * FROM employees WHERE Persal_Number = " & devstaffid.SelectedValue & " "

User is online!Profile CardPM
+Quote Post

melodimoeti

RE: Selected Index Changed

6 Jun, 2009 - 01:44 AM
Post #8

New D.I.C Head
*

Joined: 20 Apr, 2009
Posts: 39

QUOTE(noorahmad @ 6 Jun, 2009 - 10:30 AM) *

change your query
CODE
strSQL = "SELECT * FROM employees WHERE Persal_Number = " & devstaffid.SelectedValue & " "


its changed but when i run it, it still gives error on objreader, error line =
CODE
Dim objReader As OleDbDataReader = cmd.ExecuteReader

User is offlineProfile CardPM
+Quote Post

noorahmad

RE: Selected Index Changed

6 Jun, 2009 - 01:48 AM
Post #9

Webmaster
Group Icon

Joined: 12 Mar, 2009
Posts: 2,086



Thanked: 142 times
Dream Kudos: 1350
My Contributions
ok here is my code
CODE
    Dim con As OleDbConnection
    Dim da As OleDbDataAdapter
    Dim dt As DataTable
    Dim strQuery As String
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        strQuery = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\b1.mdb;Persist Security Info=True"
        con = New OleDbConnection(strQuery)
        con.Open()
        Me.Text = con.State.ToString
        strQuery = "Select * from Table1"
        da = New OleDbDataAdapter(strQuery, con)
        dt = New DataTable
        da.Fill(dt)
        With ComboBox1
            .DataSource = dt
            .DisplayMember = "Name"
            .ValueMember = "ID"
        End With
    End Sub

    Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted
        strQuery = "Select * from Table1 Where ID=" & ComboBox1.SelectedValue
        da = New OleDbDataAdapter(strQuery, con)
        dt = New DataTable
        da.Fill(dt)
        TextBox1.Text = dt(0)(0)
        TextBox2.Text = dt(0)(1)
    End Sub

User is online!Profile CardPM
+Quote Post

melodimoeti

RE: Selected Index Changed

6 Jun, 2009 - 02:28 AM
Post #10

New D.I.C Head
*

Joined: 20 Apr, 2009
Posts: 39

QUOTE(noorahmad @ 6 Jun, 2009 - 10:48 AM) *

ok here is my code
CODE
    Dim con As OleDbConnection
    Dim da As OleDbDataAdapter
    Dim dt As DataTable
    Dim strQuery As String
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        strQuery = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\b1.mdb;Persist Security Info=True"
        con = New OleDbConnection(strQuery)
        con.Open()
        Me.Text = con.State.ToString
        strQuery = "Select * from Table1"
        da = New OleDbDataAdapter(strQuery, con)
        dt = New DataTable
        da.Fill(dt)
        With ComboBox1
            .DataSource = dt
            .DisplayMember = "Name"
            .ValueMember = "ID"
        End With
    End Sub

    Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted
        strQuery = "Select * from Table1 Where ID=" & ComboBox1.SelectedValue
        da = New OleDbDataAdapter(strQuery, con)
        dt = New DataTable
        da.Fill(dt)
        TextBox1.Text = dt(0)(0)
        TextBox2.Text = dt(0)(1)
    End Sub


hi, i took ur code and put it into my system and changed fields to suit mine, but before i run it, its complaining about dt saying "class 'system.data.datatable' cannot be indexed because it has no default property"
User is offlineProfile CardPM
+Quote Post

noorahmad

RE: Selected Index Changed

6 Jun, 2009 - 02:45 AM
Post #11

Webmaster
Group Icon

Joined: 12 Mar, 2009
Posts: 2,086



Thanked: 142 times
Dream Kudos: 1350
My Contributions
i attached the database and Vb.Net Project download it.


Attached File(s)
Attached File  Access.zip ( 72.67k ) Number of downloads: 20
User is online!Profile CardPM
+Quote Post

melodimoeti

RE: Selected Index Changed

6 Jun, 2009 - 03:54 AM
Post #12

New D.I.C Head
*

Joined: 20 Apr, 2009
Posts: 39

QUOTE(noorahmad @ 6 Jun, 2009 - 11:45 AM) *

i attached the database and Vb.Net Project download it.

thanks, but for some reason i can download the zip file...gives me an error mesage, can u maybe use winrar and resend.
User is offlineProfile CardPM
+Quote Post

melodimoeti

RE: Selected Index Changed

7 Jun, 2009 - 10:36 AM
Post #13

New D.I.C Head
*

Joined: 20 Apr, 2009
Posts: 39

QUOTE(melodimoeti @ 6 Jun, 2009 - 12:54 PM) *

QUOTE(noorahmad @ 6 Jun, 2009 - 11:45 AM) *

i attached the database and Vb.Net Project download it.

thanks, but for some reason i can download the zip file...gives me an error mesage, can u maybe use winrar and resend.

thanks, its working
User is offlineProfile CardPM
+Quote Post

woodjom

RE: Selected Index Changed

23 Jun, 2009 - 07:40 PM
Post #14

D.I.C Regular
Group Icon

Joined: 8 May, 2008
Posts: 366



Thanked: 15 times
My Contributions
vb
	
Dim con As OleDbConnection
Dim da As OleDbDataAdapter
Dim dt As DataTable
Dim strQuery As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
strQuery = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\b1.mdb;Persist Security Info=True"
con = New OleDbConnection(strQuery)
con.Open()
Me.Text = con.State.ToString
strQuery = "Select * from Table1"
da = New OleDbDataAdapter(strQuery, con)
dt = New DataTable
da.Fill(dt)
With ComboBox1
.DataSource = dt
.DisplayMember = "Name"
.ValueMember = "ID"
End With
con.close() <==== Add this
End Sub

Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted
conn.open() <==== Add this
strQuery = "Select * from Table1 Where ID=" & ComboBox1.SelectedValue
da = New OleDbDataAdapter(strQuery, con)
dt = New DataTable
da.Fill(dt)
TextBox1.Text = dt(0)(0)
TextBox2.Text = dt(0)(1)
conn.close() <===== Add this
End Sub


This post has been edited by woodjom: 23 Jun, 2009 - 07:43 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/20/09 10:03PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month