We help IT Professionals succeed at work.

C# Properties value passing to another form

ANINDYA
ANINDYA asked
on
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
Comment
Watch Question

Top Expert 2010

Commented:

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

}



}
Top Expert 2010
Commented:

Filling that in your code, it would look like:


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

private plusControl_Clicked(...)
{
       string memberId = textBoxMemberId.Text

       Form_Borrowed_Items form2 = new Form_Borrowed_Items(memberId);

       OR

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

       //show form2 - Form_Borrowed_Items_Load will be called when showing

}

Here is the code of the Form2 .

public partial class Form_Borrowed_Items : Form
{
       private string _memberId = string.Empty;

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

        public Form_Borrowed_Items(string memId)
        {
            InitializeComponent();
	_memberId  = memId; // use _memberId later

        }

        private void Form_Borrowed_Items_Load(object sender, EventArgs e)
       {
            // _memberId value is available here; use it in your query
       }

Open in new window

Author

Commented:
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
 
Top Expert 2010

Commented:

ANINDYA,
You are welcome and Thanks.