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

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




Selected index problem

 
Reply to this topicStart new topic

Selected index problem

Damage
28 Aug, 2008 - 04:10 PM
Post #1

D.I.C Addict
Group Icon

Joined: 5 Jun, 2008
Posts: 754



Thanked: 7 times
Dream Kudos: 75
My Contributions
CODE

Private Sub frmModifyUser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Tables\Employees.accdb")
        conn.Open()
        Dim myadapter As New OleDbDataAdapter("SELECT * FROM tblEmployee", conn)
        Dim dtset As New DataSet
        myadapter.Fill(dtset)
        myadapter.Dispose()

        Dim table As DataTable = dtset.Tables(0)

        mcboEmployee.DataSource = table
        mcboEmployee.DisplayMember = "EmpID"



        conn.Close()

    End Sub

    Private Sub mcboEmployee_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mcboEmployee.SelectedIndexChanged
        Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Tables\Employees.accdb")
        conn.Open()
        Dim myadapter As New OleDbDataAdapter("SELECT * FROM tblEmployee where EmpID =" + mcboEmployee.Text, conn)
        Dim dtset As New DataSet
        myadapter.Fill(dtset)
        myadapter.Dispose()

        Dim table As DataTable = dtset.Tables(0)

        txtUserID.Text = table.Rows(0).Item(0)


        conn.Close()

    End Sub


the code works i just thought i'd add it to help illustrate my problem. The selectedIndexChanged event is firing on form load and I think it fires before the combobox has loaded properly so I get "No value given for one or more required parameters.". How do i get round this?
User is offlineProfile CardPM
+Quote Post

gbertoli3
RE: Selected Index Problem
28 Aug, 2008 - 04:25 PM
Post #2

DIC at Heart + Code
Group Icon

Joined: 23 Jun, 2008
Posts: 1,063



Thanked: 17 times
Dream Kudos: 950
My Contributions
Try adding a Timer to have the form wait before processing the items.
User is offlineProfile CardPM
+Quote Post

DeCompile
RE: Selected Index Problem
28 Aug, 2008 - 05:43 PM
Post #3

D.I.C Head
**

Joined: 20 Jul, 2008
Posts: 188



Thanked: 6 times
My Contributions
Doesn't the selectindexchanged event only fire when the index has been changed? ie. mcbo_Employee
User is offlineProfile CardPM
+Quote Post

Damage
RE: Selected Index Problem
28 Aug, 2008 - 07:37 PM
Post #4

D.I.C Addict
Group Icon

Joined: 5 Jun, 2008
Posts: 754



Thanked: 7 times
Dream Kudos: 75
My Contributions
yeah thats what i thought but it's the errors occuring as the form loads, using a timer should work (thanks gbertoli3) but is there something I'm not seeing here? Like is this a standard problem that people run into or am I doing something wrong?

This post has been edited by Damage: 28 Aug, 2008 - 07:37 PM
User is offlineProfile CardPM
+Quote Post

djkitt
RE: Selected Index Problem
28 Aug, 2008 - 08:49 PM
Post #5

D.I.C Head
**

Joined: 22 May, 2008
Posts: 134



Thanked: 14 times
My Contributions

Just check to make sure something is selected before you use it.
CODE

if(!String.IsNullOrEmpty(mcboEmployee.Text))



User is offlineProfile CardPM
+Quote Post

WayneSpangler
RE: Selected Index Problem
29 Aug, 2008 - 04:35 AM
Post #6

D.I.C Head
**

Joined: 22 Mar, 2008
Posts: 104



Thanked: 10 times
My Contributions
Try adding Me.Show at top of load code.
CODE
Private Sub frmModifyUser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Show()
        Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Tables\Employees.accdb")
        conn.Open()

This will load and show all controls before you use them.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Selected Index Problem
29 Aug, 2008 - 06:09 AM
Post #7

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,327



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
As your form begins loading, there are no values by default in the combobox. So what happens is as it begins populating the combobox with values this causes the SelectedIndexChanged event to fire. This is expected behavior.

Create a class level boolean variable that will keep track of the form load process. Then check the boolean inside the SelectedIndexChanged event, if it is true the form is still loading and don't run the code in the event. If it is false the loading has completed and you want the code to fire.

Example:
CODE

Dim loading As Boolean = True

Private Sub frmModifyUser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Tables\Employees.accdb")
        conn.Open()
        Dim myadapter As New OleDbDataAdapter("SELECT * FROM tblEmployee", conn)
        Dim dtset As New DataSet
        myadapter.Fill(dtset)
        myadapter.Dispose()

        Dim table As DataTable = dtset.Tables(0)

        mcboEmployee.DataSource = table
        mcboEmployee.DisplayMember = "EmpID"

        conn.Close()

        'the load event has finished, set the boolean to False
        loading = False

    End Sub

    Private Sub mcboEmployee_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mcboEmployee.SelectedIndexChanged

    'Do not run the code unless the boolean is False
    If Not loading Then
        Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Tables\Employees.accdb")
        conn.Open()
        Dim myadapter As New OleDbDataAdapter("SELECT * FROM tblEmployee where EmpID =" + mcboEmployee.Text, conn)
        Dim dtset As New DataSet
        myadapter.Fill(dtset)
        myadapter.Dispose()

        Dim table As DataTable = dtset.Tables(0)

        txtUserID.Text = table.Rows(0).Item(0)

        conn.Close()
      End If
    End Sub


Hope that helps.
User is offlineProfile CardPM
+Quote Post

Damage
RE: Selected Index Problem
29 Aug, 2008 - 02:21 PM
Post #8

D.I.C Addict
Group Icon

Joined: 5 Jun, 2008
Posts: 754



Thanked: 7 times
Dream Kudos: 75
My Contributions
man this site is great. Thanks for the help everyone
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:07PM

Be Social

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

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month