|
-
Jul 30th, 2003, 04:09 AM
#1
Thread Starter
New Member
Acess dataset between 2 Windows Form
Dear all
If I have 2 form, 1 call form1 and the other call form2.
I create a connection in form1:
CLASS FORM1
PUBLIC DS As DATASET
Dim connstr As String = "DSN=TEST"
Dim cn As New OdbcConnection(connstr)
Dim DA1 As New OdbcDataAdapter("SELECT * FROM A ", cn)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cn.Open()
odbcadapter1.Fill(DS, "A")
I can successfully access DS in form1, but when I try to access DS in form2, it said" object reference not set to instance of object". It seem that it treat it as a new variable. I try this in form2:
Dim zds As DataSet
zds = form1.ds
messagebox.show(zds.tables("A").Rows.count)
But still not work. Anyone can told me how to access it from anothe table? Thank you very much.
Last edited by fung1223; Jul 30th, 2003 at 04:20 AM.
-
Jul 30th, 2003, 08:29 AM
#2
Frenzied Member
Just pass the dataset to Form2.
-
Jul 30th, 2003, 08:44 PM
#3
Thread Starter
New Member
Thank you for your reply.
But How?
Thank you
-
Jul 30th, 2003, 09:00 PM
#4
in Form1 :
VB Code:
Public ds As New DataSet()
'/// make a public reference to ds ^^^
'///////////////////////////////////////////////
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frm2 As New Form2()
AddOwnedForm(frm2) '/// add it as owned by this form.
frm2.Show()
End Sub
in Form2 :
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frmMain As Form1 = Owner
Dim zds As DataSet = frmMain.ds
MessageBox.Show(zds.DataSetName)
End Sub
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Jul 30th, 2003, 09:23 PM
#5
Thread Starter
New Member
Thank you for your reply. I have one question. Once I declar a public variable :
public ds as new dataset()
it doesnot mean I can access it everywhere? Or I can just access the variable, but not the value of the ds.....I quite confuse.
Now, It seem that I can use ds in form2 but I can not access the "value" in form2.
Another one told me a method is declare the dataset in a modules. What is the differece between declare a varibale as public and declare it in a modules. As I know, it looks like equal since both method can access the variable everywhere. Sorry for my long question. Sorry and Thank you for your help.
-
Jul 31st, 2003, 03:49 AM
#6
here's a quick example , using oledb to open an access app
in Form1 :
VB Code:
Imports System.Data
Public ds As New DataSet()
Private frm2 As New Form2()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AddOwnedForm(frm2)
frm2.Show()
End Sub
in Form2 :
VB Code:
Imports System.Data.OleDb
Private frmMain As Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim constring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyDb.mdb"
frmMain = Owner '/// owner form.
OpenAccess(constring)
End Sub
Public Function OpenAccess(ByVal strConnection As String, Optional ByVal strCommand As String = "SELECT * FROM Table1")
Dim objCon As New OleDbConnection(strConnection)
Try
objCon.Open()
Dim objCom As New OleDbCommand(strCommand, objCon)
Dim objAdapt As New OleDbDataAdapter(objCom)
objAdapt.Fill(frmMain.ds, "Table1")
DataGrid1.DataSource = frmMain.ds.Tables("Table1")
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
objCon.Close()
End Try
End Function
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|