Click here to Skip to main content
15,885,729 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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
Posted
Comments
[no name] 19-Apr-14 8:46am    
Sorry.... I am not seeing any code that inserts any data to any grid view. Did you post the wrong code? And please format your code so that it is readable.
ZurdoDev 19-Apr-14 8:49am    
Do you mean insert into the database instead of the grid view? Either way, just debug your code and let us know what's wrong.
[no name] 19-Apr-14 8:53am    
What is it that you think a query that starts with ""Select * from Reservationinsert into Reservation values ..." is going to do? You really need to search for an example for a parameterized query before you do anything more with this code.

1 solution

It's nice that you're using parameterized queries! But, it's also an EPIC fail on your part by using string.Replace to fill in the parameters!

Google for "C# parameterize queries SqlParameter" for how to do this the correct way.
 
Share this 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