?
Solved

VB.Net Clicking on an item in combobox and the related info is displayed in another form

Posted on 2005-05-06
16
Medium Priority
?
240 Views
Last Modified: 2012-05-05
I have a ComboBox on a form in VB.net...I have the comboBox being populated from a field in a table in the database.

What I am trying to do is I click on an item from the drop-down list and I want a form to pop-up with the info related to the item clicked on from the box.

Right now I have this code on the loading of the form that holds the combo box...

 Dim dsn As String = "User ID=sa;password=ma;Initial Catalog=sa;Data Source=CYCLE"
        Dim myConn As SqlClient.SqlConnection = New SqlClient.SqlConnection(dsn)

        Dim myComm As SqlDataAdapter = New SqlDataAdapter("Select distinct order_number from   rec_orders", myConn)
        Dim ds As DataSet = New DataSet
        myComm.Fill(ds, "Rec_Orders")

        Dim dv As DataView = ds.Tables("Rec_Orders").DefaultView
        ComboBox1.DataSource = dv
        ComboBox1.DisplayMember = "order_number"


So if I click on the order number from the combo box...I want that to return all the related info from another table ( tbl_Order) for that order_number on another form that pops - up..(.I am thinking that I will need  to pass in the ID for that order number) .

The info of the form that pops - up needs to be just displayed not edited.

I am a bit in the dark on how to exactly do this codewise...any insight or help would be much appreciated.

0
Comment
Question by:nomar2
  • 8
  • 8
16 Comments
 
LVL 22

Expert Comment

by:rspahitz
ID: 13946823
I would start by creating a query/view in the database that returns exactly what you want.

Then you can use the Dataform wizard to build a form for that data.

Finally, using the ID from the dropdown list, you could open the form, pass the ID and have it show just that record.  Make the entire form disabled except for a close button.
0
 

Author Comment

by:nomar2
ID: 13946918
How do I access the dataform wizard...stupid question...but I am just starting learning Vb.net
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 13947016
oh...after starting a new project, go to the menu and select project | new windows form.  One of the options is the Data Form Wizard.

If you're using Access for your DB, you'll probably want to put one copy in the project's folder and the other in the project's bin folder (for testing)

When you go through the wizard,
* cerate a dataset name like dsTableName
* make a new connection and use the Jet 4.0 protocol
* select the query from the database
* pick all fields
* show in a grid

Let me know if you need more help with this.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:nomar2
ID: 13947024
I found it...File...Add New Item
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 13947035
that works, too...

You can also look at this Q: http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21415452.html

Where I outlined more details on using the wizard
0
 

Author Comment

by:nomar2
ID: 13947109
Okay I have a dataform created called DataForm1

How would I pass the id from the selected item in the comboBox into the other form so that it shows the details of the item selected.

0
 
LVL 22

Expert Comment

by:rspahitz
ID: 13947548
I would suggest using a friendly property, although you could also do it through a simple variable.

Try this...on the dataform, go to a blank section and enter the following:

    Friend WriteOnly Property FieldID() As Integer
        Set(ByVal Value As Integer)
            ShowRecord(Value)
        End Set
    End Property

    Private Sub ShowRecord(ByVal FieldID As Integer)
Try
        oleDbSelectCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("EventID", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, False, ((System.Byte)(10)), ((System.Byte)(0)), "EventID", System.Data.DataRowVersion.Current, Convert.ToInt16(FieldID)))
Catch exc As Exception
  MessageBox.Show("You have supplied an invalid record ID")
End Try
    End Sub

---
Now, when you give a value to the property, it will give it the value of the ID you want to use.
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 13947576
Now that you have this property, in the form with the combobox, do something like this:

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        dim frm as DataForm1 = new DataForm1()
        frm.FieldID = ComboBox1.Text
        frm.Show()
    End Sub

0
 

Author Comment

by:nomar2
ID: 13959120
Okay I have the child form poppping up on the click of the item from the drop-down list.

But the fields are blank in the child.

I am wondering about my code in the parent form where the selection is made from the combox..
<code snippet>

        Dim myComm As SqlDataAdapter = New SqlDataAdapter("Select id, rec_code from   rec_types", myConn)
        Dim ds As DataSet = New DataSet
        myComm.Fill(ds, "Rec_Types")

        Dim dv As DataView = ds.Tables("Rec_Types").DefaultView
        ComboBox1.DataSource = dv
        ComboBox1.DisplayMember = "rec_code"

"rec_code" is displayed in the drop-down list with no problems...but when I click on a rec_code.. the child form pops up but the fields are blank ..so I am wondering if the "Id" is being retrieved and sent to the child form .

Also in the code for  Private ShowRecord() method...I am getting an error at the
"((System.Byte)(10)), ((System.Byte)(0))" part of the code...it says...
'Byte" is a type in 'System' and it can't be used as an expression.

Your help has been much appreciated
0
 

Author Comment

by:nomar2
ID: 13959210
I am using Sql Server for the database..
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 13959787
1) Did you click on the Load button when the form appeared?
(This can be automated by copying the code from this button into the Load Event Procedure.)

2) Are you familiar with using the debugger and breakpoints?
At this point, I would set a breakpoint at:

 frm.FieldID = ComboBox1.Text

when you get there, step IN and ensure that it goes to the property section of the data form.

If this works, then we'll have to add some extra debugging checks in the form.
0
 

Author Comment

by:nomar2
ID: 13960237
'Byte' is a type in 'System' and cannot be used as an expression.

I am still getting this error

I hovered over with the mouse to
frm.id = ComboBox1.Text

and .id points to the property section of the dataform.

I also added...
 ComboBox1.ValueMember = "id"

and I still get blanks

0
 

Author Comment

by:nomar2
ID: 13960487
When I click on the item from the drop-down list...the dataform loads...when I click the Load button..it says Logon name for my login name fails
0
 
LVL 22

Accepted Solution

by:
rspahitz earned 268 total points
ID: 13963749
It sounds like you're having problems with permission to the database or it can't find the DB.  Did you place a copy in the bin folder?

As for the (Byte) problem, make sure that that was all one line.  If you copy-pasted from here, it may have split it into multiple lines.
0
 

Author Comment

by:nomar2
ID: 13967619
I think I have this all done wrong....the database is on a server in our office and the form and dataform I am coding is on my local machine.

This is supposed to be part of a web service that is being built.

I think I am totally lost...
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 13971106
OK...if you get a clearer picture of what's needed, let me know, although I really haven't done anything with web services yet.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview
Suggested Courses

864 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