Solved

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

Posted on 2012-03-19
15
584 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 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