Solved

add/edit/delete from dataset, and bind a field in dataset to a combobox,

Posted on 2004-04-28
10
9,238 Views
Last Modified: 2009-07-29
good day, the problem im having is that i need to fill textboxes according to the value the user clicks in a combobox.
the combobox is filled via a dataset.
from there i need to add edit and delete customers.
vb.net & access 2000,oledb
e.g.

combobox: (user chooses)         '1'

(then according to the userid)

textbox1:                        'Gary'
textbox2:                        'Johnson'

THE DATA SIDE
-------------
i have a table called customers
the customers table has the fields
CustomerID (autonumber)
Name       (text)
Surname    (text)

now i have a strongly typed dataadapter 'daCustomers' which only uses the customers field
the insert,delete & update commands are all working fine.
now i have a dataset which was generated by the daCustomers dataadapter.
if i preview the dataAdapter's fill dataset, i get the results fine, no problem

THE FORM SIDE
-------------
combobox1
textbox1  
textbox2
btnAdd
btnEdit
btnDelete

now the whole plan here is to be able to ADD, EDIT & DELETE customers.

this is what i got and now im stuck
i have 3 similar situations in this specific application and i would like to do this the right way so i can apply it in
all my future vb.net projects.
the specific situations are SUPPLIERS, CUSTOMERS, MANUFACTURERS in this project. so the user will click on a
TooLBAR button to select customers now.

i can honestly go screw around and eventually get it right but i cant afford it right now
so what im asking is either:

a link to a similar sample,  
OR
a step by step walkthrough, not neccessarily code, just the steps.
i will post my code if it is requested.

some of my immediate questions in my quest to finish this project
----------------------------------------------------------------
when do i bind the datasource of the combobox to the dataset?
do i do it at designtime by selecting the properties or do i do it in code when the user presses the CUSTOMERS toolbar

button?
i just set the combobox's datasource to dsCustomers.customers
and the display member to customerID, but when i run the program, and select customers, the combobox is empty


then what do i do in the click event of the combobox?
what event do i use for the combobox to now call the code that fills the textboxes?
how do i tell the textboxes to fill up with the values according to the ID in the combobox?

am i doing it right so far or would it be better to not have everything strongly typed?

would datareaders be brought in at any stage?

once again, im asking a whole bunch.
thank you!
0
Comment
Question by:jxharding
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 2

Assisted Solution

by:Jeffr0
Jeffr0 earned 250 total points
Comment Utility
I would use a DataTable as the datasource for the ComboBox.

        cboAnalysis.DataSource = MyDataTabe
        cboAnalysis.ValueMember = "ID"
        cboAnalysis.DisplayMember = "FieldName"

Use the ComboBox.SelectedValue to get the ID of the row he is currently on.

Then use a DataView object to get all of the data associated with that row:


            Dim dv As DataView = New DataView(Combos.Tables("tblProjects"))
            dv.RowFilter = "ProjectID = '" & ProjectID & "'"

Here's how you get specific bits of data from the DataView once you've set it up:

TextBox1.Text = dv.Item(0)("FieldName")
0
 
LVL 2

Expert Comment

by:Jeffr0
Comment Utility
"when do i bind the datasource of the combobox to the dataset?
do i do it at designtime by selecting the properties or do i do it in code when the user presses the CUSTOMERS toolbar"

I do it at runtime.  When the form is opened, I have a subroutine that is called from the Sub New() that initializes all of my ComboBoxes.  If the ComboBoxes are going to be different depending on the situation, then I add the necessary variables as arguements to the form's constructor (the Sub New method.)


"i just set the combobox's datasource to dsCustomers.customers
and the display member to customerID, but when i run the program, and select customers, the combobox is empty"

Did you set the DataSource, the ValueMember, and the DisplayMember?  Also... is the DataTable.Rows.Count > 0 for the table you attached to it?


"then what do i do in the click event of the combobox?
what event do i use for the combobox to now call the code that fills the textboxes?
how do i tell the textboxes to fill up with the values according to the ID in the combobox?"

The SelectedValueChanged event of the ComboBox might be the place you want.  Code for altering the TextBoxes is in my previous post.



"am i doing it right so far or would it be better to not have everything strongly typed?"

For a beginner, it will be faster to do this manually than it would be to learn how to make strongly typed stuff to work.



"would datareaders be brought in at any stage?"

Only if you need to do some batch processing where speed is more important than anything else.  For typical database apps like this, they probably aren't necessary.
0
 
LVL 5

Accepted Solution

by:
tgannetts earned 250 total points
Comment Utility
You will need to set the combobox data source when the user selects the relevent toolbar button:

'Create a DataView
Private dv as dataview

Private Sub ToolBar1_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles ToolBar1.ButtonClick

     Select case e.Button.Text
          Case "Customers"

            Me.ComboBox1.DataSource = oDs.Tables("Customers")
            Me.ComboBox1.DisplayMember = "CustomerID"
            Me.dv = new DataView(oDs.Tables("Customers"))

          Case "Suppliers"

            Me.ComboBox1.DataSource = oDs.Tables("Suppliers")
            Me.ComboBox1.DisplayMember = "SupplierID"
            Me.dv = new DataView(oDs.Tables("Suppliers"))

          ... and so on

       End Select

End Sub

Then you will need to respond to the selected index changed for the combo box to set the values in the text boxes:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        Me.dv.rowfilter = Me.dv.Table.Columns(0).ColumnName & "=" & Me.Combobox1.SelectedText
        Me.TextBox1.Text = dv.Item(0)(1).ToString
        Me.TextBox2.Text = dv.Item(0)(2).ToString

End Sub

For each of the buttons you will have to add the relevent code to respond to the required action, so:

Add:
Get the values of in the textbox and create a new row in the dataview

Edit
Update the row in the dataview with the details in the textboxes

Delete
Set the row in the dataview as to be deleted.

It is up to you then whether you run the Update method of the dataadapter after each action, or when the user specifies it.

Hope this helps.

Tom
0
 
LVL 5

Expert Comment

by:tgannetts
Comment Utility
Sorry, repeated a lot of what Jeffr0 said... cross submitted!

Tom
0
 

Author Comment

by:jxharding
Comment Utility
hi Jeffr0 & tgannetts
i am currently using bits of both your approaches, so im thankful for the cross submittion.
im just stuck on a rather small error here.
i call my dataview dvCustomer and in the line of code where i need to set the rowfilter, i can not get it take the value i

would like to pass.
i am filtering where CustomerName is equal to the text of cboCustomerName.
examples i tried

1.) dvCustomers.rowfilter = "CustomerName='" &cboCustomername.selectedtext & "'"
      if i do a constole.writeline(cbocustomername.selectedtext) it says = ""
      i also tried using cboCustomerName.SelectedText.Tostring
                             
2.) dvCustomers.rowfilter = "CustomerName='" &currentname & "'" (i declared currentname as string =

me.cbocustomername.selectedtext)
'console.writeline(Currentname) = ""


3. dvCustomers.rowfilter = dvcustomers.table.columns(1).columnname & "=" & cbocustomername.selectedtext
'here i get a missing operand after the '=' operator


im sure that this is the problem because if i were to put in
dvcustomers.rowfilter = "CustomerName='RealCustomerName'"
then all works fine
i am obvioulsy still trying this on my own, and will reply immediately if i were to get it right
thanks!
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Expert Comment

by:tgannetts
Comment Utility
My mistake, I meant to write me.combobox1.text rather than selectedtext....

Tom.
0
 

Author Comment

by:jxharding
Comment Utility
thanks, its working now. i hope this is the last one from my side :)
now when i load it the combobox the first time,no problem
all the appropriate text boxes are filled correctly
,but when i select a value from the combobox again,
obviously the view is filtered and the view has to be filled  again (i hope im right here,its just my logic)

i get the error:
Index 0 is not non-negative and belowe total rows count
can i refill it somewhow
this is how i declare it :
in the Combobox1_SelectedIndexChanged Event
      dim dvCustomers as dataview = new dataview(dsCustomers.tables("Customers"))

so i would think that  when the user clicks on the combobox for the second time

this would run again and refill the view but it does not.
i found it difficult to get some more info on dataview so far.
thanks again!
0
 
LVL 5

Expert Comment

by:tgannetts
Comment Utility
The view shouldn't need to be refilled, you just need to set the rowfilter to the new value in the combobox. So, in the SelectedIndexChanged Event sub, you just need the line:

dvCustomers.rowfilter = "CustomerName='" &cboCustomername.selectedtext & "'"

This will update the dataview, and you can then access the values to fill the textboxes.

Tom.
0
 

Author Comment

by:jxharding
Comment Utility
once again i got more than i bargained for
thank you both!
0
 

Expert Comment

by:banna_hostage
Comment Utility
How could i insert a data from VB.Net form into SQL data base,and how could i get the data again, and how to delete it from the data base after retrive it.????  Thanx alote for help...
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

Suggested Solutions

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 …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

10 Experts available now in Live!

Get 1:1 Help Now