Solved

Windows form combobox not showing selected value

Posted on 2014-04-18
13
443 Views
Last Modified: 2014-06-02
I want to first say that I am new to Windows Application development.  I only have experience with web app development and am really struggling with windows development.  It baffles me that windows app doesn't have a formview control like web.

I have two tables.  The Requests table has a field called status; which maps to the ID field in my Status lookup table. My status table has ID and Status fields.

I am trying to populate the options on my combobox with the records in my Status table and also assign the selectedvalue of a combobox  to the status field in my Requests table.  

Under combobox tasks, I have 'Use Databound Items' checked,  

My datasource is my Status table, ID is the value and Status is the display
Under Seletedvalue, I have my status field selected from my RequestsBindingSource.

When I run the application the combbox shows all the records from the Status table but does not have the status field from the Requests table as the selectedvalue.

What am I doing wrong?
0
Comment
Question by:imstac73
  • 9
  • 4
13 Comments
 

Author Comment

by:imstac73
ID: 40008939
BTW, if I changed the Status field in my Data Source tab to a textbox and drag it to the form the correct value is displayed.
0
 

Author Comment

by:imstac73
ID: 40013397
I watched a video on how to use the combobox and followed the directions as specified and I still get the same result.  The combobox will not show the current value of the field.  I can't figure it out.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 40015014
Can you show some code that populates the combo and loads the record?
0
 

Author Comment

by:imstac73
ID: 40015473
I'm not doing any special backend code on this.  Just filling out the options on the combobox dropdown.

I have the data source set to my status table source.  
Value = ID
Display = Status

I have my selected value set to my requests table source. Status field.  I have a relationship set up in my databset that links the reqests table status field to the ID field in the status table.

It should be so simple; I don't understand why it's not working.
0
 

Author Comment

by:imstac73
ID: 40041935
Here is my designer code  

 Me.StatusComboBox.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue", Me.RequestsBindingSource1, "Status", True))
        Me.StatusComboBox.DataSource = Me.ActiveStatusBindingSource
        Me.StatusComboBox.ValueMember = "ID"
        Me.StatusComboBox.DisplayMember = "Status"
        Me.StatusComboBox.Name = "StatusComboBox"
        Me.StatusComboBox.FormattingEnabled = True
        Me.StatusComboBox.Location = New System.Drawing.Point(628, 52)
        Me.StatusComboBox.Size = New System.Drawing.Size(169, 21)
        Me.StatusComboBox.TabIndex = 42

Open in new window

0
 

Author Comment

by:imstac73
ID: 40068401
I have worked on this for several days straight with nothing working.  My newest attempt was to not use the built in tool on the combobox to assign the combobox items and selected value and do it manually in my vb code.

This is what I tried and now I don't even get the combobox items populated.  So frustrated with Windows forms.

Dim con As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection("T_REQS.My.MySettings.TREQSConnectionString")
        Dim qrystr As String = "Select ID, Status from Status where Active = 1"
        Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
        dbCommand.CommandText = qrystr
        dbCommand.Connection = con
        Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
        dataAdapter.SelectCommand = dbCommand
        Dim dataSet As System.Data.DataSet = New System.Data.DataSet
        dataAdapter.Fill(dataSet)

        Me.StatusComboBox.DataSource = dataSet
        Me.StatusComboBox.DisplayMember = "Status"
        Me.StatusComboBox.ValueMember = "ID"

        Dim con2 As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection("T_REQS.My.MySettings.TREQSConnectionString")
        Dim qrystr2 As String = "Select Status from Requests where ReqID = '" & reqid & "'"
        Dim dbCommand2 As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
        dbCommand.CommandText = qrystr
        dbCommand.Connection = con
        Dim dataAdapter2 As System.Data.IDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
        dataAdapter.SelectCommand = dbCommand
        Dim dataSet2 As System.Data.DataSet = New System.Data.DataSet
        dataAdapter.Fill(dataSet2)

        Me.StatusComboBox.DataBindings.Add("SelectedValue", dataSet2, "Status")

Open in new window

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:imstac73
ID: 40068404
I put this code in my form load event
Dim con As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection("T_REQS.My.MySettings.TREQSConnectionString")
        Dim qrystr As String = "Select ID, Status from Status where Active = 1"
        Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
        dbCommand.CommandText = qrystr
        dbCommand.Connection = con
        Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
        dataAdapter.SelectCommand = dbCommand
        Dim dataSet As System.Data.DataSet = New System.Data.DataSet
        dataAdapter.Fill(dataSet)

        Me.StatusComboBox.DataSource = dataSet
        Me.StatusComboBox.DisplayMember = "Status"
        Me.StatusComboBox.ValueMember = "ID"

        Dim con2 As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection("T_REQS.My.MySettings.TREQSConnectionString")
        Dim qrystr2 As String = "Select Status from Requests where ReqID = '" & reqid & "'"
        Dim dbCommand2 As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
        dbCommand.CommandText = qrystr
        dbCommand.Connection = con
        Dim dataAdapter2 As System.Data.IDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
        dataAdapter.SelectCommand = dbCommand
        Dim dataSet2 As System.Data.DataSet = New System.Data.DataSet
        dataAdapter.Fill(dataSet2)

        Me.StatusComboBox.DataBindings.Add("SelectedValue", dataSet2, "Status")

Open in new window

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 40068536
Change the above code to following which should get your combobox populated and then we will move on to selecting value.

        Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter( "Select ID, Status from Status where Active = 1", T_REQS.My.MySettings.TREQSConnectionString)
        Dim dTable As New System.Data.DataTable
        dataAdapter.Fill(dTable)

        Me.StatusComboBox.DataSource = dTable
        Me.StatusComboBox.DisplayMember = "Status"
        Me.StatusComboBox.ValueMember = "ID"

Open in new window

0
 

Author Comment

by:imstac73
ID: 40070070
I get an error 'Value of type System.Data.DataTable cannot be converted to System.Data.DataSet'

Here is my new code:  Also, I have this code in the form load event; is this where i need it or do I need to move it to a different event?

Public Sub Details2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'TREQSDataSet.ActiveStatus' table. You can move, or remove it, as needed.
        Me.ActiveStatusTableAdapter.Fill(Me.TREQSDataSet.ActiveStatus)

      
        Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter("Select ID, Status from Status where Active = 1", "T_REQS.My.MySettings.TREQSConnectionString")
        Dim dTable As New System.Data.DataTable
        dataAdapter.Fill(dTable)

        Me.StatusDescComboBox.DataSource = dTable
        Me.StatusDescComboBox.DisplayMember = "Status"
        Me.StatusDescComboBox.ValueMember = "ID"

    End Sub

Open in new window

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 40074663
Comment out line 4 and change line 7 to following

Dim dataAdapter As New System.Data.OleDb.OleDbDataAdapter("Select ID, Status from Status where Active = 1", T_REQS.My.MySettings.TREQSConnectionString)

Just out of curiosity, why do you insist on putting the connection string in quotes? Its a reference to a class so it would not get evaluated if you put it in quotes. It will be taken as literally instead.
0
 

Accepted Solution

by:
imstac73 earned 0 total points
ID: 40074913
Hmm..I haven't had any issues with using quotes.  Good tip though, I will starting doing it without.

So I finally was able to get the combobox working.  I had to start from scratch with my form.  

My tableadapter query has an input parameter. Previously I was passing the parameter value from my main page directly into the details form query.  This worked fine for all controls except the combobox.  I changed it to passing the parameter value and then assigning a textbox value to the parameter value.  Then using the textbox value as my input parameter value when calling my query.

I have no idea why the combobox wouldn't work with directly passing the parameter value into the query.  Windows really seems to be quirky.

Thanks for all your help in helping me troubleshoot.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 40075155
Glad its working.
0
 

Author Closing Comment

by:imstac73
ID: 40105106
This was the only thing that worked.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
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…

746 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

12 Experts available now in Live!

Get 1:1 Help Now