Welcome to Dream.In.Code
Become a C# Expert!

Join 150,368 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,761 people online right now. Registration is fast and FREE... Join Now!




Using variables from a previous form

 
Reply to this topicStart new topic

Using variables from a previous form

Premier2k
29 Aug, 2008 - 10:38 AM
Post #1

New D.I.C Head
*

Joined: 26 Aug, 2008
Posts: 22


My Contributions
Hi all,

I am trying to use a variable that is declared on a separate form.
I have been playing around with it for a few days and eventually discovered I have to make it public, so I changed my code slightly but I'm still having trouble getting it to work. Code below;

The line in the code is the public static one. I have posted a portion of the surrounding code to show it's position within the structure.

CODE

public Form1()
        {
            InitializeComponent();

        }

        System.Data.SqlClient.SqlConnection con;
        System.Data.SqlClient.SqlDataAdapter da;
        public static DataSet ds1 = new DataSet();
        int MaxRows = 0;
        int inc = 0;

        private void Form1_Load(object sender, EventArgs e)
        {



And this is the code on my other form that uses this

CODE

public void btnSave_Click(object sender, EventArgs e)
        {
            DataRow dRow = ds1.Tables["Workers"].NewRow();

            dRow[1] = textBox1.Text;
            dRow[2] = textBox2.Text;
            dRow[3] = textBox3.Text;

            ds1.Tables["Workers"].Rows.Add(dRow);

            
        }


The error I'm getting is:

Error 1 The name 'ds1' does not exist in the current context

What am I doing wrong? Shouldn't this work?
I just can't seem to figure out what I'm doing wrong

Thanks all,

Premier2k

This post has been edited by Premier2k: 29 Aug, 2008 - 10:39 AM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Using Variables From A Previous Form
29 Aug, 2008 - 11:25 AM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,327



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
You need to specify on which form the object is located.
CODE

DataRow dRow = Form1.ds1.Tables["Workers"].NewRow();


Form1.ds1.Tables["Workers"].Rows.Add(dRow);

User is offlineProfile CardPM
+Quote Post

Premier2k
RE: Using Variables From A Previous Form
29 Aug, 2008 - 11:34 AM
Post #3

New D.I.C Head
*

Joined: 26 Aug, 2008
Posts: 22


My Contributions
QUOTE(jayman9 @ 29 Aug, 2008 - 12:25 PM) *

You need to specify on which form the object is located.
CODE

DataRow dRow = Form1.ds1.Tables["Workers"].NewRow();


Form1.ds1.Tables["Workers"].Rows.Add(dRow);



Of course!

How did I miss that!?


Thanks Jayman!

Premier2k
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:09PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month