?
Solved

Combobox Help in VB.NET

Posted on 2008-10-27
3
Medium Priority
?
419 Views
Last Modified: 2012-08-14
I am using VB.NET VS 2005 ..the problem is this...

I have a Combobox that is coded up this way on the Form Load event...

  strSql = "SELECT lID,sDepotDesc From tDepot"
        da = New SqlDataAdapter(strSql, con)
        da.Fill(ds, "tDepot")

        Dim dt As New DataTable
        dt.Columns.Add("lID", GetType(System.String))
        dt.Columns.Add("sDepotDesc", GetType(System.String))

        Dim drNewRow As DataRow

        For Each drDSRow In ds.Tables("tDepot").Rows()
            drNewRow = dt.NewRow()
            drNewRow("lID") = drDSRow("lID")
            drNewRow("sDepotDesc") = drDSRow("sDepotDesc")
            dt.Rows.Add(drNewRow)
        Next

        cmbo_depot1.DropDownStyle = ComboBoxStyle.DropDownList

        With cmbo_depot1
            .DataSource = dt
            .DisplayMember = "sDepotDesc"
            .ValueMember = "lID"
            .SelectedIndex = 0
        End With

I have a variable that holds the ID of the Depot when the ComboBox is changed ( which will be used to insert as part of a row in a table)...the problem is if you don't change the comboBox ...I get an error on the Insert statement because the Index hasn't changed
"The name "System.Data.DataRowView" is not permitted in this context"


this is the code that is supposed to capture the ID of the selection in the ComboBox...

   Private Sub cmbo_depot1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbo_depot1.SelectedIndexChanged
                lnPrivDepotID = cmbo_depot1.SelectedValue.ToString

    End Sub


Basically I need words displayed in the comboBox to force the user to change the combobox to pick a selection, so the index is changed  .... I used the .SelectedText to do this but I still don't see the words as the firstSelction in the ComboBox..

I don't need to do it this way....I need to be able to grab the first selection in the combobox without actual changing the combobox...the user sees the first selection and sometimes doesn't change the selection and thus it throws an error..

Any help or insight would be much appreciated...

0
Comment
Question by:nomar2
[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
  • 2
3 Comments
 
LVL 48

Accepted Solution

by:
jpaulino earned 208 total points
ID: 22815871

Two corrections:

1 - You should change the order of the bind to this:
      With cmbo_depot1
            .DisplayMember = "sDepotDesc"
            .ValueMember = "lID"
            .DataSource = dt
            .SelectedIndex = 0
        End With
 
2 - You should check if the index is valid in the SelectedIndexChanged event
Private Sub cmbo_depot1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbo_depot1.SelectedIndexChanged
         If cmbo_depot1.SelectedIndex <> -1 Then
                lnPrivDepotID = cmbo_depot1.SelectedValue.ToString
        End If

    End Sub

 
0
 

Author Comment

by:nomar2
ID: 22823769
100% PERFECT!!!!!!!!!!!!!!!!!!!!!!!!
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22824171
Glad I could help!
jpaulino
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 21 hours left to enroll

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