Click here to Skip to main content
12,243,333 members (48,560 online)
Click here to Skip to main content
Add your own
alternative version

Stats

56.8K views
1.6K downloads
35 bookmarked
Posted

Pass Data from One Form to Another Form

, 25 Mar 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
How to transfer data from one form to another form

Introduction

For some applications, it is sometimes common to have parent and child forms, where a user is guided to the child form to input something and next the changes that he has made are visible
in the parent form without closing the child form.

If the program doesn't allow this, the only solution is closing the child form programmatically and returning the user to the parent form.

Suppose we have a program where the user will input 1000 customer names at the end of the day. Would it be efficient and user friendly to return the user to the parent form after he inputs each and every customer name.

The answer is no!

This is because the user will have to open the child form 1000 times to input 1000 customer names.

Instead of opening the child form multiple times, there is a way that I know which passes data from the child form to the parent form without closing the child form. The changes that the user has made are visible at the back of the child form, which is in the parent form.

Well, this simple application passes data from one form to another form. Many programmers face this kind of problem when the necessity of passing data from one form to another form occurs.

I have seen many articles where many experienced programmers wrote about data passing, but this one is the simplest one to learn (I hope Smile | :) ).

First of all, you have to create a database. I created a database and named it CustomerInfo.

Then add a Table named Customer, and a field to it named CustomerName(Varchar(50)).

DatabaseScript

Using the Code

Create two forms frmCustomers and frmCustomer. One should hold all the Customers, and another should simply save the customer like the following:

DatabaseScript

The following is for the frmCustomers form which shows all the customers in the Datagrid view list:

//This code is for the frmCustomers Form            
private void frmCustomers_Load(object sender, EventArgs e)
{
    //load all customers
    loadCustomer();
}        

public void loadCustomer()
{
    //load all customers

    SqlConnection con = new SqlConnection("Data Source=localhost; 
	Database=CustomerInfo; UID=achakraborty; Integrated Security = True;");
    con.Open();

    SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Customer", con);
					//SQL query
					
	DataSet ds = new DataSet("Customer");
    da.Fill(ds, "Customer");

//Fill the dataset
    grd.DataSource = ds.Tables["Customer"];
//set the grid source to customer data table
}

private void btnAdd_Click(object sender, EventArgs e)
{
    //During this operation, bind the Customer form event with a method 
    //which will fire from Customer form

    frmCustomer c = new frmCustomer();
    c.CustomerEventHandler += new EventHandler(RefreshCustomerList);
                        //This is the method which will fire upon any change
    c.ShowDialog();
}

void RefreshCustomerList(object sender, EventArgs e)
{
    //load all customers
    SqlConnection con = new SqlConnection("Data Source=localhost; 
	Database=CustomerInfo; UID=achakraborty; Integrated Security = True;");
    con.Open();
    SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Customer", con);
    DataSet ds = new DataSet("Customer");
    da.Fill(ds, "Customer");
    grd.DataSource = ds.Tables["Customer"];
    con.Close();
}

private void btnExit_Click(object sender, EventArgs e)
{
    this.Close();
}                 

//The following code is for frmCustomer form
public event System.EventHandler CustomerEventHandler;
private void btnSave_Click(object sender, EventArgs e)
{
    //First of all open a database connection
    SqlConnection con = new SqlConnection("Data Source=localhost; 
	Database=CustomerInfo; UID=achakraborty; Integrated Security = True;");
    con.Open();

    //Execute Command object to insert data in the corresponding customer table
    SqlCommand cmd = new SqlCommand("Insert Into Customer VALUES
		('" + txtCustomerName.Text.ToString().Trim() + "')", con); 
    cmd.ExecuteNonQuery();
    con.Close();

    if (CustomerEventHandler != null)
    { 
        CustomerEventHandler(sender, e);

	//firing if any change made 
    }

    txtCustomerName.Clear();
    txtCustomerName.Focus();
}

I'm happy to present my first article on The Code Project. I'm hoping to use the experience to improve my writing style. So your comments, suggestions and criticism are very welcome.

History

  • 26th March, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Angsuman Chakraborty
Software Developer NA
Bangladesh Bangladesh
Graduated with IT in 2006.

Currently working as a software engineer.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 3 Pin
Meysampaknahad9-Jan-12 5:21
memberMeysampaknahad9-Jan-12 5:21 
Generalsame code Pin
antomic0712-Jun-11 23:25
memberantomic0712-Jun-11 23:25 
i want this code in vb.net winform
GeneralRe: same code Pin
Angsuman Chakraborty13-Jun-11 1:15
memberAngsuman Chakraborty13-Jun-11 1:15 
GeneralRe: same code Pin
antomic0715-Jun-11 23:20
memberantomic0715-Jun-11 23:20 
GeneralRe: same code Pin
Angsuman Chakraborty15-Jun-11 23:53
memberAngsuman Chakraborty15-Jun-11 23:53 
GeneralRe: same code Pin
antomic0716-Jun-11 3:41
memberantomic0716-Jun-11 3:41 
GeneralRe: same code Pin
antomic0715-Jun-11 23:24
memberantomic0715-Jun-11 23:24 
AnswerRe: same code Pin
thatraja26-Jan-12 21:55
mvpthatraja26-Jan-12 21:55 
GeneralCalling Methods on Parent Form (MDI) Pin
AllanMunro6-Dec-09 23:47
memberAllanMunro6-Dec-09 23:47 
GeneralMy vote of 1 Pin
ramuknavap1-Apr-09 16:14
memberramuknavap1-Apr-09 16:14 
GeneralRe: My vote of 1 [modified] Pin
Angsuman Chakraborty11-Jun-09 2:45
memberAngsuman Chakraborty11-Jun-09 2:45 
QuestionObserver Pattern? Pin
Pat Tormey31-Mar-09 1:29
memberPat Tormey31-Mar-09 1:29 
GeneralMy vote of 2 Pin
Dinand31-Mar-09 0:07
memberDinand31-Mar-09 0:07 
GeneralSuggestion Pin
Dan1235430-Mar-09 17:09
memberDan1235430-Mar-09 17:09 
AnswerRe: Suggestion Pin
Angsuman Chakraborty31-Mar-09 2:40
memberAngsuman Chakraborty31-Mar-09 2:40 
GeneralMy vote of 2 Pin
Marc Clifton30-Mar-09 3:30
protectorMarc Clifton30-Mar-09 3:30 
GeneralSQL Injection Pin
canozurdo26-Mar-09 4:03
membercanozurdo26-Mar-09 4:03 
GeneralSuggestion Pin
Bashir Magomedov26-Mar-09 1:13
memberBashir Magomedov26-Mar-09 1:13 
GeneralRe: Suggestion Pin
Angsuman Chakraborty26-Mar-09 3:00
memberAngsuman Chakraborty26-Mar-09 3:00 
GeneralSimply good!!! Pin
Md. Ismail25-Mar-09 22:49
memberMd. Ismail25-Mar-09 22:49 
GeneralGreat start!! Pin
Member 299245225-Mar-09 22:48
memberMember 299245225-Mar-09 22:48 
GeneralNice to see your first article. Pin
Razan Paul (Raju)25-Mar-09 22:43
memberRazan Paul (Raju)25-Mar-09 22:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 26 Mar 2009
Article Copyright 2009 by Angsuman Chakraborty
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid