Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
How to pass value from one text box to a new form. in detailed i mean i have a textbox i want to pass that textbox's text to a new form that will do action like displaying a label's text as a sql server's column i have made that code with many search and this community have helped me to bind a column's data into a label. but i just want to pass a text box value to another form there will be label which will initiated with column's name which is given to it in prevous form
my code is like this which is of one form.i want to divide it in two form

C#
private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\aquib\My Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Database1.mdf;Integrated Security=True;User Instance=True";
            string q = ("select SEC_QUESTION from Table1 where USERNAME='" + textBox1.Text + "'");
            con.Open();
            SqlCommand cmd = new SqlCommand(q, con);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows ==true )
            {
                while (dr.Read())
                {
                   label1.Text = dr["SEC_QUESTION"].ToString();
                }
              dr.Close();
                con.Close();
            }
        }


first the texbox will be there then passing by submit button a second form will open which will set the label's text as column. just want some idea how to divide in two forms
can this be happen? we can do such thing?
Posted
Updated 26-Aug-12 7:41am
v4

You can access any form's control by changing the modifiers state as public.
Then you can create an object. Then use that control.
C#
public partial class Form1 : Form
{
public static Form2 f2=null;
Private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\aquib\My Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Database1.mdf;Integrated Security=True;User Instance=True";
            string q = ("select SEC_QUESTION,col2,col3 from Table1 where USERNAME='" + textBox1.Text + "'");
            con.Open();
            SqlCommand cmd = new SqlCommand(q, con);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows ==true )
            {
                f2=new Form2();
                while (dr.Read())
                {
                   label1.Text = dr["SEC_QUESTION"].ToString();
                   f2.textBox1.Text=dr["col2"].ToString();
                }
                dr.Close();
                con.Close();
                f2.Show();
            }
              
        }

now its your code.
try this.
 
Share this answer
 
v3
Comments
sariqkhan 26-Aug-12 12:54pm    
yup i agree with you. access modifiers place a role but here.how can i do with it?
i have explained m doubt in detailed help me with it
[no name] 26-Aug-12 13:11pm    
When you are retrieving the data from the sql query, that time only, create the static objects of the forms and place the data on them.

Then on your submit button jest call the show function of the objects.

public static Form2 obj=new Form2();
obj.textBox1.Text="data";


obj.Show();

it should work for you.
sariqkhan 26-Aug-12 13:39pm    
can you please
rewrite the code. I cant understand code in writing.
[no name] 26-Aug-12 14:03pm    
now refer the solution once again
[no name] 26-Aug-12 14:03pm    
refer the solution once again
hi,

first create object of your form.

frmTest f = new frmTest();
f.Tag = txtTextbox.text;
f.Show();

Now on the page load of second page. you have write follwing code.
suppose i am displaying value in lable;

label1.Text = this.Tag.ToString();

Its 100% work..

Thanks,
Viprat Shah
 
Share this answer
 
Comments
VIPR@T 27-Aug-12 8:24am    
is it not working???
can you explain???
sariqkhan 27-Aug-12 10:26am    
bro i am not working with a textbox value i am working with database connectivity. So how can we do? Can you explain me in detail please
VIPR@T 28-Aug-12 0:40am    
you can pass any value in palce of textbox.
if you want to pass multiple values then also you can pass like this.

this.Tag = "ABC" | "PQR" | "XYZ"

now you can take one string array on the page of Form2.
Split the value with | this.
you can get the values.

if you are working with database connectivity then store the fetch value in string and pass that value.

Thanks,
Viprat
[no name] 27-Aug-12 10:37am    
sariqkhan is using winforms not asp
so your code is useless for him.
Read the question carefully.
sariqkhan 27-Aug-12 11:18am    
absolutely correct
:)
Create some static class file suppose Class1
Declare static variable in that.
Public static string strColumn;

In first form use static variable.

C#
Private void button1_Click(object sender, EventArgs e)
{
// If class file is not static then create object of class file & use object to //access variable
//Class1 obj = new Class(); 
//obj.strColumn = txtColumn.Text;
 
Class1.strColumn  = txtColumn.Text;
Form2.Show();
}


Second form Coding
C#
Private void Page_load((object sender, EventArgs e))
{

  string q = ("select SEC_QUESTION from Table1 where USERNAME='" + Class1.strColumn + "'");
            
}
 
Share this answer
 
Comments
sariqkhan 27-Aug-12 10:14am    
bro your first part can you explain me with my coding part? how to create that?
i am a beginer in coding i am studying can you help me out with this please
Member 13956159 6-Sep-18 8:40am    
we write in one textbox and we get meaning from database in secondtextbox of word that we write in first textbox.plz help me how code in c# for this

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900