 |
| Pro VB Databases Advanced-level VB coding questions specific to using VB with databases. Beginning-level questions or issues not specific to database use will be redirected to other forums. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Pro VB Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

March 29th, 2007, 11:57 PM
|
|
Authorized User
|
|
Join Date: Jan 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Mixed results.
1. If there are more than 1 result it goes to Tabpage2, correctly.
2. If there is 1 result it will open Tabpage1, correctly.
But there is no data in the Tabpage1. The data is still in the DataGridView Tabpage2. Tabpage1's FirstName TextBox has only the query string entered previously.
3. Doubleclicking on Rowheader does not work.
-----------------------------------------------------------------------------------
'This is the RowHeaderMouseDoubleClick event:
Private Sub Table1DataGridView_RowHeaderMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArg s) Handles DataGridView1.RowHeaderMouseDoubleClick
Me.TabControl1.SelectedTab = TabPage1
End Sub
---------------------------------------------------------------------------------------
The original code I had works flawlessly ---- well, except it doesn't do what I am after, which is to query on the fly so to speak.
Private Sub Find_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Find.Click
IF FNTextBox.Text <> "" Then
Try
Me.Table1TableAdapter.FillBy(Me.NewAppDBDataSet.Ta ble1, FNTextBox.Text, LNTextBox.Text)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
ElseIf LNTextBox.Text <> "" Then
Try
Me.Table1TableAdapter.FillBy(Me.NewAppDBDataSet.Ta ble1, FNTextBox.Text, LNTextBox.Text)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
ElseIf IDTextBox.Text <> "" Then
Try
Me.Table1TableAdapter.FillBy1(Me.NewAppDBDataSet.T able1,Integer.Parse(IDTextBox.Text))
Catch ex As Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
ElseIf MSTextBox.Text <> "" Then
Try
Me.Table1TableAdapter.FillBy2(Me.NewAppDBDataSet.T able1, New System.Nullable(Of Integer)(CType(MSTextBox.Text, Integer)))
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
ElseIf DOBTextBox.Text <> "" Then
Try
Me.Table1TableAdapter.FillBy3(Me.NewAppDBDataSet.T able1, New System.Nullable(Of Date)(CType(DOBTextBox.Text, Date)))
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
ElseIf ************.Text <> "" Then
Try
Me.Table1TableAdapter.FillBy4(Me.NewAppDBDataSet.T able1, ************.Text)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End If
Dim result As Integer = 0
result = Table1BindingSource.Count
If result > 1 Then
Me.TabControl1.SelectedTab = TabPage2
End If
End Sub
|
|

March 30th, 2007, 10:22 AM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I am not sure I understand everything you want to do. For example.. what kind of object is Table1TableAdapter? and so on... too much to cover easily here. The following line looks like it should be opening TabPage1, and not TabPage2:
You say you want this to be happening:
2. If there is 1 result it will not open Tabpage2,correctly.
But this code says TabPage1 should be open, not TabPage2 when there is only one found:
Code:
If dv.Count > 1 Then
Me.TabControl1.SelectedTab = TabPage2
ElseIf dv.Count = 1 Then
Me.TabControl1.SelectedTab = TabPage1
End If
But like they say - give a man a fish, and you feed him for a day, teach him to fish and eventually he will deplete the ocean of fish.
So I am going to try to teach you to fish.
Isolate the behavior you want to have happen in a little separate sample app, and make it work there. Always spike the simplest scenario possible. Experiment Fast and Small, and you will Learn Fast.
Woody Z
http://www.learntoprogramnow.com
How to use a forum to help solve problems
|
|

March 30th, 2007, 02:45 PM
|
|
Authorized User
|
|
Join Date: Jan 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Your critic about style and methods are valid - and I am going to do "my" best as to not rely on help. Pursuing your and Gonzalo's critic I have changed the code to conform it to Option Strict On and I will try to follow-up on all your other suggestions (reduce the number of lines of the code, make it easier to read).
The logic of my trivial application:
1. If query result is more than one then open "DataGridView"- Tabpage2 (this is done by your code). Next step would be to choose one person among the DataGridView's list by clicking on the RowHeader and display that person's data in the "EntryForm" - Tabpage1. The reason? To see or modify that person's data.
2. If query result = 1 then open "EntryForm" - Tabpage1 with that person's data.
The previous solution (me fishing-without help) with the TableAdaper.Fill method works well, it satisfies all the above requirement. In the TableAdapter scheme of things, this works because all the TextBoxes are associated with the Database corresponding data. But this kind of query will not allow for ad hoc queries.
My crying for help was about how to be able to query by TextBox.text for the First and Last Name. It is obvious that the code "DataGridView1.DataSource = dv" will not display data in the DataEntry - Tabpage1.
I need a code a la:
If dv.Count > 1 Then
Me.TabControl1.SelectedTab=Tabpage2
DataGridView.DataSource = dv
Elseif dv.Count=1
Me.TabControl1.SelectedTab = Tabpage1
"Fill.EntryFormTextBoxes"
End if
The problem with the fishing analogy is that I need at least a hook and a line and to know where the fish are.
And as you said, there are many solutions for the same problem.Me fishing again: the code below would do, essentially, the same as your code (but again, it does not display data in TabPage1):
Dim myConn As New OleDbConnection
myConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\..\DB.mdb;Jet OLEDB:Engine Type=5;"
myConn.Open()
Dim myCMD As New OleDbCommand
myCMD.CommandText = "Select * from Table1 where FN LIKE '" & FNTextBox.Text & "%'"
myCMD.Connection = myConn
Dim myAdapter As New OleDbDataAdapter
myAdapter.SelectCommand = myCMD
Dim ds As New DataSet
ds.Clear()
myAdapter.Fill(ds, "Table1")
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Table1"
|
|

April 3rd, 2007, 09:58 AM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Unfortunately, it isn't clear to me what you are trying to do.
Is the selected tab being set properly? Is it that you merely need to populate that tab with the data?
It seems to me that this is what you are saying. Is that correct?
Woody Z
http://www.learntoprogramnow.com
How to use a forum to help solve problems
|
|

April 3rd, 2007, 02:50 PM
|
|
Authorized User
|
|
Join Date: Jan 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Yes, that is correct.
I do not blame you. I went back to see some of my posts - and I can tell you, it was very, very hard to understand them.
I try:
My application has a Form with a TabControl. This control has twoTabPages (TAbPage1 and TabPage2).
TabPage1 is for "Data Entry" with TextBoxes: ID, FirstName, LastName, DOB, Phone,Notes. There are Buttons on this TabPage1: Delete, Clear, Find, Save.
TabPage2 is for "DataGridView". This TabPage2 is needed to see the results, when result is more than one. This DataGridView is a regular Datagridview with Row and ColumnHeaders.
The whole point of this:
1. The user has to be able to perform partial queries (you addressed this part with your solution). Result should display in DataGridView. (done)
Now the user choose one of the Names from the results in DataGridView by clicking the RowHeader of the desired row. TabPage1 opens with this row's full data. (I would like to solve this problem)
2. As it is now, although the result is one, the data still displays in DataGridView only. Wrong. If query result is one it should display data in TabPage1. (I would like to solve this problem)
================================================== =================================
I tried a newbie solution to this (me fishing again). Read the selected row's ID, transfer it to the IDTextBox of TabPage1 and perform a TableAdapter.Fill query on it.
While this works in debug mode (it does not when the application is deployed), it is a ball of wax.
Private Sub DataGridView1_RowHeaderMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArg s) Handles DataGridView1.RowHeaderMouseClick
IDTextBox.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToSt ring
Me.TabControl1.SelectedTab = TabPage1
If IDTextBox.Text <> "" Then
Try
Me.Table1TableAdapter.FillBy1(Me.NewAppDBDataSet.T able1,Integer.Parse(IDTextBox.Text))
Catch ex As Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End If
End Sub
|
|

April 3rd, 2007, 04:01 PM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
There are many unknowns.
Why is it that you expect the data to display in TabPage1? Do you expect this to happen automatically? Have you bound all the controls there to the grid? Have you written code to do this?
Woody Z
http://www.learntoprogramnow.com
How to use a forum to help solve problems
|
|

April 3rd, 2007, 07:15 PM
|
|
Authorized User
|
|
Join Date: Jan 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
1. I expect the data to be displayed in TabPage1, because that is the main entry point to the database. The DataGridView is secondary, auxiliary tool. It is used only if the user forgot the name's exact spelling.
The data entry page is for the seeing, entering, deleting and FREQUENTLY updating data.
2. Of course not. I see the problem in these lines:
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Table1"
These lines obviously will not display data in TabPage1. I tried to address that with the code in my post above. Reading the selected row's ID number, placing it into the IDTextBox of the Data Entry TabPage1 and perform a TableAdapter.Fill query. But it is a ball of wax. It brings lot of other problems with it. Many unknown unknow-s. Also, as I mentioned in that post, that code will not work in a deployed application.
3. How to bound the controls to the grid? - this is my question too. A known unknown.
(In VB05Express MS made it too easy to drag-and-drop data Columns from the Data Sources Window into the corresponding TextBoxes of a Form, or just drag-and-drop a whole Table into a Form to have DataGridView. But that left me again with the problem of the rigid
query string (no feedback from TextBoxes).
4. I tried in my previous post. Again, I realize that, that is not data binding per se. A known unknown.
|
|

April 4th, 2007, 12:25 AM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Okay. Are you saying that you have no code or no binding that will display data in your TabPage1?
What don't you write code or bind those controls to a datatable to display the data???
Is this what you have been asking? Are you asking "How do I display data in textboxes?????"
If so: Don't mess with databinding for now. Just set the text property of each textbox to display the corresponding value from the record you want to edit. Once you have done that, you are going to need code that allows you to verify changes, and then save the data to the database.
If this is your situation, I highly recommend that you DON'T USE THE TABS for now. Write an editing form that displays modally (that is, as a dialog) when you have selected a record to edit. You are making things very difficult for yourself by trying to write a relatively complex app when you are still learning the basics. Get the basics down, one by one, and then go on to making your more complex app.
I don't know how to better say this: You need to master each small part first. Get a book on this and follow it step by step. Try to find a book that doesn't use drag and drop grids and textboxes. Learn to write the code yourself... then later learn the short-cuts.
Make things easier for yourself.
Woody Z
http://www.learntoprogramnow.com
How to use a forum to help solve problems
|
|

April 4th, 2007, 02:45 AM
|
|
Authorized User
|
|
Join Date: Jan 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I agree. I know that I know very little if anything.
I got the binding of the TextBoxes to the database by trial and error. This doesn't mean of course, that I do know what I am doing. My fishing ability is blind intuition and not knowledge.
One question: to query for the Last Name (LN) by
myCMD.CommandText = "Select * from Table1 where LN LIKE '" & LNTextBox.Text & "%'"
do I have to repeat the whole connections etc lines, or can I include this query somewhere else?
Fishing tasks:
1. I had to rewrite the Clear event. For now, I just clear the TextBoxes one by one in code. I guess it would be more efficient (less lines) to clear the dataset, if it is possible.
2. I will have to rewrite the database save (update) event. An other headache - the last one, hopefully.
Any suggestions?
|
|

April 4th, 2007, 10:07 AM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
There are numerous ways to do what you want to do. Here is one way.
To keep things simple, I would suggest the following approach:
1.
Use one form to display the grid and to allow users to select a record to edit.
Use another form for editing. There are ways to put both of these functional areas on a single form, but it is best to follow the "single responsibility" rule, especially when you are first learning this stuff.
2.
Write a class that decouples (conceptually, anyway) your database access from the rest of your code. Lets call it DAL for data access layer.
You can make a public method (call it GetDataTable, perhaps) in that class that returns a DataTable for whatever select statement you pass in.
This method will declare and instantiate the objects needed (the connection, adapter, command, and table objects, and whatever else).
For now, your connection string can hard coded. Later on you can explore more flexible ways to do things.
3.
The code in each form will have a method that instantiates the DAL and calls the GetDataTable method
4.
In the grid form use the DataTable to populate the grid.
When the user selects a record and clicks a button, instantiate and display the Edit form by calling a method you write on the Edit form called ShowEdit and that accepts the Id number of the record you want to edit
5.
In the Edit form ShowEdit
- Call the DAL.GetDataTable passing in a select statement that retrieves the DataTable that contains the single record of the Id number you passed in
- Display the form itself (using ShowDialog) and populate each textbox with the appropriate field from the record in the DataTable.
After you get all that working, then it will be time to move on to how to actually save the edited record, and how to validate user input, and how to prohibit bad data from being entered.
Woody Z
http://www.learntoprogramnow.com
How to use a forum to help solve problems
|
|
 |