Solved

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

Posted on 2012-03-19
15
585 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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
 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 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