Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / October 2004

Tip: Looking for answers? Try searching our database.

constraint exception when changing bindingcontext position?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John Cox - 03 Oct 2004 18:33 GMT
This is the first program I've tried to write in vb so I'll apologize
in advance for my terrible code.  :)

I've got a form with a tab control on it.  I'm working on the first
tab, binding data to controls in it.  So far I load data, the
navigation buttons and search button work, and I can add data.
Occassionally I get a ConstraintException and I can't figure out why.
The exception gets thrown right after my Update() which is in a
try/catch block.  No exception is thrown during the try/catch, but one
is thrown during the next statement to adjust the position of
bindingcontext.

I'm using visual studio.net professional on windows xp professional
with an msde db.

If you need any other info to help out feel free to post a reply or
send me email.  Thanks in advance.

Here's all the code I can think of that could be relevant:

Public Class frmStudio
   Inherits System.Windows.Forms.Form
   Dim ctrl As Control
   Dim errNumber As Integer

   Public Sub TryUpdate(ByRef da As
System.Data.SqlClient.SqlDataAdapter, _
   ByRef dt As System.Data.DataTable, ByRef er As Integer)
       Try
           da.Update(dt)
       Catch err As System.Data.SqlClient.SqlException
           er = err.Number
           If er = 2627 Then
               MessageBox.Show("School name " & txtSchoolName.Text &
" already exists." _
               & "  Change the name or delete this record and edit
the other version.")
           Else
               MessageBox.Show(err.Message.ToString)
               MessageBox.Show(err.Number.ToString)
           End If
       Catch cserr As System.Data.ConstraintException
           er = -1
           MessageBox.Show(cserr.Message.ToString)
       Catch ex As Exception
           MessageBox.Show(ex.Message.ToString)
       End Try
   End Sub
   Public Sub DisableControls(ByRef tpctrl As TabPage)
       For Each ctrl In tpctrl.Controls
           ctrl.Enabled = False
       Next
   End Sub

   Public Sub EnableControls(ByRef tpctrl As TabPage)
       For Each ctrl In tpctrl.Controls
           ctrl.Enabled = True
       Next
   End Sub

   Public Sub SetNavigationStatesStudents()
       If Me.BindingContext(DsAll, "Students").Count = 0 Then
           DisableControls(tpStudents)
           btnStudentNew.Enabled = True
       Else
           EnableControls(tpStudents)
       End If
   End Sub

   Public Sub SetNavigationStatesSchools()
       If dvSchools.Count < 2 Then
           ' there's less than 2 records so disable all navigation
           btnSchoolFirstRecord.Enabled = False
           btnSchoolPreviousRecord.Enabled = False
           btnSchoolNextRecord.Enabled = False
           btnSchoolLastRecord.Enabled = False
           lblSchoolCurrentRecord.Text =
(Me.BindingContext(dvSchools).Position + 1).ToString _
           & " of " & dvSchools.Count.ToString
       ElseIf dvSchools.Count = Me.BindingContext(dvSchools).Position
+ 1 Then
           ' we're on the last record so disable forward navigation
and enable backward
           btnSchoolFirstRecord.Enabled = True
           btnSchoolPreviousRecord.Enabled = True
           btnSchoolNextRecord.Enabled = False
           btnSchoolLastRecord.Enabled = False
           lblSchoolCurrentRecord.Text =
(Me.BindingContext(dvSchools).Position + 1).ToString _
           & " of " & dvSchools.Count.ToString
       ElseIf Me.BindingContext(dvSchools).Position = 0 Then
           ' we're on the first record so disable backward navigation
and enable forward
           btnSchoolFirstRecord.Enabled = False
           btnSchoolPreviousRecord.Enabled = False
           btnSchoolNextRecord.Enabled = True
           btnSchoolLastRecord.Enabled = True
           lblSchoolCurrentRecord.Text =
(Me.BindingContext(dvSchools).Position + 1).ToString _
           & " of " & dvSchools.Count.ToString
       Else
           ' we're in the middle of the records so enable all
navigation controls
           btnSchoolFirstRecord.Enabled = True
           btnSchoolPreviousRecord.Enabled = True
           btnSchoolNextRecord.Enabled = True
           btnSchoolLastRecord.Enabled = True
           lblSchoolCurrentRecord.Text =
(Me.BindingContext(dvSchools).Position + 1).ToString _
           & " of " & dvSchools.Count.ToString
       End If

   End Sub

   Public Sub SetControlStatesSchools()
       If dvSchools.Count = 0 Then
           DisableControls(tpSchools)
           btnSchoolNew.Enabled = True
           txtSchoolSearchPattern.Enabled = True
           btnSchoolSearch.Enabled = True
       Else
           EnableControls(tpSchools)
       End If
       SetNavigationStatesSchools()
   End Sub

   Public Sub SetControlStatesStudents()
       If Me.BindingContext(DsAll, "Students").Count = 0 Then
           DisableControls(tpStudents)
           btnStudentNew.Enabled = True
       Else
           EnableControls(tpStudents)
       End If
       SetNavigationStatesStudents()
   End Sub

   Public Sub New()
       MyBase.New()

       'This call is required by the Windows Form Designer.
       InitializeComponent()

       'Add any initialization after the InitializeComponent() call
       errNumber = 0
       Try
           DsAll.Clear()
           daStudents.Fill(DsAll)
           daSchools.Fill(DsAll)
           daLessons.Fill(DsAll)
           SetControlStatesStudents()
           SetControlStatesSchools()
       Catch err As System.Data.SqlClient.SqlException
           MessageBox.Show(err.Message.ToString)
       Catch ex As Exception
           MessageBox.Show(ex.Message.ToString)
       End Try
   End Sub

   Private Sub btnSchoolNew_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnSchoolNew.Click
       Dim drNew As System.Data.DataRowView

       'call update in case changes were made to the current record
       TryUpdate(Me.daSchools, Me.DsAll.Schools, errNumber)
       If errNumber = 0 Then
           'create a new row and add it to the dataset, binding it to
the form
           drNew = Me.dvSchools.AddNew()
           drNew("lessonSite") = 0
           drNew("schoolName") = "<New School>"
           TryUpdate(Me.daSchools, Me.DsAll.Schools, errNumber)
           If errNumber = 0 Then
               Me.BindingContext(dvSchools).Position = _
                               Me.BindingContext(dvSchools).Count - 1

               EnableControls(tpSchools)
               SetNavigationStatesSchools()
               txtSchoolName.Focus()
               txtSchoolName.SelectAll()
           Else
               errNumber = 0
           End If
       Else
           errNumber = 0
       End If
       
   End Sub

   Private Sub btnSchoolLastRecord_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnSchoolLastRecord.Click
       TryUpdate(Me.daSchools, Me.DsAll.Schools, errNumber)
       If errNumber = 0 Then
           Me.BindingContext(Me.dvSchools).Position =
Me.BindingContext(Me.dvSchools).Count - 1
           SetNavigationStatesSchools()
       Else
           errNumber = 0
       End If
   End Sub

   Private Sub btnSchoolFirstRecord_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnSchoolFirstRecord.Click
       TryUpdate(Me.daSchools, Me.DsAll.Schools, errNumber)
       If errNumber = 0 Then
           Me.BindingContext(Me.dvSchools).Position = 0
           SetNavigationStatesSchools()
       Else
           errNumber = 0
       End If
   End Sub

   Private Sub btnSchoolPreviousRecord_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnSchoolPreviousRecord.Click
       TryUpdate(Me.daSchools, Me.DsAll.Schools, errNumber)
       If errNumber = 0 Then
           '<THIS NEXT LINE IS WHERE THE CONSTRAINTEXCEPTION WAS
THROWN.
           'IT COMPLAINED ABOUT A SCHOOLID (primary key) THAT ALREADY
EXISTED.
           'SCHOOLID IS AN AUTOINCREMENT COLUMN IN MY DB
           Me.BindingContext(Me.dvSchools).Position -= 1
           SetNavigationStatesSchools()
       Else
           errNumber = 0
       End If
   End Sub

   Private Sub btnSchoolNextRecord_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnSchoolNextRecord.Click
       TryUpdate(Me.daSchools, Me.DsAll.Schools, errNumber)
       If errNumber = 0 Then
           Me.BindingContext(Me.dvSchools).Position += 1
           SetNavigationStatesSchools()
       Else
           errNumber = 0
       End If
   End Sub

   Private Sub btnSchoolDelete_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnSchoolDelete.Click
       Me.dvSchools.Delete(Me.BindingContext(Me.dvSchools).Position)
       TryUpdate(Me.daSchools, Me.DsAll.Schools, errNumber)
       If errNumber = 0 Then
           SetControlStatesSchools()
           SetNavigationStatesSchools()
       Else
           errNumber = 0
       End If
   End Sub

   Private Sub btnSchoolSearch_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnSchoolSearch.Click
       TryUpdate(Me.daSchools, Me.DsAll.Schools, errNumber)
       If errNumber = 0 Then
           dvSchools.RowFilter = "schoolName LIKE '*" &
txtSchoolSearchPattern.Text & "*'"
           SetControlStatesSchools()
           SetNavigationStatesSchools()
       Else
           errNumber = 0
       End If
   End Sub
End Class
John Cox - 06 Oct 2004 12:57 GMT
I've narrowed the problem down a little, but I'm still totally
confused by the behavior I'm seeing.  I've got a sub I call whenever I
want to update()...

Public Sub TryUpdate(ByRef da As System.Data.SqlClient.SqlDataAdapter,
_
   ByRef dt As System.Data.DataTable, ByRef er As Integer)
       Try
           da.Update(dt)
       Catch err As System.Data.SqlClient.SqlException
           er = err.Number
           If er = 2627 Then
               MessageBox.Show("School name " & txtSchoolName.Text &
" already exists." _
               & "  Change the name or delete this record and edit
the other version.")
           Else
               MessageBox.Show(err.Message.ToString)
               MessageBox.Show(err.Number.ToString)
           End If
       Catch cserr As System.Data.ConstraintException
           er = -1
           MessageBox.Show(cserr.Message.ToString)
       Catch ex As Exception
           MessageBox.Show(ex.Message.ToString)
       End Try
   End Sub

... and I've got a search button that calls TryUpdate()...

Private Sub btnSchoolSearch_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnSchoolSearch.Click
       TryUpdate(Me.daSchools, Me.DsAll.Schools, errNumber)
       If errNumber = 0 Then
           dvSchools.RowFilter = "schoolName LIKE '*" &
txtSchoolSearchPattern.Text & "*'"
           If Me.BindingContext(Me.dvSchools).Count > 0 Then
               Me.BindingContext(Me.dvSchools).Position = 0
           End If
           SetControlStatesSchools()
           SetNavigationStatesSchools()
       Else
           errNumber = 0
       End If
   End Sub

... the problem is that AFTER the call to TryUpdate(), the value of
errNumber is 0 and no exception has been thrown.  Then the program
throws an unhandled contraintException on the line after
dvSchools.RowFilter assignment.

Does changing the BindingContext position call update() on the data
adapter?  I'm totally confused why there would be a
constraintException here and have no idea how to fix it.  Any help
would be greatly appreciated.  Thanks.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.