Solved

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

Posted on 2004-04-28
10
9,293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

734 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