Solved

combobox and assigning value from database then updating from a result set - error

Posted on 2012-03-19
15
581 Views
Last Modified: 2012-03-25
I have a combobox that I bind to a data table. I then query Active Directory to gety the value to assign to the combo box and I keep getting "Index was out of range Must be no negative and less than the siz of the collection"
Plus it assigns the value and Please Select
for example "United StatesPlease Select"

here is what I am doing
cbUserRegion.SelectedText = ((result.Properties("co")(0)))

Open in new window


and
        If (indexOfSelectedTab = 1) Then
            Try
                Dim connString As String = "server=MYSERVER;database=MINE; user=USER; password=PASS;trusted_connection=yes"
                Dim conn As New SqlConnection(connString)
                conn.Open()
                Dim strSQL As String = "SELECT Country,Value FROM Country"
                Dim da As New SqlDataAdapter(strSQL, conn)
                Dim ds As New DataSet
                da.Fill(ds, "Country")

                With cbUserRegion
                    .DataSource = ds.Tables("Country")
                    .DisplayMember = "Country"
                    .ValueMember = "value"

                End With
                conn.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If

Open in new window


Any help would be appreciated
0
Comment
Question by:r3nder
  • 8
  • 6
15 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 37740761
are you sure
result.Properties("co") 

Open in new window

returns at least one value ?
0
 
LVL 6

Author Comment

by:r3nder
ID: 37740773
yes the result saet is from Active Directory

       Try
            Dim mFilter As String = "(sAMAccountName=" & Me.txtUserUserName.Text & ")"
            Dim dirEntry As DirectoryEntry = New DirectoryEntry("LDAP://myAD", "Server","password")
            Dim filter As String = mFilter
            Dim findUser As DirectorySearcher = New DirectorySearcher(dirEntry, filter)
            Dim results As SearchResultCollection = findUser.FindAll
            For Each result As SearchResult In results
                For Each prop As DictionaryEntry In result.Properties
                    For Each individualValue As Object In prop.Value
                        txtUserFirstName.Text = ((result.Properties("givenName")(0)))
                        txtUserLastName.Text = ((result.Properties("sn")(0)))
                        txtUserDisplayName.Text = ((result.Properties("displayName")(0)))
                        txtUserInitial.Text = ((result.Properties("initials")(0)))
                        txtUserOffice.Text = ((result.Properties("physicalDeliveryOfficeName")(0)))
                        txtUserEmail.Text = ((result.Properties("mail")(0)))
                        txtUserStreet.Text = ((result.Properties("streetAddress")(0)))
                        txtUserPOBox.Text = ((result.Properties("postOfficeBox")(0)))
                        txtUserCity.Text = ((result.Properties("l")(0)))
                        'Dim strItem As String = ((result.Properties("co")(0)))
                        'Me.cbUserRegion.Text = Me.cbUserRegion.ToString(strItem)
                        'strItem = cbUserRegion.Text
                        cbUserRegion.SelectedText = ((result.Properties("co")(0)))
                        txtUserZip.Text = ((result.Properties("postalCode")(0)))
                        txtUserState.Text = ((result.Properties("st")(0)))
                        txtUserTelephoneNumber.Text = ((result.Properties("telephoneNumber")(0)))
                        txtUserTitle.Text = ((result.Properties("title")(0)))
                        txtUserPhoneExt.Text = ((result.Properties("phone")(0)))
                    Next
                Next
            Next

Open in new window

I tried assigning it to a variable but to no availe
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 37740779
which line is causing the problem now ?
0
 
LVL 6

Author Comment

by:r3nder
ID: 37740783
22 I believe
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 37740799
can u try setting the combo box selected index instead and see what happens, what is the combo bound to ? does it have values in it ?
0
 
LVL 6

Author Comment

by:r3nder
ID: 37740813
the combobox is bound to a dataset (at the top in second code snippet)

Here is the error messege
ex.Message      "Conversion from string "United States" to type 'Integer' is not valid."      String
0
 
LVL 6

Author Comment

by:r3nder
ID: 37740814
that was the message after changing it to .SelectedIndex
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 37740848
sorry i was not clear, what i meant was set selected index like cbUserRegion.SelectedIndex = 1 to make sure the combo is bound, then instead of setting the selected text, set the selected index by finding the index of "result.Properties("co")(0)" in the data set
0
 
LVL 6

Author Comment

by:r3nder
ID: 37740870
ok - I think I found the problem - I took out the combobox so all I have is the SQL filling the combobox - it loads fine when the tab is selected - but when I look up the user thats when it gives me the error - I am leaning toward a postback? - how do you do a if not post back even though a with is in the function
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 37740974
you may need to cache the results in a session and then reassign it from there.
0
 
LVL 6

Author Comment

by:r3nder
ID: 37740983
I dont know how to do that
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 37741013
Session["user"] = ur source from db

and when post back is done set it from this session
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37741453
You have been using combobox word but not mentioned postback. Is it winforms or asp.net?
0
 
LVL 6

Accepted Solution

by:
r3nder earned 0 total points
ID: 37742793
Its a win form and I found it
The problem was
txtUserPhoneExt.Text = ((result.Properties("phone")(0)))
no value such as "phone" exists in the result set. Sorry for the mis direction
0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 37762381
Even though I found the problem please assign 500 points to gauthampj for helping - if I did not have his imput - I would never have seen it
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now