Results 1 to 6 of 6

Thread: Acess dataset between 2 Windows Form

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Posts
    8

    Question 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.

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Just pass the dataset to Form2.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Posts
    8
    Thank you for your reply.
    But How?
    Thank you

  4. #4
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    in Form1 :
    VB Code:
    1. Public ds As New DataSet()
    2. '/// make a public reference to ds ^^^
    3. '///////////////////////////////////////////////
    4. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.     Dim frm2 As New Form2()
    6.     AddOwnedForm(frm2) '/// add it as owned by this form.
    7.     frm2.Show()
    8. End Sub
    in Form2 :
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim frmMain As Form1 = Owner
    3.     Dim zds As DataSet = frmMain.ds
    4.     MessageBox.Show(zds.DataSetName)
    5. 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]

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Posts
    8
    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.

  6. #6
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    here's a quick example , using oledb to open an access app
    in Form1 :
    VB Code:
    1. Imports System.Data
    2.  
    3. Public ds As New DataSet()
    4. Private frm2 As New Form2()
    5.  
    6. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7.     AddOwnedForm(frm2)
    8.     frm2.Show()
    9.  
    10. End Sub
    in Form2 :
    VB Code:
    1. Imports System.Data.OleDb
    2.  
    3.     Private frmMain As Form1
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.         Dim constring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyDb.mdb"
    7.         frmMain = Owner '/// owner form.
    8.  
    9.         OpenAccess(constring)
    10.     End Sub
    11.  
    12.     Public Function OpenAccess(ByVal strConnection As String, Optional ByVal strCommand As String = "SELECT * FROM Table1")
    13.         Dim objCon As New OleDbConnection(strConnection)
    14.         Try
    15.             objCon.Open()
    16.             Dim objCom As New OleDbCommand(strCommand, objCon)
    17.             Dim objAdapt As New OleDbDataAdapter(objCom)
    18.  
    19.             objAdapt.Fill(frmMain.ds, "Table1")
    20.             DataGrid1.DataSource = frmMain.ds.Tables("Table1")
    21.         Catch ex As Exception
    22.             MessageBox.Show(ex.Message)
    23.         Finally
    24.             objCon.Close()
    25.         End Try
    26.  
    27.     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
  •  



Click Here to Expand Forum to Full Width