none
How to Access RRS feed

  • Question

  • HI
     

    I think this is easiest question for anyone ,but it difficuilt to me.Below I mention my coidng ,what i want to do is access the  myBindingSource from the CmbStuID_SelectedIndexChanged() method SO i dont know how to access the myBindingSource.
    coz I think myBindingSource is limited to Form_Load method.How  can I access the myBindingSource with in CmbStuID_SelectedIndexChanged

    Thank you


    Code Snippet

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

            private void Form1_Load(object sender, EventArgs e)
            {
                // TODO: This line of code loads data into the 'schoolDataSet.ClassRoom' table. You can move, or remove it, as needed.
                this.classRoomTableAdapter.Fill(this.schoolDataSet.ClassRoom);
                BindingSource myBindingSource = new BindingSource();
                SqlConnection conn = new SqlConnection("Data Source=TOM\\SQLEXPRESS;Initial Catalog=School;Integrated Security=True");
                SqlCommand command = new SqlCommand("Select * from ClassRoom", conn);
                try
                {
                    conn.Open();
                     SqlDataAdapter data = new SqlDataAdapter(command);
                    DataSet mydata = new DataSet();
                    data.Fill(mydata);
                   
                    myBindingSource.DataSource = mydata;
                    myBindingSource.DataMember = mydata.Tables[0].TableName;
                    mygridview.DataSource = myBindingSource;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            private void CmbStuID_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (CmbStuID.Text == "2")
                {             
                  myBindingSource.    
                }
            }    }
    }





    Thursday, March 20, 2008 11:24 AM

Answers

  • You can't access myBindingSource variable in CmbStuID_SelectedIndexChanged because myBindingSource is a local variable declared in Form1_Load event. If you want to make it accessible in whole class, then declare it as a member of Form1 class.


    Code Snippet

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


       BindingSource myBindingSource = new BindingSource();

           

       private void Form1_Load(object sender, EventArgs e)

            {
                // TODO: This line of code loads data into the 'schoolDataSet.ClassRoom' table. You can move, or remove it, as needed.
                this.classRoomTableAdapter.Fill(this.schoolDataSet.ClassRoom);
                SqlConnection conn = new SqlConnection("Data Source=TOM\\SQLEXPRESS;Initial Catalog=School;Integrated Security=True");
                SqlCommand command = new SqlCommand("Select * from ClassRoom", conn);
                try
                {
                    conn.Open();
                     SqlDataAdapter data = new SqlDataAdapter(command);
                    DataSet mydata = new DataSet();
                    data.Fill(mydata);
                   
                    myBindingSource.DataSource = mydata;
                    myBindingSource.DataMember = mydata.Tables[0].TableName;
                    mygridview.DataSource = myBindingSource;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            private void CmbStuID_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (CmbStuID.Text == "2")
                {             
                  //You can now access myBindingSource.    
                }
            }    }
    }



    Thursday, March 20, 2008 7:29 PM

All replies

  • You can't access myBindingSource variable in CmbStuID_SelectedIndexChanged because myBindingSource is a local variable declared in Form1_Load event. If you want to make it accessible in whole class, then declare it as a member of Form1 class.


    Code Snippet

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


       BindingSource myBindingSource = new BindingSource();

           

       private void Form1_Load(object sender, EventArgs e)

            {
                // TODO: This line of code loads data into the 'schoolDataSet.ClassRoom' table. You can move, or remove it, as needed.
                this.classRoomTableAdapter.Fill(this.schoolDataSet.ClassRoom);
                SqlConnection conn = new SqlConnection("Data Source=TOM\\SQLEXPRESS;Initial Catalog=School;Integrated Security=True");
                SqlCommand command = new SqlCommand("Select * from ClassRoom", conn);
                try
                {
                    conn.Open();
                     SqlDataAdapter data = new SqlDataAdapter(command);
                    DataSet mydata = new DataSet();
                    data.Fill(mydata);
                   
                    myBindingSource.DataSource = mydata;
                    myBindingSource.DataMember = mydata.Tables[0].TableName;
                    mygridview.DataSource = myBindingSource;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            private void CmbStuID_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (CmbStuID.Text == "2")
                {             
                  //You can now access myBindingSource.    
                }
            }    }
    }



    Thursday, March 20, 2008 7:29 PM

  • HI


    Thank You Giorgi Dalakishvili.


    Bye

    Friday, March 21, 2008 1:23 AM
  • You are welcome Smile
    Friday, March 21, 2008 7:25 AM