By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,133 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,133 IT Pros & Developers. It's quick & easy.

Refreshing a main form from another form

P: n/a
Hi!

A mainform is being used to show records from a table in an sql
database.

A button on the main form will load a new form that allows the user to
add, delete, update and search certain records in the underlying table
of the main form.

My question:

If the user add's a record:

How can I refresh the underlying table of the main form from the second
form and show the changes in the datagrid on the mainform?

Here's the code in the 'Update-event' of the second form:

Dim intPosition As Integer
Dim objCommand As SqlCommand = New SqlCommand
intPosition = myCurrencyManager.Position

objCommand.Connection = myConnection
objCommand.CommandText = "UPDATE titles " & _
"SET title = @title,price = @price WHERE title_id = @title_id"
objCommand.CommandType = CommandType.Text

objCommand.Parameters.Add("@title", txtBookTitle.Text)
objCommand.Parameters.Add("@price", txtPrice.Text).DbType =
DbType.Currency
objCommand.Parameters.Add("@title_id",
BindingContext(myDV).Current("title_id"))
Try
myConnection.Open()

objCommand.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message,
"btnUpdate_EXECUTENONQUERY_ERROR", MessageBoxButtons.OK,
MessageBoxIcon.Error)
Finally
myConnection.Close()
FillDataSetAndView()
BindFields()
End Try
myCurrencyManager.Position = intPosition
ShowPosition()
StatusBar1.Text = "Record updated"
'Here I will refresh the main form, I think...
'Me.Close()

P.S
This is a training case and the example is taken from VB.NET 2nd
Edition
Me.Name

Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
gs***@hotmail.com wrote:
Hi!

A mainform is being used to show records from a table in an sql
database.

A button on the main form will load a new form that allows the user to
add, delete, update and search certain records in the underlying table
of the main form.

My question:

If the user add's a record:

How can I refresh the underlying table of the main form from the second
form and show the changes in the datagrid on the mainform?

Here's the code in the 'Update-event' of the second form:

Dim intPosition As Integer
Dim objCommand As SqlCommand = New SqlCommand
intPosition = myCurrencyManager.Position

objCommand.Connection = myConnection
objCommand.CommandText = "UPDATE titles " & _
"SET title = @title,price = @price WHERE title_id = @title_id"
objCommand.CommandType = CommandType.Text

objCommand.Parameters.Add("@title", txtBookTitle.Text)
objCommand.Parameters.Add("@price", txtPrice.Text).DbType =
DbType.Currency
objCommand.Parameters.Add("@title_id",
BindingContext(myDV).Current("title_id"))
Try
myConnection.Open()

objCommand.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message,
"btnUpdate_EXECUTENONQUERY_ERROR", MessageBoxButtons.OK,
MessageBoxIcon.Error)
Finally
myConnection.Close()
FillDataSetAndView()
BindFields()
End Try
myCurrencyManager.Position = intPosition
ShowPosition()
StatusBar1.Text = "Record updated"
'Here I will refresh the main form, I think...
'Me.Close()

P.S
This is a training case and the example is taken from VB.NET 2nd
Edition
Me.Name

Read up on the Observer design pattern, and I think you will be able to
do this. It's too long since I have used it myself, so I can't tell you
how to do it, but from what I remember this sounds like a typical case
for the Observer pattern.

Here is an article that talks about the pattern
http://www.codeproject.com/vb/net/Us...verPattern.asp

Hope you find it usefull!

__
Tor Inge Schulstad
Nov 21 '05 #2

P: n/a

Hi,
Two approaches:

1) DO NOT USE THIS ONE - IT SUCKS
In main form.Activated event add code to reload table on the main
form. - As I said, this is not, not, not good.

2) BETTER APPROACH
Use events to notify main form that it needs to update itself and
inside the event handler reload the table.

If you dont know how to do this, read Ged Mead's article on Multiple
Forms in VB.NET, Part 3 - Using Events in Multiple Forms

I would recoment you reading Part 1 and Part 2 - all 3 parts are
exceptionally good and easy to understand and follow.

Simply google for it.

Hope this help
_dino_
On 22 Sep 2005 05:55:02 -0700, gs***@hotmail.com wrote:
Hi!

A mainform is being used to show records from a table in an sql
database.

A button on the main form will load a new form that allows the user to
add, delete, update and search certain records in the underlying table
of the main form.

My question:

If the user add's a record:

How can I refresh the underlying table of the main form from the second
form and show the changes in the datagrid on the mainform?

Here's the code in the 'Update-event' of the second form:

Dim intPosition As Integer
Dim objCommand As SqlCommand = New SqlCommand
intPosition = myCurrencyManager.Position

objCommand.Connection = myConnection
objCommand.CommandText = "UPDATE titles " & _
"SET title = @title,price = @price WHERE title_id = @title_id"
objCommand.CommandType = CommandType.Text

objCommand.Parameters.Add("@title", txtBookTitle.Text)
objCommand.Parameters.Add("@price", txtPrice.Text).DbType =
DbType.Currency
objCommand.Parameters.Add("@title_id",
BindingContext(myDV).Current("title_id"))
Try
myConnection.Open()

objCommand.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message,
"btnUpdate_EXECUTENONQUERY_ERROR", MessageBoxButtons.OK,
MessageBoxIcon.Error)
Finally
myConnection.Close()
FillDataSetAndView()
BindFields()
End Try
myCurrencyManager.Position = intPosition
ShowPosition()
StatusBar1.Text = "Record updated"
'Here I will refresh the main form, I think...
'Me.Close()

P.S
This is a training case and the example is taken from VB.NET 2nd
Edition
Me.Name


Nov 21 '05 #3

P: n/a

And if you are too busy to read like I often am then here is what you
need:

1. in main form declare:
dim WithEvents form2 as new AddRecordForm()
- this will make your main form aware that we added a new record from
form 2
2. Instantiate your second form, maybe in main form load event or in
button_click events like for example in load event of main form:

if not isNothing(form2) then
if not form2.IsDisposed() then
form2.BringToFront()
form2.Show()
else
form2 = new AddRecordForm()
form2.Show()
end if
else
form2 = new AddRecordForm()
form2.Show()
end if

3. In form2, declare event to notify main form that a new record has
been added:
Event NewRecordAddedEvent()

4. Raise event from the code that adds new record. Say you add it by
clicking Add button, then in AddButton_Click event type

RaiseEvent NewRecordAddedEvent()

5. Finally, tell main form what to do then new record added from
form2. To do this, go to main form code window and click left hand
combo. Here you will find your form2 object. Select it then click on
right hand side combo above the code to select an event for it. You
should find your NewRecordAddedEvent event in the drop down list here.
select it and VB.NET will insert new event handler block in your code
as:

private sub form2_NewRecordAddedEvent() Handles
form2.NewRecordAddedEvent
' reload your list view here to refresh it
Call ReloadListView()
End Sub

This should work fine then

Hope it helps.

_dino_
On 22 Sep 2005 05:55:02 -0700, gs***@hotmail.com wrote:
Hi!

A mainform is being used to show records from a table in an sql
database.

A button on the main form will load a new form that allows the user to
add, delete, update and search certain records in the underlying table
of the main form.

My question:

If the user add's a record:

How can I refresh the underlying table of the main form from the second
form and show the changes in the datagrid on the mainform?

Here's the code in the 'Update-event' of the second form:

Dim intPosition As Integer
Dim objCommand As SqlCommand = New SqlCommand
intPosition = myCurrencyManager.Position

objCommand.Connection = myConnection
objCommand.CommandText = "UPDATE titles " & _
"SET title = @title,price = @price WHERE title_id = @title_id"
objCommand.CommandType = CommandType.Text

objCommand.Parameters.Add("@title", txtBookTitle.Text)
objCommand.Parameters.Add("@price", txtPrice.Text).DbType =
DbType.Currency
objCommand.Parameters.Add("@title_id",
BindingContext(myDV).Current("title_id"))
Try
myConnection.Open()

objCommand.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message,
"btnUpdate_EXECUTENONQUERY_ERROR", MessageBoxButtons.OK,
MessageBoxIcon.Error)
Finally
myConnection.Close()
FillDataSetAndView()
BindFields()
End Try
myCurrencyManager.Position = intPosition
ShowPosition()
StatusBar1.Text = "Record updated"
'Here I will refresh the main form, I think...
'Me.Close()

P.S
This is a training case and the example is taken from VB.NET 2nd
Edition
Me.Name


Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.