Jyoti Jodha

Jyoti Jodha

  • NA
  • 1.7k
  • 407.2k

how to textbox and gridview values save in database in c#

Apr 6 2017 8:40 AM
how to textbox and gridview values save in database in c# windows application
 
private void btn_Insert_Click(object sender, EventArgs e)
{
string Product_Name = string.Empty;
string Description = string.Empty;
string Qty = string.Empty;
string Unit_Price = string.Empty;
string Tax = string.Empty;
string Total_Amount = string.Empty;
foreach (DataGridViewRow row in this.dataGridView.Rows)
{
Product_Name = row.Cells[0].Value.ToString();
Description = row.Cells[1].Value.ToString();
Qty = row.Cells[2].Value.ToString();
Unit_Price = row.Cells[3].Value.ToString();
Tax = row.Cells[4].Value.ToString();
Total_Amount = row.Cells[5].Value.ToString();
this.Save(Product_Name, Description, Qty, Unit_Price, Tax,Total_Amount);
}
MessageBox.Show("Data Added");
}
private void Save(string Product_Name, string Description, string Qty, string Unit_Price, string Tax, string Total_Amount)
{
//string conString = ConfigurationSettings.AppSettings["conString"];
string sqlStatment = "INSERT INTO TestData( Name,Mobile,Date,Orderno,Description,Qty,Unit_Price,Tax,Total_Amount) VALUES(@Name,@Mobile,@Date,@Orderno,@Product_Name,@Description,@Qty,@Unit_Price,@Tax,@Total_Amount)";
//using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand(sqlStatment, con))
{
cmd.Parameters.AddWithValue("@Name", Nametxt.Text);
cmd.Parameters.AddWithValue("@Mobile", Phtxt.Text);
cmd.Parameters.AddWithValue("@Date", Datetxt.Text);
cmd.Parameters.AddWithValue("@Orderno", ONtxt.Text);
cmd.Parameters.AddWithValue("@Product_Name", Product_Name);
cmd.Parameters.AddWithValue("@Description", Description);
cmd.Parameters.AddWithValue("@Qty", Qty);
cmd.Parameters.AddWithValue("@Unit_Price", Unit_Price);
cmd.Parameters.AddWithValue("@Tax", Tax);
cmd.Parameters.AddWithValue("@Total_Amount", Total_Amount);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}

Answers (5)