[RESOLVED] Accessing DataTables from Windows Form
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: [RESOLVED] Accessing DataTables from Windows Form

  1. #1
    Join Date
    Feb 2003
    Location
    Germany
    Posts
    87

    [RESOLVED] Accessing DataTables from Windows Form

    Hi, just started playing around with C#, and ran promptly into a problem. So here is -I think - a common newbie question:
    Using VS9 and .NET 4.0, I used the designer to construct a solution with a dataset (DataSet1) with DataTables(DataTable1,DataTable2) and a Windows Form (Form1) with Datagridviews for the Tables - no own coding so far. Then I tried to access a Table from Form1, which failed.

    DataSet1:
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    
    namespace CSWF {
    
        public partial class DataSet1
        {
    
        }
    }
    Form1:
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    
    namespace CSWF
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
           
    //My attempt to deal with the data tables:     
                try
                {
                     DataTable1.ReadXML("Set1.XML",System.Data.XmlReadMode.Auto);
    
                }
                catch (System.IO.FileNotFoundException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
    }
    How can I enable Form1 to access DataTable1 ?

    Thx adv.
    Last edited by Wild Thing; November 9th, 2010 at 09:28 AM.
    prepare that the light at the end of your tunnel is just a freight train coming your way - metallica

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,961

    Re: Accessing DataTables from Windows Form

    Download the 101 Samples. They have examples for VB.Net as well as C# in VS2008 or VS2010. Get the one that you like
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Feb 2003
    Location
    Germany
    Posts
    87

    Re: Accessing DataTables from Windows Form

    @dglienna: thanks. Just a small correction: the link is VB, C# is http://code.msdn.microsoft.com/cs2008samples
    I will look thru that; examples are always helpful.

    Anyway, may someone tell me what to do on this?
    the error I get is
    Code:
    ...: error CS0103: The name 'DataTable1' does not exist in the current context
    makes sense, due to the fact that those are different classes. Is there a way to declare them as "friendly class" or "friend assembly" or something like that?
    Any hints?
    Last edited by Wild Thing; November 9th, 2010 at 02:04 PM.
    prepare that the light at the end of your tunnel is just a freight train coming your way - metallica

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Accessing DataTables from Windows Form

    A class is a template for an object. Unless the class contains static methods that you want to use you need to create an instance of the class within the scope of your form class. You should find a good beginner's tutorial that explains the basics of how classes and objects work.

  5. #5
    Join Date
    Feb 2003
    Location
    Germany
    Posts
    87

    Re: Accessing DataTables from Windows Form

    OK, BigEd, got me on that. I admit I'm with stupid. Got it running now.

    I Love this forum; you always get help.

    A word to the youngsters:
    Books are those paper things that people used before Google. They were helpful, also their search capabilities were very limited. And don't mind if you get a tap on the head or in the neck by the elders time to time, keeps you down to earth and reminds you that you're not immortal - like in the old days.
    prepare that the light at the end of your tunnel is just a freight train coming your way - metallica

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center