• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 9327
  • Last Modified:

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

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
jxharding
Asked:
jxharding
  • 4
  • 3
  • 2
  • +1
2 Solutions
 
Jeffr0Commented:
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
 
Jeffr0Commented:
"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
 
tgannettsCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
tgannettsCommented:
Sorry, repeated a lot of what Jeffr0 said... cross submitted!

Tom
0
 
jxhardingAuthor Commented:
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
 
tgannettsCommented:
My mistake, I meant to write me.combobox1.text rather than selectedtext....

Tom.
0
 
jxhardingAuthor Commented:
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
 
tgannettsCommented:
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
 
jxhardingAuthor Commented:
once again i got more than i bargained for
thank you both!
0
 
banna_hostageCommented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now