SqlConnection objCon = new SqlConnection(@"Data Source=MIS-PC;Initial Catalog=projecthotel;Integrated Security=True");
SqlCommand objCmd = new SqlCommand();
objCmd.Connection = objCon;
string query = "Select * from Reservation";
for (int i = 0; i < dataGridView3.Rows.Count - 1; i++)
{
query = query + "insert into Reservation values(@Code, @Status,@Guest Name,@Guest Email,@Guest Phone No,@Checkin,@Check Out,@Adult No,@Child No,@Infant No,@Net Total,@Discount,@Tax,@Total,@Paid)";
query = query.Replace("@Code", "'" +textBox1.Text+ "'");
query = query.Replace("@Status", "'" + dataGridView3.Rows[i].Cells["Status"].Value.ToString() + "'");
query = query.Replace("@Guest Name", "'" + dataGridView3.Rows[i].Cells["Guest Name"].Value.ToString() + "'");
query = query.Replace("@Guest Email", "'" + dataGridView3.Rows[i].Cells["Guest Email"].Value.ToString() + "'");
query = query.Replace("Guest Phone No", "'" + dataGridView3.Rows[i].Cells["Guest Phone No"].Value.ToString() + "'");
query = query.Replace("Checkin", "'" + dataGridView3.Rows[i].Cells["Checkin"].Value.ToString() + "'");
query = query.Replace("Checkin", "'" + dataGridView3.Rows[i].Cells["Checkin"].Value.ToString() + "'"); query = query.Replace("Check Out", "'" + dataGridView3.Rows[i].Cells["Check Out"].Value.ToString() + "'");
query = query.Replace("Adult No", "'" + dataGridView3.Rows[i].Cells["Adult No"].Value.ToString() + "'");
query = query.Replace("Child No", "'" + dataGridView3.Rows[i].Cells["Child No"].Value.ToString() + "'");
query = query.Replace("Infant No", "'" + dataGridView3.Rows[i].Cells["Infant No"].Value.ToString() + "'");
query = query.Replace("Net Total", "'" + dataGridView3.Rows[i].Cells["Net Total"].Value.ToString() + "'");query = query.Replace("Checkin", "'" + dataGridView1.Rows[i].Cells["Checkin"].Value.ToString() + "'");
query = query.Replace("Discount", "'" + dataGridView3.Rows[i].Cells["Discount"].Value.ToString() + "'");
query = query.Replace("Tax", "'" + dataGridView3.Rows[i].Cells["Tax"].Value.ToString() + "'");
query = query.Replace("Total", "'" + dataGridView3.Rows[i].Cells["Total"].Value.ToString() + "'");
query = query.Replace("Paid", "'" + dataGridView3.Rows[i].Cells["Paid"].Value.ToString() + "'");
}
objCmd.CommandText = query;
objCon.Open();
int j = objCmd.ExecuteNonQuery();
if (j > 0)
{
MessageBox.Show("Record Inserted","www.codingresolved.com");
dataGridView1.Rows.Clear();
}
objCon.Close();
}
I did this for insert data into gridview
but ot inserting any data