?
Solved

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

Posted on 2012-03-19
15
Medium Priority
?
588 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

777 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