Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2012-03-19
15
Medium Priority
?
590 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

604 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