Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Dear Guys,

I'm developing an application in C#.net with a MS Access database.

I want the following option:

When I click the search button all available info of a member should display on another form with a print button from where U can take print out.

Here is the code:
C#
private void button1_Click(object sender, EventArgs e)
   {
      string sql = "Select * from login where (Name='" + textBox1.Text + "')";

            OleDbDataAdapter adapter = new OleDbDataAdapter(sql, myCon);

            DataTable login = new DataTable();

            adapter.Fill(login);

            if (login.Rows.Count > 0)
            {
                textBox1.Text = login.Rows[0]["Name"].ToString();
                MessageBox.Show("Record found");                
            }
            else
            {
                textBox1.Text = "";
                MessageBox.Show("No Record Found");
            }
}

Can anybody guide to achieve my target and refine the above code.
Thanks,
Posted
Updated 23-Nov-11 22:12pm
v3
Comments
Dalek Dave 24-Nov-11 4:12am    
Edited for Grammar and Readability.
Sander Rossel 24-Nov-11 5:09am    
Glad I could help :)

1 solution

You should store the results in an Object (probably your DataTable) and pass that Object to another form like this (answer copied from a question I answered a while back):
Here is the original question: How to pass the Value one form to another form in windows form[^]

To pass Objects between Forms you can follow one of these approaches:
One is using a Property on one of your Forms.
C#
MyForm frm = new MyForm();
// It is possible to set MyValue at any point in your code, as long as you have a reference to an instance of MyForm.
frm.MyValue = "some value";
frm.Show();

Another is passing it to the constructor.
C#
public partial class MyForm : Form
{
   private String _myValue;
   public Form1(String myValue)
   {
      InitializeComponent();
      _myValue = myValue;
      // Possibly use myValue here.
   }
   // Do stuff with _myValue here.
}
Usage would look like this:
C#
MyForm frm = new MyForm("some value");
frm.Show();

Another approach could be to use a Method...
C#
public partial class MyForm : Form
{
   private String _myValue;
   public void SetValue(String myValue)
   {
      _myValue = myValue;
     // Possibly do stuff with myValue here.
   }
   // Or use _myValue here.
}
Usage:
C#
MyForm frm = new MyForm();
frm.Show();
// Once again, you can use this anywhere in your code, as long as you have a reference to the instance of MyForm.
frm.SetValue("some value");


So now you got your data in another Form, all you need to do is handle it there, edit it etc. and update it in the Form it originally came from.

Hope it helps :)
 
Share this answer
 
v2
Comments
rajeevcapgeminiindia 24-Nov-11 3:11am    
Naerling is right but it is case of Windows Appliaction. If you are talking about Asp.net pages then you use State Management to do the same.
thatraja 24-Nov-11 3:29am    
[Moved from Answer]
shah_sawar12431 wrote:
ok thanks alot dear Naerling.
RaisKazi 24-Nov-11 3:40am    
Comprehensive and equally correct. 5ed.
Sander Rossel 24-Nov-11 5:08am    
Thanks Rais :)
Dalek Dave 24-Nov-11 4:13am    
Good Answer.

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