Results 1 to 14 of 14

Thread: How do you reference DataSets between forms?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518

    How do you reference DataSets between forms?

    I'm working with a DataSet created with the DataSet component (the one that ends up in the component tray) and I'd like to have multiple forms that can all access the same dataset - how do you reference a dataset created in Form1 with code in Form2? When I try to do this, the dataset appears to be undeclared.

    Right now I am just working with generic controls stuck in a panel and that works okay, so this isn't a horrible problem but if some kind person could toss me a bone I'd appreciate it. <--- complete newbie at VB programming

    Thanks!

  2. #2
    New Member
    Join Date
    Oct 2004
    Posts
    1

    Unhappy Dataset referencing

    I need to reference a dataset on another form, but it keeps telling me that it is not declared. I was wondering if you have found a solution for it. I have tried all types of things but at this stage the only way I can think of is through SQL commands, then refencing the database table and field.

    This approach still hasnt worked for me, so I am a little lost.

    Hope you have had some resolve in your attempts to work out this problem.

    Thanks.............

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    How and where have you instantiated form1 and form2?

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Please don't PM me unless the content is that of either

    a) absolute secrecy
    b) a crazed mind



    Now, I'll ask again. HOW are you declaring and instantiating them? in other words, show your code.

  5. #5
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604

    Re: How do you reference DataSets between forms?

    hey Mendhak
    You scared these guys off!

    Well, I'm doing my first full project in .NET finally.
    Unfortunately it has a database requirement so it's also my first ADO.NET project.

    Following the book I'm reading "Introduction to Visual Basic.NET", I've pieced together this set of code to set up the connection, open it, and populate a data table from my SQL Server DB. but since it was all DIM'd, I don't know how to access the dataset outside the sub where I initialized it. I'm sure I don't have to initialize a new dataset every time I want to add a recordset to memory. And vb.net won't let me change the declarations to Public, so I also don't know how to reference the dataset outside that sub, much less the form. I thought putting the dataset init inside the project's startup form's NEW sub, I'd be able to access it throughout the project. I haven't figured out how to use formless modules yet, if the concept is even the same in .NET.

    Any thoughts?

    If you're interested, I'll paste the codes I'm using with my own comments (should they be inaccurate):

    'open a connection
    Dim connString As String = "Provider= SQLOLEDB.1;Data Source=ipaddress;uid=user;pwd=password;Initial Catalog=database;"
    Dim myConn As New OleDbConnection
    myConn.ConnectionString = connString
    myConn.Open()
    'build a command
    Dim sqlStatement As String = "SELECT * FROM tblUsers"
    Dim myComm As OleDbCommand = New OleDbCommand(sqlStatement, myConn)
    'set up a DataAdapter needed to build a DataSet
    Dim dsCommand As New OleDbDataAdapter
    dsCommand.SelectCommand = myComm
    'declare a dataset
    Dim dsData As New DataSet
    dsCommand.Fill(dsData, "Users")
    'MsgBox(dsData.Tables.Item("Users").Rows(0).Item("Username").ToString)

    I was able to get this Msgbox to show the first username in the RS.
    Beyond that, I haven't made much progress yet.

    Since it was all dimmed in the sub, I can't reference it anywhere else.
    Where/how should this be set up?

    Thanks if you have time to address this.
    Wen Gang, Programmer
    VB6, QB, HTML, ASP, VBScript, Visual C++, Java

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How do you reference DataSets between forms?

    Declare it Public outside the sub.

  7. #7
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: How do you reference DataSets between forms?

    Hi,

    To be precise, declare it public (or friend etc) in the form or module in the normal way

    Public dsTemp As Dataset

    Then when you want to create the instance use dsTemp as a reference

    dsTemp=New Dataset( "whatever")

    Provided you have not altered the default scope of the form (Friend) that will be accessable or destroyable from anywhere
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How do you reference DataSets between forms?

    That's my friend taxes, Mr. Precise.

  9. #9
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604

    Re: How do you reference DataSets between forms?

    I see.
    But that's mildly confusing for me.
    Public dsTemp as Dataset
    This is the Dataset class then?
    dsTemp=New Dataset( "whatever")
    This is instantiating one Dataset every time you want to use a dataset?

    But the way this book I'm reading introduces the dataset, it was different from recordset from the ADO because it could be used all over the project, adding table to the same object as needed. Maybe I'm misunderstanding that.
    But if I did it this way, it's use in the application is essentially the same as the recordset, declaring a new one, every time I want to retrieve data.

    If I'm not being clear on that, what I thought this book was saying was after this instance:
    Dim dsData As New DataSet
    dsCommand.Fill(dsData, "Users")

    if I needed another recordset later, I could fill it in the same instance:
    dsCommand.Fill(dsData, "Products")

    It doesn't explicitly say that, just seems to read that way in describing how a Dataset is different from a Recordset.

    Well, the other thing that through me was I haven't been able to publicly declare anything, at the top of the codepage where I usually would. It says statement is not valid in a namespace. Same with trying to place it in a blank module.

    So, my first .NET application, lots to learn.
    Wen Gang, Programmer
    VB6, QB, HTML, ASP, VBScript, Visual C++, Java

  10. #10
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604

    Re: How do you reference DataSets between forms?

    okay, i got that last part. inside the form class, after the inherits statement.
    Wen Gang, Programmer
    VB6, QB, HTML, ASP, VBScript, Visual C++, Java

  11. #11
    Member
    Join Date
    May 2005
    Posts
    54

    Re: How do you reference DataSets between forms?

    Hmmm

    Maybe I can get help here. I have a similar problem. I have created all my dataAdapters and Dataset on a form and can only reference the dataset from other forms.This means I cant call the dataAdapters fill command from another form. I figured out how to create references for the datasets already.

    I created all the adapters using the Designer. Am I going about this the wrong way?

  12. #12
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: How do you reference DataSets between forms?

    Hi wengang,

    Not quite sure what you are asking but I'll have a guess.

    "Public dsTemp as Dataset
    This is the Dataset class then?
    dsTemp=New Dataset( "whatever")
    This is instantiating one Dataset every time you want to use a dataset?"

    dsTemp has been allocated to holding a reference to a dataset. There will be only one dsTemp in existence.

    dsTemp=New Dataset creates the instance of a Dataset. Whilst it IS possible to hold several datasets with the same name it is highly dangerous. You can repeatedly use the same dsTemp and re-fill it from other (or the same ) datasource but you have to clear it each time first. e.g.

    dsTemp.Clear

    Hope that helps.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  13. #13
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: How do you reference DataSets between forms?

    Quote Originally Posted by 99pshop
    Hmmm

    Maybe I can get help here. I have a similar problem. I have created all my dataAdapters and Dataset on a form and can only reference the dataset from other forms.This means I cant call the dataAdapters fill command from another form. I figured out how to create references for the datasets already.

    I created all the adapters using the Designer. Am I going about this the wrong way?
    Hi, Because this is VISUAL basic it is, of course, quite correct to create any control in the Design view, but you will learn more about controls, especially data related controls, if you build them in code. This is because you are forced to consider every property and method individually.

    Either I am completely at sea on this one or you have mis-stated your problem in your post. I HOPE you mean that you cannot reference the dataset from the form on which it was created!!!!!! If not, I'm afraid I haven't a clue as to what you have done.

    Assuming I am correct, check the modifiers property of all the controls you created in design view. Make sure it is Public or Friend. Then make sure the form which is holding them was instanced as public or friend. If you created the instance of that form with a DIM from another form, you are only going to access them from the form itself and the form which created the instance of that form.

    Hope that is clear but let me know if it is not.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  14. #14
    Member
    Join Date
    May 2005
    Posts
    54

    Re: How do you reference DataSets between forms?

    The problem is vb.net allows you to reference your dataset from another form but I cant seem to find a way to reference the data Adapter from other forms so I can refill the dataset

    e.g

    foreignform.dataadapter1.fill(dataset,"table")

    I have gone down the route of creating a module with functions for getting and filling public dataset in code. I can fill it from the functions but having problem now with exposing the propertys so I can use it with comboboxes. Ie Datamember and Valuemember in code?

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