Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
SqlConnection objCon = new SqlConnection(@"Data Source=MIS-PC;Initial Catalog=projecthotel;Integrated Security=True");

SqlCommand objCmd = new SqlCommand();
objCmd.Connection = objCon;

string query = "insert into Reservation values('" + textBox1.Text + "', '" + comboBox2.Text + "','" + textBox4.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox15.Text + "','" + comboBox7.Text + "','" + comboBox8.Text + "','" + comboBox9.Text + "','" + textBox25.Text + "','" + textBox17.Text + "','" + textBox18.Text + "','" + textBox20.Text + "','" + textBox21.Text + "')";

objCmd.CommandText = query;

objCon.Open();
int j = objCmd.ExecuteNonQuery();
if (j > 0)
{

MessageBox.Show("Record Inserted","www.codingresolved.com");
dataGridView3.Rows.Clear();
}

objCon.Close();

Form1 form1 = new Form1();
form1.Show();
}
Posted

1 solution

For starters, don't do it like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

Secondly, an INSERT operation affect the DB but it doesn't affect your application display.

Thirdly, you are emptying the DataGridView when you have successfully inserted the record, so it will always be empty.

There is nothing in your code that displays records, unless your new form access the DB and retrieves them.
 
Share this answer
 
Comments
hilbert hussen 24-Apr-14 8:27am    
how to do that sir
OriginalGriff 24-Apr-14 8:39am    
How do do what, exactly?
Remember that we can't see your screen, access your HDD, or read your mind - I only get exactly what you tell me.
hilbert hussen 24-Apr-14 8:41am    
That values are going into database but not displying gridview
OriginalGriff 24-Apr-14 10:39am    
I can't see your screen, remember? :laugh:
What GridView? The one you clear? A different one?
How are you loading it? Where is it? Which form? When are you loading it?
Have you switched to parameterised queries?

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