How to pass one form database data pass in another form
Forums.NET5 answers
Forums.NET5 answers
How to pass one form database data pass in another form
Know the answer? Post it — somebody with the same question will find it here.
Join to answer this question
Members earn 10 SHARP for a comment, 10 SHARP for a reaction, and 20 SHARP for every answer they publish.
DevSocialUpcoming · 3rd editionAI Agents ConferenceNov 23–27, 2026Live onlineMembers only16days leftRegisterMembers onlyAnswer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
Rajanikant HawaldarPosted Nov 1, 2026, 11:33 PM EST
Use delegate and events
Arun PottiPosted Nov 3, 2026, 6:11 PM EST
After successful login, get the user's name, email and contact number from the database and pass them to the Order form using its constructor.
In the Order form:
This is a simple and clean way to pass the logged-in user's information from the Login form to the Order form.
Altaf AnsariPosted Nov 6, 2026, 12:18 AM EST
If you're asking in Windows Forms (C#) how to pass data from one form to another, there are several common ways.
Method 1: Pass Data Through Constructor (Recommended)
Form1.cs
C#
private void btnOpenForm2_Click(object sender, EventArgs e)
{
string name = txtName.Text;
Form2 frm = new Form2(name);
frm.Show();
}
Form2.cs
C#
public partial class Form2 : Form
{
public Form2(string name)
{
InitializeComponent();
lblName.Text = name;
}
}
Method 2: Use Public Properties
Form2.cs
C#
public partial class Form2 : Form
{
public string UserName { get; set; }
private void Form2_Load(object sender, EventArgs e)
{
lblName.Text = UserName;
}
}
Form1.cs
C#
Form2 frm = new Form2();
frm.UserName = txtName.Text;
frm.Show();
Method 3: Pass Database Data (DataTable)
Form1.cs
C#
DataTable dt = GetDataFromDatabase();
Form2 frm = new Form2(dt);
frm.Show();
Form2.cs
C#
private DataTable _dt;
public Form2(DataTable dt)
{
InitializeComponent();
_dt = dt;
}
private void Form2_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = _dt;
}
Example
Suppose Form1 fetches employee details from SQL Server:
C#
string empName = dr["EmployeeName"].ToString();
Form2 frm = new Form2(empName);
frm.Show();
holi alicePosted Nov 6, 2026, 5:46 AM EST
This is a simple and clean way to pass the logged-in user's information from the Login form to the Order form .
Tahir AnsariPosted Nov 6, 2026, 9:37 AM EST
After a successful login, read the user's details from the database, store them in an object, and pass that object to the order form. The example below is for Windows Forms. An ASP.NET Core version is at the end.
1. Create a class for the user's details
public class LoggedInUser { public int Id { get; set; } public string Name { get; set; } = ""; public string Email { get; set; } = ""; public string ContactNumber { get; set; } = ""; }2. Fetch the data in the login form
This example uses the BCrypt.Net-Next NuGet package to check the password.
private void btnLogin_Click(object sender, EventArgs e) { const string sql = @"SELECT Id, Name, Email, ContactNumber, PasswordHash FROM Users WHERE Email = @Email"; using var con = new SqlConnection(connectionString); using var cmd = new SqlCommand(sql, con); cmd.Parameters.Add("@Email", SqlDbType.NVarChar, 256).Value = txtEmail.Text.Trim(); con.Open(); using var reader = cmd.ExecuteReader(); if (!reader.Read() || !BCrypt.Net.BCrypt.Verify(txtPassword.Text, reader["PasswordHash"].ToString())) { MessageBox.Show("Invalid email or password."); return; } var user = new LoggedInUser { Id = Convert.ToInt32(reader["Id"]), Name = reader["Name"].ToString() ?? "", Email = reader["Email"].ToString() ?? "", ContactNumber = reader["ContactNumber"].ToString() ?? "" }; var orderForm = new OrderForm(user); orderForm.FormClosed += (s, args) => Close(); // exits the app when the hidden login form closes orderForm.Show(); Hide(); }3. Receive the data in the order form
public partial class OrderForm : Form { private readonly LoggedInUser _user; public OrderForm(LoggedInUser user) { InitializeComponent(); _user = user; txtName.Text = user.Name; txtEmail.Text = user.Email; txtContact.Text = user.ContactNumber; } }When saving the order, store
_user.Idin the Orders table as the link to the user.Important
@Email, as joining strings into SQL allows SQL injection.If this is ASP.NET Core
Forms don't pass data to each other directly. Add the user's ID as a claim at login, then load their details in the order action:
[Authorize] public async Task Order() { var userId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!); var user = await _db.Users.FindAsync(userId); if (user == null) return NotFound(); return View(new OrderViewModel { Name = user.Name, Email = user.Email, ContactNumber = user.ContactNumber }); }