Hi guys please help me on how to save all data coming textbox and i store it to datatable and i can view it to datagridview. How can I save all the data to database. Advance thanks, here's my sample code:
private void add_Click(object sender, EventArgs e) { if (txtAddEmail.Text.Trim() == "") { MessageBox.Show("Please enter Email Address.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtAddEmail.Focus(); txtAddEmail.Visible = true; return; } dt.Rows.Add(txtAddEmail.Text); dgvEmailAdd.DataSource = dt; txtAddEmail.Text = ""; SaveEmail(); }
private void SaveEmail()
{
try
{
string strDBConfig = Application.StartupPath + @"MedXPro.xml";
using (SqlCoection co = PSCSClass.DataCoections.MDFCoection("", "", false, "", "", strDBConfig))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Coection = co;
for (int i = 0; i < dgvEmailAdd.Rows.Count; i++)
{
string StrQuery = @"INSERT INTO EmailAddress (EmailAddress) VALUES ("
+ dgvEmailAdd.Rows[i].Cells["Email Address"].Value + ");";
cmd.CommandText = StrQuery;
cmd.ExecuteNonQuery();
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
