Monday, March 31, 2008

Finding Details Window

Title: Finding a Detail Window in a Master/Detail Scenario
Issue: Details Window Open again and again
Level: Intermediate
Knowledge Required:
To understand the following solution you must have the knowledge of:
  • Win Forms
  • Properties
  • Events
Description:
Consider a scenario, we have created 2 Forms
  1. Master Form i.e. frmStudentMaster
  2. Detail Form i.e. frmStudentDetail
In Master Form we have created a list of Students in which we are displaying his/her Name and Father Name. We have placed 2 buttons on the same form i.e. "New Student" and "Edit Student" what we want to do is:

if "New Student" button is clicked then another form should be displayed (Detail Form) which takes the full detail of student and saves it in database

if the second button "Edit Student" is clicked then same Detail Form is displayed but this time user is allowed to make changes and save them in database again.

We are NOT focusing here that how to create list and how to save data in database instead we are focusing here a situation when "Edit Student" button is clicked consider the following code:

Dim Student_ID As Integer
Dim frmNew As frmStudentDetail
Student_ID = Me.SelectedStudentID
frmNew = New frmStudentDetail(Student_ID)
frmNew.Show()


Above code is executed when "Edit Student" button is clicked. What actually happens that procedure first gets the Currently Selected Student ID by calling Forms Private Property "SelectedStudentID" then it creates a new instance of frmStudentDetail by passing the Student ID so this form opens up with Student Details to edit.

This works fine if just edit the student and save it. What if user clicks the "Edit Student" Button and after window is opened, goes back in Master Window and again clicks on Edit button then another instance of the Detail Window will appear, which is actually wrong since 2 windows of same student is appearing simaltaneously. We want that whenever "Edit Student" button is clicked then system do:

Check that Form is Opened?
If Yes Then
    Set Focus On That Form
Else
    Create New Instance

In my previous post I have demonstrated how to find a form whether it is opened or NOT. We are going to add few things in order to acheive our goal. For this purpose we will add a Public Readonly Property to Detail Form as:

Private _StudentID As Integer

Public ReadOnly Property StudentID()
As Integer
    Get
        Return _StudentID
    End Get
End Property


Public Sub New(ByVal Student_ID As Integer)
    InitializeComponent()
    Me._StudentID = Student_ID
End Sub


In above code StudentID property will tell us about the Detail Form and finally in the "Edit Student" Button Click Event we will change code as:

Dim Student_ID As Integer
Student_ID = Me.SelectedStudentID

For Each frmEach As Form In My.Application.OpenForms
    If TypeOf frmEach Is frmStudentDetail Then
        If CType(frmEach, frmStudentDetail).StudentID = Student_ID Then
            frmEach.Activate()
            Exit Sub
        End If
    End If
Next


Dim frmNew As frmStudentDetail
frmNew = New frmStudentDetail(Student_ID)
frmNew.Show()


In the above loop when the Student Detail window is found then system first check that is this the same window has the same Student ID which is going to be opened then system will activate this form and will Exit the Sub, this will prevent the creation of 2nd instance of same Student ID.

Saturday, March 29, 2008

How To Find a Form in Open Forms

Title: Finding Form in Open Forms
Issue: How to find a Form whether it is already opened or NOT
Level: Beginner
Knowledge Required:
To understand the solution you must have the knowledge of following things
  • For Each Loop
  • Win Forms

Description:
Sometimes it is required to find a Form in Open Forms, for this purpose we can use the following code:

For Each frmEach As Form In My.Application.OpenForms
    If TypeOf frmEach Is frmManageOrders Then 
        frmEach.Show()
        frmEach.Activate()
        Exit For
    End If
Next

Thursday, March 27, 2008

TableAdapters and Transactions

Another greate work by Beth Massi:

http://blogs.msdn.com/bethmassi/archive/2007/07/11/tableadapters-and-transactions.aspx

Working with Table Adapters and Related Data Tables

I have found a very good topic of Beth Massi from Microsoft about Table Adapters here is the link

http://blogs.msdn.com/bethmassi/archive/2007/07/10/working-with-tableadapters-related-datatables-and-transactions.aspx

Identity Column Primary Key Violation in Typed DataSet

Title: Primary Key Violation Exception on Identity Column while Updating via Table Adapter
Issue: Exception occurs when Table Adapter's Update method is called
Level: Advanced
Knowledge Required:
To understand the following solution you must have the knowledge of:
  • Typed DataSets
  • Table Adapters
  • Tables in Database
  • Identity Columns
Description:
Identity Column in a Typed DataSet may throw Primary Key Voilation Exception while Updating via Table Adapter. This happens due to the incorrect use of AutoIncreamentSeed and AutoIncreamentStep Properties.

For example:
We have a Typed DataSet called StudentDataSet having a table called StudentDataTable which has an Identity Column i.e. Student_ID

By default DataSet Designer sets following properties for the Identity Column as,
AutoIncreamentSeed = 0
AutoIncreamentStep = 1

So when we add 2 rows in the said table the first row will have Student_ID = 0 and the second Row will have Student_ID = 1

Now if we give this Data Table to Table Adapter for update with the following code as:

StudentTableaAapter.Update(StudentDataSet.Student)

The table adapter will insert the first row in Database using Insert Stored Procedure then stored procedure will return the Student_ID which is actually returned by the Database (since this is also an identity column in database) so suppose database returned New Student_ID = 1 and now the table adapter will refresh this row and try to replace the Student_ID = 0 with Student_ID = 1 but at the same time we have a second row in the DataTable having Student_ID = 1 which will cause an exception of violating of Primary Key or Constraint.

So to overcome this issue the simple solution is to set the:
AutoIncreamentStep = -1
this will create the next value to -1 and will never be equal to the value returned by database (if database has an AutoIncreamentStep = 1)

In the Name of ALLAH

In the Name of ALLAH the most beneficent and merciful

Starting my blog dedicated to Software Development