Hi guys,
Im having trouble with this code, when i had it working with static variables set, it was fine, now i am using a passed variable, its not working... Might not be anything to do with it, anyhow...
Public Class frmContacts
Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
Dim projectid As Integer
Private Sub frmContacts_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call reload()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
End Sub
Public Sub clear()
ds.Tables("contacts").Clear()
End Sub
Public Sub reload()
con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\EZaudit.mdb"
con.Open()
sql = "SELECT id As pk, contacts.p_id As id, contacts.con_name as Name, contacts.con_position AS JobTitle, contacts.con_email AS Email, contacts.con_phone AS PhoneNo FROM contacts WHERE contacts.p_id=" & projectid & ""
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "contacts")
con.Close()
dgCon.DataSource = ds.Tables("contacts")
dgCon.Visible = True
dgCon.Columns("id").Visible = False
dgCon.Columns("pk").Visible = False
dgCon.Columns("Name").Width = 100
dgCon.Columns("JobTitle").Width = 100
dgCon.Columns("Email").Width = 145
dgCon.Columns("PhoneNo").Width = 120
End Sub
Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
Dim entry As New frmConEntry()
entry.ProjectId3 = projectid
entry.Show()
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim cb As New OleDb.OleDbCommandBuilder(da)
da.Update(ds, "contacts")
MsgBox("Records Updated")
End Sub
Public Property ProjectId5() As String
Get
Return projectid
End Get
Set(ByVal Value As String)
projectid = Value
End Set
End Property
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.clear()
Me.reload()
End Sub
End Class
The 2 public subs being clear, and reload.
If i call them within the same form - with button2.click, then it works as it should... but the following code in another form...
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
dsNewRow = ds.Tables("contacts").NewRow()
dsNewRow.Item("id") = projectid
dsNewRow.Item("Name") = txtName.Text
dsNewRow.Item("JobTitle") = txtJTitle.Text
dsNewRow.Item("Email") = txtEmail.Text
dsNewRow.Item("PhoneNo") = txtPhoneNo.Text
ds.Tables("contacts").Rows.Add(dsNewRow)
da.Update(ds, "contacts")
MsgBox("New Record added to the Database")
txtName.Text = ""
txtJTitle.Text = ""
txtEmail.Text = ""
txtPhoneNo.Text = ""
frmContacts.clear()
frmContacts.reload()
End Sub
frmContacts.clear(), just does not clear, it gives a "NullReferenceException - Object reference not set to an instance of an object" and the reload doesn't seem to reload the set of data which was loaded the first time round...
Very Confusing... Any ideas? Everything else is working fine, but i dont want to have to click a button to refresh the table every time i add a contact in a seperate form...
Any ideas?