mardi 31 mars 2015

How to pass data from one form to another?

I'm building a C# application which has to retreive a table data from my local database & display it on the form1, the form1 contains a separate button for Email function when i click on the Email button a new form form2 is opening through which a email can be sent & it contains To,cc,subject & message body, i want the retrieved data from database to be displayed in message body & the same data to be sent to the mail ids when i click on send button.


Currently I'm able to retrieve a data in form1 & from form2 i'm able to send a email successfully.


Can someone tell me how can i bring the data on form1 to form2 directly to message body of the Email


Form1 code



private void exitBtn_Click(object sender, EventArgs e)
{
this.Close();
}


private void exitBtn_MouseClick(object sender, MouseEventArgs e)
{
this.Close();
}
private void submitBtn_MouseClick(object sender, EventArgs e)
{
try
{
String str = "server=localhost;port=3306;database=demo;UID=root;password=admin1234";
String query = "select * from demo.cstdetails;";
MySqlConnection con = new MySqlConnection(str);
MySqlCommand cmd = new MySqlCommand(query);
con.Open();
MySqlDataAdapter sda = new MySqlDataAdapter(query, str);
DataSet ds = new DataSet();
sda.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
//MessageBox.Show("connect with sql server");
con.Close();
}
catch (Exception es)
{
MessageBox.Show(es.Message);

}
}

public void sql_conn()
{
throw new NotImplementedException();
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}

private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{

}

private void emailBtn_Click(object sender, System.EventArgs e)
{
email frm = new email();
frm.Show();
}


}


Form2 Code



private void sndBtn_Click(object sender, EventArgs e)
{
// MessageBox.Show("This is sendbutton");
try
{
SmtpClient server = new SmtpClient("smtp.gmail.com");
server.Port = 587;
server.EnableSsl = true;
server.Timeout = 100000;
server.DeliveryMethod = SmtpDeliveryMethod.Network;
server.UseDefaultCredentials = false;
server.Credentials = new NetworkCredential("user", "password");
MailMessage msg = new MailMessage();
msg.To.Add(textBox_To.Text);
msg.From = new MailAddress("mytest@gmail.com");
msg.Subject = textBox_Subject.Text;
msg.Body = dataGridView1.DataSource = ds.Tables[0].DefaultView;
try
{
String str = "server=localhost;port=3306;database=demo;UID=root;password=admin1234";
String query = "select * from demo.cstdetails;";
MySqlConnection con = new MySqlConnection(str);
MySqlCommand cmd = new MySqlCommand(query);
con.Open();
MySqlDataAdapter sda = new MySqlDataAdapter(query, str);
DataSet ds = new DataSet();
sda.Fill(ds);
//MessageBox.Show("connect with sql server");
con.Close();
}
catch (Exception es)
{
MessageBox.Show(es.Message);

}
//Attachment data = new Attachment(textBox_Attachment.Text);
// msg.Attachments.Add(data);
server.Send(msg);
MessageBox.Show("Successfully Sent Message.");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void dscrdBtn_Click(object sender, EventArgs e)
{
this.Close();
}
private void dscrdBtn_MouseClick(object sender, MouseEventArgs e)
{
this.Close();
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}
}

Aucun commentaire:

Enregistrer un commentaire