A
al
Greetings,
I have this probelm synchronising dataset with the currencymanager.
I basically have MDI app with a form for data entry and search. In
the search part of it,there are two options for the user, either get
all records or filtered ones based on entrer employeeID. The problem
i'm facing is that when I search for second time,be the first time
getting all records or filtereing, the currencymanager gets
out-of-synch and doesn't udpate form. I do clear binding the second
time and currencymanager shows zero counts of elements. Here is the
code: if I clear bindings, can I user sam currencymanager again, if
yes, how????
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
Dim frmemp As New Employees
Dim frmcus As New customers
Dim ds As New DataSet
Dim str As String = ("data source=pc1;initial
catalog=northwind;integrated security=sspi")
Dim sql As String = "select * from employees"
Dim sql2 As String = "select * from employees where employeeid="&
empid.text
Dim mydatabinding As BindingManagerBase
Dim mycm As CurrencyManager
Dim mybc As BindingContext
Dim activeChild As Form = Me.ActiveMDIChild
Dim da1 As SqlDataAdapter
Dim da2 As SqlDataAdapter = New SqlDataAdapter(sql2, str)
Private Sub Search_ButtonClick(ByVal sender As System.Object, ByVal e
As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles
ToolBar1.ButtonClick
Try
Select Case ToolBar1.Buttons.IndexOf(e.Button)
Case 0
Case 1
If (ds.Tables("employees") Is Nothing) Then
If frmemp.txtID.Text.Length > 0 Then
da1 = New SqlDataAdapter(sql2, str)
da1.Fill(ds, "Employees")
dobinding()
ds_changexposition()
Else
da1 = New SqlDataAdapter(sql, str)
da1.Fill(ds, "Employees")
mycm = CType(Me.BindingContext(ds,
"Employees"), CurrencyManager)
dobinding()
ds_changexposition()
End If
ElseIf Not (ds.Tables("employees") Is Nothing)
Then
ds.Tables("employees").Clear()
clearbinding()
da1.Fill(ds, "Employees")
dobinding()
ds_changexposition()
End If
Case 2
Case 3
clearbinding()
End Select
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Sub clearbinding()
Dim ctl As Control
Dim txt As Control
Dim i As Short = 2
ds.Tables("employees").Reset()
For Each ctl In frmemp.Controls
If TypeOf ctl Is GroupBox Then
For Each txt In ctl.Controls
If TypeOf txt Is TextBox Then
txt.Text = ""
txt.DataBindings.Clear()
End If
Next
End If
Next
ds_changexposition()
End Sub
Sub dobinding()
frmemp.txtID.DataBindings.Add("text", ds,
"employees.employeeid")
frmemp.txtlastname.DataBindings.Add("text", ds,
"employees.lastname")
frmemp.txtfirstname.DataBindings.Add("text", ds,
"employees.firstname")
frmemp.txttitle.DataBindings.Add("text", ds,
"employees.title")
End Sub
Sub ds_changexposition()
Me.StatusBar1.Panels(1).Text = ((mycm.Position + 1).ToString)
+ "of" + (mycm.Count.ToString)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
frmemp.MdiParent = Me
frmcus.MdiParent = Me
Me.StatusBar1.Panels(0).Text = Now.ToShortTimeString
End Sub
Private Sub customers_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles customers.Click
frmcus.Show()
End Sub
Private Sub Employees_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Employees.Click
frmemp.Show()
End Sub
Private Sub ToolBar2_ButtonClick(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles
ToolBar2.ButtonClick
Select Case ToolBar2.Buttons.IndexOf(e.Button)
Case 0
mycm.Position = 0
ds_changexposition()
Case 1
mycm.Position += 1
ds_changexposition()
Case 2
mycm.Position -= 1
ds_changexposition()
Case 3
mycm.Position = mycm.Count - 1
ds_changexposition()
End Select
End Sub
End Class
I have this probelm synchronising dataset with the currencymanager.
I basically have MDI app with a form for data entry and search. In
the search part of it,there are two options for the user, either get
all records or filtered ones based on entrer employeeID. The problem
i'm facing is that when I search for second time,be the first time
getting all records or filtereing, the currencymanager gets
out-of-synch and doesn't udpate form. I do clear binding the second
time and currencymanager shows zero counts of elements. Here is the
code: if I clear bindings, can I user sam currencymanager again, if
yes, how????
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
Dim frmemp As New Employees
Dim frmcus As New customers
Dim ds As New DataSet
Dim str As String = ("data source=pc1;initial
catalog=northwind;integrated security=sspi")
Dim sql As String = "select * from employees"
Dim sql2 As String = "select * from employees where employeeid="&
empid.text
Dim mydatabinding As BindingManagerBase
Dim mycm As CurrencyManager
Dim mybc As BindingContext
Dim activeChild As Form = Me.ActiveMDIChild
Dim da1 As SqlDataAdapter
Dim da2 As SqlDataAdapter = New SqlDataAdapter(sql2, str)
Private Sub Search_ButtonClick(ByVal sender As System.Object, ByVal e
As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles
ToolBar1.ButtonClick
Try
Select Case ToolBar1.Buttons.IndexOf(e.Button)
Case 0
Case 1
If (ds.Tables("employees") Is Nothing) Then
If frmemp.txtID.Text.Length > 0 Then
da1 = New SqlDataAdapter(sql2, str)
da1.Fill(ds, "Employees")
dobinding()
ds_changexposition()
Else
da1 = New SqlDataAdapter(sql, str)
da1.Fill(ds, "Employees")
mycm = CType(Me.BindingContext(ds,
"Employees"), CurrencyManager)
dobinding()
ds_changexposition()
End If
ElseIf Not (ds.Tables("employees") Is Nothing)
Then
ds.Tables("employees").Clear()
clearbinding()
da1.Fill(ds, "Employees")
dobinding()
ds_changexposition()
End If
Case 2
Case 3
clearbinding()
End Select
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Sub clearbinding()
Dim ctl As Control
Dim txt As Control
Dim i As Short = 2
ds.Tables("employees").Reset()
For Each ctl In frmemp.Controls
If TypeOf ctl Is GroupBox Then
For Each txt In ctl.Controls
If TypeOf txt Is TextBox Then
txt.Text = ""
txt.DataBindings.Clear()
End If
Next
End If
Next
ds_changexposition()
End Sub
Sub dobinding()
frmemp.txtID.DataBindings.Add("text", ds,
"employees.employeeid")
frmemp.txtlastname.DataBindings.Add("text", ds,
"employees.lastname")
frmemp.txtfirstname.DataBindings.Add("text", ds,
"employees.firstname")
frmemp.txttitle.DataBindings.Add("text", ds,
"employees.title")
End Sub
Sub ds_changexposition()
Me.StatusBar1.Panels(1).Text = ((mycm.Position + 1).ToString)
+ "of" + (mycm.Count.ToString)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
frmemp.MdiParent = Me
frmcus.MdiParent = Me
Me.StatusBar1.Panels(0).Text = Now.ToShortTimeString
End Sub
Private Sub customers_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles customers.Click
frmcus.Show()
End Sub
Private Sub Employees_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Employees.Click
frmemp.Show()
End Sub
Private Sub ToolBar2_ButtonClick(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles
ToolBar2.ButtonClick
Select Case ToolBar2.Buttons.IndexOf(e.Button)
Case 0
mycm.Position = 0
ds_changexposition()
Case 1
mycm.Position += 1
ds_changexposition()
Case 2
mycm.Position -= 1
ds_changexposition()
Case 3
mycm.Position = mycm.Count - 1
ds_changexposition()
End Select
End Sub
End Class