Solved

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

Posted on 2004-04-28
10
9,268 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
ID: 10938770
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
ID: 10938889
"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
ID: 10939116
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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 5

Expert Comment

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

Tom
0
 

Author Comment

by:jxharding
ID: 10946538
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
 
LVL 5

Expert Comment

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

Tom.
0
 

Author Comment

by:jxharding
ID: 10947160
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
ID: 10947253
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
ID: 10947907
once again i got more than i bargained for
thank you both!
0
 

Expert Comment

by:banna_hostage
ID: 11136555
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

778 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