Solved

Combobox Help in VB.NET

Posted on 2008-10-27
3
406 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
  • 2
3 Comments
 
LVL 48

Accepted Solution

by:
jpaulino earned 52 total points
Comment Utility

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
Comment Utility
100% PERFECT!!!!!!!!!!!!!!!!!!!!!!!!
0
 
LVL 48

Expert Comment

by:jpaulino
Comment Utility
Glad I could help!
jpaulino
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now