Data transfer from one form to another


This article gives you short idea of, how to transfer data from one form to another

Any small or large project needs data transfer from one form to another.

Data transfer methods are much easier in ASP.NET (web application) than in C#.NET (Window Based application).
ASP.NET uses their own stateManagement technique

Unlike VB.NET in C#.NET we can not access all forms with it's all controls and values directly.

In this article we cover how to transfer values from one page to another in C#.NET

Below example will clear all idea

I have two form in C#.NET application
1. FormOne having 2 textboxes (Userid and Password) and 1 button (Login)
2. FormTwo having 1 Label

now, My requirement is to show Userid on FormTwo.

it can be done using following ways

SCENARIO 1:

Declear a public veriable at formTwo, see the below code



//FormTwo
public string szUserId;

onForm_Load()
{
Label.Text = szUserId;
}



now show FormTwo on click on FormOne
By creating object of FormTwo, we can Access global veriable and assign values to it



FormOne_Button1_Click()
{
//create a object of formTwo
FormTwo frmtwo = new FormTwo();

//Access global veriable and assign values to it
frmtwo.szUserId = txtUserId.Text;

//Hide formOne
this.Hide();

//show formtwo
frmTwo.Show();

}



SCENARIO 2:

Declear value in constructor on formTwo


//FormTwo
public string szUserId;

//in formtwo Constructor assign a value to it's veriable
FormTwo (string Uid)
{
Init()
this.szUserId = Uid
}

onForm_Load()
{
Label.Text = szUserId;
}



on FormOne while creating Object of FormTwo pass the userid value


//FormOne
FormTwo frmtwo = new FormTwo(txtUserId.Text);
frmTwo.Show()


Here is the simple ways to transfer the values from one form to another.

Hope it helps all.

Regards
koolprasad2003


Comments

Author: Biju26 Aug 2010 Member Level: Gold   Points : 2

CountryIL

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.IO;
using System.Data;
using Interlace.VKSRETANIUM.DTO;
namespace Interlace.VKSRETANIUM.ILayer
{
public partial interface ICountry
{
int Save(CountryDTO objCountryDTO);
int HardDeleteCountry(CountryDTO objCountryDTO);
DataTable Search(string strCodn);
}
}



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: