0

I'm trying to Get a Value From the variable I Diclear in a private Method and I want to use that value in another private method

First I want to add a picture in the Picture box I create this Button after using FileDialog for browsing pictures I saved the file path in a string named PicPath.

private void Add_Pic_bt_Click(object sender, EventArgs e)
    {
        // open file dialog   
        OpenFileDialog open = new OpenFileDialog();
        // image filters  
        open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp; *.png)|*.jpg; *.jpeg; *.gif; *.bmp; *.png";
        if (open.ShowDialog() == DialogResult.OK)
        {
            // display image in picture box  
            Std_Pic.Image = new Bitmap(open.FileName);
            // image file path  
            string Picpath = open.FileName;
        }
    }

what I want is to call that variable another button so I can add the file path to the database.

  private void Save_Info_bt_Click(object sender, EventArgs e)
    {
        string DateOfBirth = Std_Year_tx.Text + "/" + Std_Month_tx.Text + "/" + Std_Day_tx.Text;
        OleDbDataAdapter InfoSave = new OleDbDataAdapter(" INSERT INTO  [StudentInfo] (Name, Student_ID, Departmint, Stage, Date_Of_Birth, Email_Address, Phone_Number, Profile_Picture ) values('" + Std_Name_tx.Text + "', '" + Std_ID_tx.Text + "','" + Std_Dep_cb.Text + "', '" + Std_Stage_cb.Text + "', '" + DateOfBirth + "', '" + Std_Email_tx.Text + "', '" + Std_Num_tx.Text + "', '"+ Picpath + "')", Mycon) ;
       DataSet DataSave = new DataSet();
        InfoSave.Fill(DataSave);
    }

NOTE: I'm new to C# im trying to learn by making this kind of stuff So if u can explain in a simple way And thank you for your time.

Software: Visual Studio 2019. Database: MS Access 2019.

  • 1
    Are these methods of your class? If so, create a private _field_ or _property_ of your class and store it there. The variable `picpath` will be gone as soon as its containing scope disappears (i.e., on the next line). If you put it in a class level field or property it will live as long as the class instance. However, you need to read up on **SQL Injection**, you have very insecure code. Look here, for example: https://bobby-tables.com/ – Flydog57 Aug 13 '21 at 18:44
  • All these methods are in the `namespace StudentInfo { public partial class StdInfoFrm : Form { ... ` Do u mean I have to declare Picpath in the main class? And Thank you for pointing for the SQL Injection for now I'm just trying to get used to C# and working with the database @Flydog57 – Ammar Talal Aug 13 '21 at 18:52
  • Yes, declare Picpath as `public string Picpath { get; set; }` within your (partial) `class StdInfoFrm` declaration. You could probably declare it as `private` as well, if your methods are the only things using it. – Flydog57 Aug 13 '21 at 19:07

1 Answers1

-2

you can change the private Method to public example

        public int x;
        public void Main()
        {
            x = 1
        }

and then call it from another method

Titoot
  • 120
  • 6
  • 1
    That's not really an answer to the OP's question (unless I'm reading it wrong). What you show is a class not a private or public _Method_. And, you've make `x` a static variable, which I'm guessing the OP doesn't really want to do – Flydog57 Aug 13 '21 at 18:51
  • Sorry but I'm Not working with classes I'm having a problem getting data from a private method I don't think they work the same. – Ammar Talal Aug 13 '21 at 19:01
  • sorry i was lazy to type it for method instead of a class but i think this should work – Titoot Aug 13 '21 at 21:24