954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help with error

i get an error when i try and do this.

private void button2_Click(object sender, EventArgs e)
        {

            DataSet ds = new DataSet();
            DBconnect db = new DBconnect();

            Global.GlobalVar = textBox1.Text;

            string Co = new Password('J', textBox1.Text).SetSql();

            ds = db.GetDataSet(Co);

            DataTable dt = ds.Tables[0];

            if (dt.Rows.Count == 0)
            {
                if (textBox1.Text != "")
                {
                    MessageBox.Show("Password Does Not Exist");
                    textBox1.Text = "";
                }
                else
                {
                    this.Close();   
                }
            }
            else
            {
                Co = new Password('K', "").SetSql();
                ds = db.GetDataSet(Co);
                DataRow dr = dt.Rows[0];
                DataColumn dc = dt.Columns[0];
                
                foreach (DataRow row in dt.Rows)
                {
                    foreach (DataColumn column in dr.Table.Columns)
                    {
                        Global.GlobalVar2 = <strong>dc</strong>[1].ToString();
                        Global.GlobalVar3 = <strong>dc</strong>[1].ToString();
                    }
                }
                this.Close();
            }
        }


Where it says "Global.GlobalVar 2-3" the error states.
"System.Data.DataColumn.this[int] is inaccessible due to it's protection level"

I'm not sure what to do about it.

NH1
Light Poster
43 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

Has probably to do with public or private access modifiers.
Check your Global class.
Just a remark on the side: It is for several reasons, a bad idea to maintain a class with globals...
But please feel free to do whatever you want. At least I'm still rocking in a free world!;)

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 
Has probably to do with public or private access modifiers. Check your Global class. Just a remark on the side: It is for several reasons, a bad idea to maintain a class with globals... But please feel free to do whatever you want. At least I'm still rocking in a free world!;)

everything in my "Global" class is public
ill show you how my "Global class is set up.

namespace WindowsFormsApplication3
{
    static class Global
    {

        public static string Pass = "";
        public static string Pass2 = "";
        public static string Pass3 = "";
        public static string Pass4 = "";
        public static string Pass5 = "";
        public static string Pass6 = "";
        

        public static string GlobalVar
        {

          get{ return Pass; }
          set{ Pass = value;}
        }

        public static string GlobalVar2
        {

          get{ return Pass2; }
          set{ Pass2 = value;}
        }

        public static string GlobalVar3
        {

          get{ return Pass3; }
          set{ Pass3 = value;}
        }

        public static string GlobalVar4
        {

          get{ return Pass4; }
          set{ Pass4 = value;}
        }

        public static string GlobalVar5
        {

          get{ return Pass5; }
          set{ Pass5 = value;}
        }

        public static string GlobalVar6
        {

          get{ return Pass6; }
          set{ Pass6 = value;}
        }
        

    }

   
}
NH1
Light Poster
43 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

Your issue seems to be related to the DataColumn and not the Global from what I'm reading of the error. Your error is contained within the following segment ... dc[1].ToString(); ... It reads to me that you are saying the following:

// Create DataSet ds from db.GetDataSet(Co)
ds = db.GetDataSet(Co);
// Create DataTable dt from the first table of ds
DataTable dt = ds.Tables[0];
            else
            {
                // Change the definition of Co
                Co = new Password('K', "").SetSql();
                // Re-create the dataset ds with new definition
                ds = db.GetDataSet(Co);
                // Create DataRow dr from dt which has not been reinitialized since
                // it's original values were pulled
                DataRow dr = dt.Rows[0];
                // Create DataColumn dc from dt which has not been reinitialized since
                // it's original values were pulled
                DataColumn dc = dt.Columns[0];
                
                // Cycle through the DataRows (you've only made one DataRow definition
                // which is the equivalent of row 0 of your table) within all rows of
                // the datatable
                foreach (DataRow row in dt.Rows)
                {
                    // Cycle through the DataColumns (you've only made one DataColumn
                    // which is the equivalent of column 0 of your table) for the table
                    // based on the dr DataRow
                    foreach (DataColumn column in dr.Table.Columns)
                    {
                        // Change GlobalVar2 to equal array position "1" of dc (which =
                        // dt.Columns[0])
                        Global.GlobalVar2 = dc[1].ToString();
                        // Change GlobalVar3 to equal array position "1" of dc (which =
                        // dt.Columns[0])
                        Global.GlobalVar3 = dc[1].ToString();
                    }
                }
                this.Close();
            }
        }


To be quite honest... between the DataRow and DataColumn definitions and loopings I'm not entirely sure what you're looking to do here. What I will say, however, is that it is very possible depending on how your table was set up that your attempt at cycling through datacolumns within datarows may be performing an 'illegal' operation with data that is not, technically, accessible due to the way one or the other works (particularly in tandem with each other).

Lusiphur
Posting Shark
Team Colleague
966 posts since Jun 2010
Reputation Points: 207
Solved Threads: 127
 

I fixed the problem on my own. I dont know why this worked all of a sudden, it didnt seem to work before.

but this is what i did.

private void button2_Click(object sender, EventArgs e)
        {

            DataSet ds = new DataSet();
            DBconnect db = new DBconnect();

            Global.GlobalVar = textBox1.Text;

            string Co = new Password('J', textBox1.Text).SetSql();

            ds = db.GetDataSet(Co);

            DataTable dt = ds.Tables[0];

            if (dt.Rows.Count == 0)
            {
                if (textBox1.Text != "")
                {
                    MessageBox.Show("Password Does Not Exist");
                    textBox1.Text = "";
                }
                else
                {
                    this.Close();
                }
            }
            else
            {
                Co = new Password('K', "").SetSql();
                ds = db.GetDataSet(Co);
                DataRow dr = dt.Rows[0];
               // DataColumn dc = dt.Columns[0];


                Global.GlobalVar2 = dr[3].ToString();
                Global.GlobalVar3 = dr[4].ToString();
                Global.GlobalVar4 = dr[5].ToString();
                Global.GlobalVar5 = dr[6].ToString();
                Global.GlobalVar6 = dr[2].ToString();

                this.Close();
            }
        }
NH1
Light Poster
43 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

See, now that makes much more sense to me than the looping that you were attempting before :) Glad it worked out.

Don't forget to mark your thread solved now that you've resolved your issue.

Lusiphur
Posting Shark
Team Colleague
966 posts since Jun 2010
Reputation Points: 207
Solved Threads: 127
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: