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 = [B]dc[/B][1].ToString();
                        Global.GlobalVar3 = [B]dc[/B][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.

Recommended Answers

All 5 Replies

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!;)

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;}
        }
        

    }

   
}

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).

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();
            }
        }

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.