Solved

Combobox Help in VB.NET

Posted on 2008-10-27
3
414 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create XML 5 47
Datagridview control last row back color red vb.net 4 38
Close word object 13 38
Receiving a string from a WebService Push 21 38
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

26 Experts available now in Live!

Get 1:1 Help Now