Link to home
Start Free TrialLog in
Avatar of ANINDYA
ANINDYAFlag for India

asked on

C# Properties value passing to another form

Experts
I have a question .
Please see the screen shots and the code and you will be able to know what is my problem and what I want to do.
Please see both the screen shots.
Thanking you,
This is the code of the form where the Plus sign is there and also the MemberId textbox is also there.That is Form1.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace CDShop
{
    public partial class Form_BorrowSection : Form
    {
        public Form_BorrowSection()
        {
            InitializeComponent();
        }
        Int32 memberid { get; set; }
        public void getMemberid()
        {
            Int32 id;
            id = Convert.ToInt32(textBox_MemberID.Text);
            memberid = id;
        }


Here is the code of the Form2 .

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace CDShop
{
    public partial class Form_Borrowed_Items : Form
    {
        public Form_Borrowed_Items()
        {
            InitializeComponent();
        }

        private void Form_Borrowed_Items_Load(object sender, EventArgs e)
        {
            label1.Text = fb.memberid;//fb is the Form1 object


            string b = "Borrowed";
            string query = "select * from Table_Borrow where MemberID=@parameter1 and Status LIKE '" + b.ToString() + "' ";
            using (SqlConnection con = new SqlConnection("Data Source=INVENTOR-1D70F6\\SQLEXPRESS;Initial Catalog=AnindyaCD;Integrated Security=SSPI"))
            {
                using (SqlCommand cmd = new SqlCommand(query, con))
                {
                    cmd.Parameters.AddWithValue("@parameter1", MemberID);

                    using (SqlDataAdapter ad = new SqlDataAdapter(query, con))
                    {
                        ad.SelectCommand = cmd;
                        DataSet ds = new DataSet();
                        ad.Fill(ds, "Table_Borrow");
                        dataGridView5.DataSource = ds.Tables[0];
                    }
                }
            }
        }
    }
}

Open in new window

error.JPG
error.JPG
Avatar of kris_per
kris_per


When clicking the plus, get the member id textbox value and pass to the second form through the constructor or through a property...

class Form1
{

on_plus_clicked_handler(...)
{

string memberId = textBoxMemberId.Text

Form2 form2 = new Form2(memberId);
//show form2

OR

Form2 forms = new Form2();
form2.MemberID = memberID;

}

}





class Form2
{

private string _memberId = string.Empty;


public string MemberID
{
get {return _memberId;}
set { _memberId = value; }
}

public Form2(string memId)
{

_memberId  = memId; // use _memberId later

}



}
ASKER CERTIFIED SOLUTION
Avatar of kris_per
kris_per

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ANINDYA

ASKER

Expert kris_per
Thanks for your timely answer.
Also it is so vivid that too easy to understand.
I am extremely thankful to you.
Thanks for the total code also.
Thanking you,
Anindya Chatterjee
Bangalore
India
 

ANINDYA,
You are welcome and Thanks.