Employers - Post Free Jobs
Forum guidelines
AuthorQuestion
call the main fuction
Posted on: 12 Dec 2012
Hi;
I want to call the Main function of class Program, and pass a sql string to the Main fuction,
from the function of btn1Search_Click of the class Form1, when i click a button on the main form.

i am writing a project for database, and all the data put into the dataGridView. and i want the window displays another data when i click a button.

Could anyone tell me how to do that, please.

///////////////////////////////
  static class Program

  {
  public static string sql; 
  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main(string[] args)
  {
  if (args.Length < 1)
  sql = "select buyerName, id, category, productName, price, date, referenceNo from elena ";
  else
  sql = args[0];
 
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  Application.Run(new Form1(sql));
 
  }
  }
/////////////////////////////
  public partial class Form1 : Form
  {
  ........
  private void btn1Search_Click(object sender, EventArgs e)
  {

  }
  ...........

  }

AuthorReply
Walter Kiess
  • 1
  • 0
accepted
Re: call the main fuction
Posted on: 12 Dec 2012   Accepted Answer
As you've not posted code for From1 I will assume that the sql parameter is being used by Form1 to call a method which reads the database and updates your datagridview. All you have to do is is call that method again from the search button click event and the grid will refresh. If you are not calling a method to do the work (in Form1), I suggest you change the code so that you are. This will also make your code more legible.
Re: call the main fuction
Posted on: 12 Dec 2012  
Is this a trick question?

Try this (aircode):

private void btn1Search_Click(object sender, EventArgs e)

  {
        string sql = "...";
        Main(sql);
  }

You may need to redefine Main as:

private void Main(string sql)
{...}

kam
  • 0
  • 0
Re: call the main fuction
Posted on: 12 Dec 2012  
In the Class Program, i define a method

  private void reload(string sql)
  {

  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  Application.Run(new Form1(sql));
  }


and in the Form1


  private void btn1Search_Click(object sender, EventArgs e)
  {
  Program.reload("select buyerName, id, category, productName, price, date, referenceNo from elena ");
  }

But the compiler tell me
Error 1 'elena.Program.reload(string)' is inaccessible due to its protection level E:\C#\elena\elena\Form1.cs 62 21 elena


kam
  • 0
  • 0
Re: call the main fuction
Posted on: 12 Dec 2012  
Thanks for your advice
kam
  • 0
  • 0
Re: call the main fuction
Posted on: 13 Dec 2012  
In the Form1, i write the code for the button of the btn1Search_Click, but when i click
the button, the window still show the same data?????

//////////////////////

   public partial class Form1 : Form
    {

        public static String str;

        public Form1(String myStr)
        {
            InitializeComponent();
            str = myStr;
           
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            dataGridView1.DataSource = Infor.GetDataSet(str);
            dataGridView1.DataMember = "elena";
           

        }

        private void save_Click(object sender, EventArgs e)
        {
            Infor.UpdateDataSet((DataSet)dataGridView1.DataSource);

        }


        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex >= 0)
            {
                if (dataGridView1.Columns[e.ColumnIndex].Name == "Column8")
                {
                    dataGridView1.Rows.RemoveAt(e.RowIndex);
                }
            }
        }

        private void btn1Search_Click(object sender, EventArgs e)
        {
            this.Hide();
            str = "select buyerName, id, category, productName, price, date, referenceNo from elena where referenceNo='d11'";
            Form1 f1 = new Form1(str);
            this.Show();
        }


    }



Re: call the main fuction
Posted on: 13 Dec 2012  
I wouldn't invoke Form1 again. You're already in it. Just execute the Load event again. See below:

private void btn1Search_Click(object sender, EventArgs e)
        {
            this.Hide();
            str = "select buyerName, id, category, productName, price, date, referenceNo from elena where referenceNo='d11'";
            Form1_Load(sender, e);
        }

You may need to clear the grid first though. not sure...

kam
  • 0
  • 0
Re: call the main fuction
Posted on: 13 Dec 2012  
I find out the solution already.

i create a class called loader

        public Loader(string sql)
        {
            Form1 f1 = new Form1(sql);
            f1.Show();
        }

and in the Form1,

        private void btn1Search_Click(object sender, EventArgs e)
        {
            this.Hide();
            str = "select buyerName, id, category, productName, price, date, referenceNo from elena where referenceNo='d11'";
            Loader L = new Loader(str);
           
        }

Thanks a lot!!!!!!!!!!!

Re: call the main fuction
Posted on: 13 Dec 2012  
No problem. Happy to help. But your solution will eventually run out of stack space due to the recursive nature of your call.
Employers - Post Free Jobs

SPONSORED BY

Offshore Software and Database Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.