Welcome to Dream.In.Code
Become a C# Expert!

Join 150,407 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 949 people online right now. Registration is fast and FREE... Join Now!




using textbox value of one form into other form

 
Reply to this topicStart new topic

using textbox value of one form into other form, how can i use txtusername value of form1 into if statement of form2

sada
28 Mar, 2008 - 09:23 AM
Post #1

New D.I.C Head
*

Joined: 28 Mar, 2008
Posts: 0

Code of Form1:


csharp

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


namespace WindowsApplication1
{


public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}




private void button1_Click(object sender, EventArgs e)
{
OleDbConnection objcon = new OleDbConnection();
objcon.ConnectionString = "Provider=Microsoft.Jet.oleDB.4.0;Data source=Z:\\Student.mdb";
Form2 objfrm = new Form2(txtusername.Text);
try
{
objcon.Open();
OleDbCommand objcmd = new OleDbCommand();
objcmd.CommandText = "select * from tblstudent where ID=@ID and Name=@Name";
OleDbParameter paramid = new OleDbParameter("@ID", txtpassword.Text);
OleDbParameter paramname = new OleDbParameter("@Name", txtusername.Text);

objcmd.Parameters.Add(paramid);
objcmd.Parameters.Add(paramname);
objcmd.Connection = objcon;

string pass = txtpassword.Text.ToString();
string pass1 = txtusername.Text.ToString();

OleDbDataReader rd = objcmd.ExecuteReader();

if (rd.Read())
{
if (pass == rd["ID"].ToString())
{



Form2 objfrm2 = new Form2(txtusername.Text);
objfrm.Show();



}
}







else
{
MessageBox.Show("Error");
}








}
catch (OleDbException ex)
{
MessageBox.Show(ex.Message);

}
finally
{
objcon.Close();

}


}


}

}



Code of Form2:


csharp

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


namespace WindowsApplication1
{
public partial class Form2 : Form
{
public Form2(string strTextBox)
{
InitializeComponent();




}




private void Form2_Load(object sender, EventArgs e)
{

OleDbConnection objcon = new OleDbConnection();
objcon.ConnectionString = "Provider=Microsoft.Jet.oleDB.4.0;Data source=Z:\\Student.mdb";

try
{
objcon.Open();
OleDbCommand objcmd = new OleDbCommand();
objcmd.CommandText = "select * from tblstudent where ID=@ID and Name=@Name ";
//OleDbParameter paramid = new OleDbParameter("@ID", );
//OleDbParameter paramname = new OleDbParameter("@Name", txtusername.Text);
OleDbParameter paramage = new OleDbParameter("@Age",txtage.Text);
OleDbParameter paramcgpa = new OleDbParameter("@Cgpa",txtcgpa.Text);
OleDbParameter paramcity = new OleDbParameter("@City", txtcity.Text);
OleDbParameter paramclass = new OleDbParameter("@Class", txtclass.Text);
OleDbParameter paramsemester = new OleDbParameter("@Cgpa", txtsemester.Text);
//objcmd.Parameters.Add(paramid);
//objcmd.Parameters.Add(paramname);
objcmd.Parameters.Add(paramage);
objcmd.Parameters.Add(paramcgpa);
objcmd.Parameters.Add(paramcity);
objcmd.Parameters.Add(paramclass);
objcmd.Parameters.Add(paramsemester);
objcmd.Connection = objcon;

// string pass = txtpassword.Text.ToString();



OleDbDataReader rd = objcmd.ExecuteReader();
Form1 frm=new Form1();


if (rd.Read())
{





txtage.Text = rd["Age"].ToString();
txtcgpa.Text = rd["Cgpa"].ToString();
txtcity.Text = rd["City"].ToString();
txtclass.Text = rd["Class"].ToString();
txtsemester.Text = rd["Semester"].ToString();

}


else
{
MessageBox.Show("Error");
}




}

catch (OleDbException ex)
{
MessageBox.Show(ex.Message);

}
finally
{
objcon.Close();

}
}
}
}

*edit: Please use code tags in the future, thanks! code.gif

This post has been edited by Martyr2: 28 Mar, 2008 - 09:30 AM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Using Textbox Value Of One Form Into Other Form
28 Mar, 2008 - 10:17 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 314 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Unlike VB.NET where you can just help yourself to controls on other forms, C# makes them private to the class (after all windows forms are just classes). So in order to get your two forms to communicate you can take several approaches.

1) Make the control public
2) Expose the control through a property or public function
3) Or in some cases if you just need to communicate messages, you could use delegates.

Typically you use 1 if you don't care what other forms do with the control. You use 2 when you want others to use the control, but have specialized permissions on what they can do with that control (like controlling data values etc that go into a control... eg a textbox can only accept numbers for instance). You use number 3 when you are communicating event messages or trying to tie forms together like in a child to parent situation where if they type in a textbox on the parent all the children will also update their controls.

It is up to you as to which method you want and best fits your design. smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 07:29PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month