phaseshift
IS-IT--Management
I have two tables with a PK FK relationship. In my code I have set a datarelation between the table but the addnew() is not working. Can anyone look over this and see if you spot a problem.
thanks
Dim conn As New SqlConnection
Dim daclient As New SqlDataAdapter
Dim dareferrals As New SqlDataAdapter
Dim ds As New DataSet
Dim cmdclient As New SqlCommand
Dim cmdreferrals As New SqlCommand
Private Sub frm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
fileutilmod.getxmldata()
conn.ConnectionString = "Server= " & xmldataServerName & "; uid=" & xmldataUserName & " ; pwd=" & xmldataUserPass & "; database=" & xmldataDatabaseName & ";"
Try
'client info collection
cmdclient = conn.CreateCommand
cmdclient.CommandText = "Select * from tblclients"
daclient.SelectCommand = cmdclient
Dim cdclient As SqlCommandBuilder = New SqlCommandBuilder(daclient)
daclient.Fill(ds, "tblclients")
' referral info collection
cmdreferrals = conn.CreateCommand
cmdreferrals.CommandText = "Select * from tblreferrals"
dareferrals.SelectCommand = cmdreferrals
Dim cbreferrals As SqlCommandBuilder = New SqlCommandBuilder(dareferrals)
dareferrals.Fill(ds, "tblreferrals")
Dim relreferrals As New DataRelation _
("ds", ds.Tables("tblclients").Columns("clientid"), _
ds.Tables("tblreferrals").Columns("clientid"))
ds.Relations.Add(relreferrals)
'''Databinding info goes here
updateposition()
Catch ex As Exception
MsgBox(ex.Source & ": " & ex.Message)
End Try
End Sub
Private Sub btnnext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnext.Click
Me.BindingContext(ds.Tables("tblclients")).Position += 1
Me.BindingContext(ds.Tables("tblreferrals")).Position += 1
updateposition()
End Sub
Private Sub btnprev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprev.Click
Me.BindingContext(ds.Tables("tblclients")).Position -= 1
Me.BindingContext(ds.Tables("tblreferrals")).Position -= 1
updateposition()
End Sub
Private Sub updateposition()
Me.lblposition.Text = ((Me.BindingContext(ds.Tables("tblclients")).Position + 1).ToString + _
" of " + Me.BindingContext(ds.Tables("tblclients")).Count.ToString)
End Sub
Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
Try
Me.BindingContext(ds.Tables("tblclients")).EndCurrentEdit()
daclient.Update(ds.Tables("tblclients"))
Me.BindingContext(ds.Tables("tblreferrals")).EndCurrentEdit()
dareferrals.Update(ds.Tables("tblreferrals"))
Catch ex As Exception
MsgBox(ex.Source & " " & ex.Message)
End Try
End Sub
Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
Try
Me.BindingContext(ds.Tables("tblclients")).EndCurrentEdit()
Me.BindingContext(ds.Tables("tblclients")).AddNew()
Me.BindingContext(ds.Tables("tblreferrals")).EndCurrentEdit()
Me.BindingContext(ds.Tables("tblreferrals")).AddNew()
updateposition()
Catch ex As Exception
MsgBox(ex.Source & " " & ex.Message)
End Try
End Sub
thanks
Dim conn As New SqlConnection
Dim daclient As New SqlDataAdapter
Dim dareferrals As New SqlDataAdapter
Dim ds As New DataSet
Dim cmdclient As New SqlCommand
Dim cmdreferrals As New SqlCommand
Private Sub frm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
fileutilmod.getxmldata()
conn.ConnectionString = "Server= " & xmldataServerName & "; uid=" & xmldataUserName & " ; pwd=" & xmldataUserPass & "; database=" & xmldataDatabaseName & ";"
Try
'client info collection
cmdclient = conn.CreateCommand
cmdclient.CommandText = "Select * from tblclients"
daclient.SelectCommand = cmdclient
Dim cdclient As SqlCommandBuilder = New SqlCommandBuilder(daclient)
daclient.Fill(ds, "tblclients")
' referral info collection
cmdreferrals = conn.CreateCommand
cmdreferrals.CommandText = "Select * from tblreferrals"
dareferrals.SelectCommand = cmdreferrals
Dim cbreferrals As SqlCommandBuilder = New SqlCommandBuilder(dareferrals)
dareferrals.Fill(ds, "tblreferrals")
Dim relreferrals As New DataRelation _
("ds", ds.Tables("tblclients").Columns("clientid"), _
ds.Tables("tblreferrals").Columns("clientid"))
ds.Relations.Add(relreferrals)
'''Databinding info goes here
updateposition()
Catch ex As Exception
MsgBox(ex.Source & ": " & ex.Message)
End Try
End Sub
Private Sub btnnext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnext.Click
Me.BindingContext(ds.Tables("tblclients")).Position += 1
Me.BindingContext(ds.Tables("tblreferrals")).Position += 1
updateposition()
End Sub
Private Sub btnprev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprev.Click
Me.BindingContext(ds.Tables("tblclients")).Position -= 1
Me.BindingContext(ds.Tables("tblreferrals")).Position -= 1
updateposition()
End Sub
Private Sub updateposition()
Me.lblposition.Text = ((Me.BindingContext(ds.Tables("tblclients")).Position + 1).ToString + _
" of " + Me.BindingContext(ds.Tables("tblclients")).Count.ToString)
End Sub
Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
Try
Me.BindingContext(ds.Tables("tblclients")).EndCurrentEdit()
daclient.Update(ds.Tables("tblclients"))
Me.BindingContext(ds.Tables("tblreferrals")).EndCurrentEdit()
dareferrals.Update(ds.Tables("tblreferrals"))
Catch ex As Exception
MsgBox(ex.Source & " " & ex.Message)
End Try
End Sub
Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
Try
Me.BindingContext(ds.Tables("tblclients")).EndCurrentEdit()
Me.BindingContext(ds.Tables("tblclients")).AddNew()
Me.BindingContext(ds.Tables("tblreferrals")).EndCurrentEdit()
Me.BindingContext(ds.Tables("tblreferrals")).AddNew()
updateposition()
Catch ex As Exception
MsgBox(ex.Source & " " & ex.Message)
End Try
End Sub