10 Replies - 5125 Views - Last Post: 06 September 2012 - 10:54 AM Rate Topic: -----

#1 Charlie20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-September 12

name does not exist in the current context error

Posted 05 September 2012 - 01:10 PM

Hi all,

I'm new to C# and am bumping against an error that I hope someone can assist with.

In my winforms application I have a button that allows the user to select a flat file and the data is imported into a datatable & displayed in a datagridview. No problems with all that :bigsmile:

I have a second button on the form where I want to copy certain colums from datatable1 into a new datatable. To do this I am trying to use the following code :-
DataTable uniqueCols = dt1DefaultView.ToTable(true, "dt1column1");


When I try & run this I get an erros stating that
The name 'dt1' does not exist in the current context

I am assuming this is because dt1 is "setup" in the code block for the first button. Am I correct in assuming this & if so how do I "fix" the issue.

Any pointers would be welcomed.

Thanks all

Is This A Good Question/Topic? 0
  • +

Replies To: name does not exist in the current context error

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8365
  • View blog
  • Posts: 31,080
  • Joined: 12-June 08

Re: name does not exist in the current context error

Posted 05 September 2012 - 01:15 PM

Quote

I am assuming this is because dt1 is "setup" in the code block for the first button. Am I correct in assuming this & if so how do I "fix" the issue.

What do you mean? Show us the code from there..
Was This Post Helpful? 0
  • +
  • -

#3 Charlie20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-September 12

Re: name does not exist in the current context error

Posted 05 September 2012 - 01:54 PM

namespace Import
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static string SelectedTable = string.Empty;

        // Select & open file
        private void button1_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "Select a File to import";
            fdlg.InitialDirectory = @"c:\";
            fdlg.FileName = txtFileName.Text;
            fdlg.Filter = "Comma Seperated Values Files|*.csv";
            fdlg.FilterIndex = 1;
            fdlg.RestoreDirectory = true;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                txtFileName.Text = fdlg.FileName;
                ImportRaw();
                Application.DoEvents();
            }
        }

        public void ImportRaw()
        {
            if (txtFileName.Text.Trim() != string.Empty)
            {
                try
                {
                    DataTable dtRaw = GetDataTableRaw(txtFileName.Text);
                    dataGridView1_Original.DataSource = dtRaw.DefaultView;

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }

        public static DataTable GetDataTableRaw(string strFileName)
        {
            ADODB.Connection oConn = new ADODB.Connection();
            oConn.Open("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(strFileName) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\";", "", "", 0);
            string strQuery = "SELECT * FROM [" + System.IO.Path.GetFileName(strFileName) + "]";
            ADODB.Recordset rs = new ADODB.Recordset();
            System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter();
            DataTable dtRaw = new DataTable();
            rs.Open(strQuery, "Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(strFileName) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\";",
                ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly, 1);
            adapter.Fill(dtRaw, rs);
            return dtRaw;
            
        } 

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
             DataTable uniqueCols = dtRaw.DefaultView.ToTable(true, "dgcolumn1");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

        }

        // Exit button
        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}



Line 66 is where the error message appears.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3160
  • View blog
  • Posts: 9,532
  • Joined: 05-May 12

Re: name does not exist in the current context error

Posted 05 September 2012 - 02:05 PM

dtRaw is declared to be a local variable on line 36 within ImportRaw(). If you want the variable to be shared with button2_Click(), you should declare the variable as a member variable.
Was This Post Helpful? 1
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3160
  • View blog
  • Posts: 9,532
  • Joined: 05-May 12

Re: name does not exist in the current context error

Posted 05 September 2012 - 02:12 PM

A couple notes about your code:

Do not use Application.DoEvents() like you do on line 26. This leads to re-entrancy issues. Unless you write you code expecting to be re-entrant, do not do this. It is an evil VB hack that VB programmers put in when their programs were "non-responsive" and they wanted the WM_PAINT messages to come through and repaint their windows. Most of them did not realize that by running the message pump they made their programs re-entrant because not only will the paint messages come back in, but so will button clicks, window closes, etc.

Take time to rename your controls. It only takes a couple seconds to rename button1, or button2 to btnImport and btnUnique. Do it while you are creating the buttons on screen and then enjoy the benefits of having meaningful control names for rest of your project lifetime.
Was This Post Helpful? 1
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3160
  • View blog
  • Posts: 9,532
  • Joined: 05-May 12

Re: name does not exist in the current context error

Posted 05 September 2012 - 02:20 PM

Use the built in Oledb provider. It looks like you imported the MDAC type library and now have the ADODB.Connection and it's ilk. You can use the built in Oledb provider: http://msdn.microsof...090.aspx#_OleDb . It looks like you are already using part of it on line 53.

This post has been edited by Skydiver: 05 September 2012 - 02:25 PM

Was This Post Helpful? 1
  • +
  • -

#7 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: name does not exist in the current context error

Posted 05 September 2012 - 03:02 PM

View PostSkydiver, on 05 September 2012 - 03:05 PM, said:

dtRaw is declared to be a local variable on line 36 within ImportRaw(). If you want the variable to be shared with button2_Click(), you should declare the variable as a member variable.


Expanding on that...


Its what we call "scope". A thing exists between the {} it was created in. So if you make a thing within a for{} loop, that's its scope. If you want to access a thing for the entire class, then define it in the class braces

class someClass
{
   int yogi = 5; // Accessible to the class
   
   void someMethod
   {
        int booboo = 3; // Accessible to the method

        while(true)
        {
           string Ranger = "Smith"; // Accessible to the loop
        }
   }
}

Was This Post Helpful? 1
  • +
  • -

#8 Charlie20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-September 12

Re: name does not exist in the current context error

Posted 06 September 2012 - 08:42 AM

Thanks for the help & the pointers - much appreciated.

If I do the following :-
    public partial class Form1 : Form
    {
        public DataTable dtRaw = new DataTable();
        public Form1()
        {
            InitializeComponent();
        }



I no longer get the error, however........using the code block I provided previously........if I use the dataset visualiser on dtRaw line 36 I can see the data that's been imported as I expect. If I use the dataset visualiser on dtRaw line 66 the visualiser is empty.

As I imported the data in the scope for button1 does that mean the data is only available within that scope? If so how could would one allow the data to be shared with other scopes (if that's the term?) & could the data be passed to another form?

Nothing I've found on google or the books I've bought seems to help.

Thanks all.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3160
  • View blog
  • Posts: 9,532
  • Joined: 05-May 12

Re: name does not exist in the current context error

Posted 06 September 2012 - 08:50 AM

Show us your complete updated code.

I suspect that you line 36 still looks like this:
DataTable dtRaw = GetDataTableRaw(txtFileName.Text);


which is declaring a variable named dtRaw that is only local to that scope. You are not actually updating the member variable that you just added in post #8.

It should look like this:
dtRaw = GetDataTableRaw(txtFileName.Text);


so that you'll be setting the member variable.

I think you need to review your the book you are using and re-read the section about "scope" and "scoping".
Was This Post Helpful? 1
  • +
  • -

#10 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 256
  • View blog
  • Posts: 529
  • Joined: 07-July 10

Re: name does not exist in the current context error

Posted 06 September 2012 - 08:58 AM

What does line 36 look like now? I'm guessing they still look like this.
DataTable dtRaw = GetDataTableRaw(txtFileName.Text);

This doesn't do what you may think it does. "DataTable dtRaw" creates a new DataTable called "dtRaw". This is a different object than any others called "dtRaw". Just because a person called "George" was the first president does it mean that all other people called "George" are presidents. Doing something to a new George doesn't effect the old George. Line 36 should look like this.
dtRaw = GetDataTableRaw(txtFileName.Text);

If this is not the problem, we will probably need to see your new code.


Second point: You declared dtRaw as a Public variable. Do you want any code outside your Form1 class to change your DataTable? I'm going to assume the answer is no. Therefore, you should change it to a Private variable. Always* make your variable private. Only change it to a less secure protection if you need it less secure.

*There are no hard and fast rules. There is probably an exception to "always".
Was This Post Helpful? 1
  • +
  • -

#11 Charlie20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-September 12

Re: name does not exist in the current context error

Posted 06 September 2012 - 10:54 AM

Thanks everyone - it's all working as needed.

Appreciate that you put up with my newbieness :stupid: :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1