Click here to Skip to main content
15,907,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
try
{
string s = "INSERT INTO [Students Records] (Name,Age,Class,Gender,Guardian's Name,Contact.No,Address,Email-Address) VALUES(@Name,@Age,@Class,@Gender,@Guardian's Name,@Contact.No,@Address,@Email-Address)";
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand(s,con);
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Name", textBox1.Text);
cmd.Parameters.AddWithValue("@Age", textBox2.Text);
cmd.Parameters.AddWithValue("@Class", textBox3.Text);
cmd.Parameters.AddWithValue("@Gender", textBox4.Text);
cmd.Parameters.AddWithValue("@Guardian's Name", textBox5.Text);
cmd.Parameters.AddWithValue("@Contact.No", textBox6.Text);
cmd.Parameters.AddWithValue("@Address", textBox7.Text);
cmd.Parameters.AddWithValue("@Email-Address", textBox8.Text);
 

con.Open();
cmd.ExecuteNonQuery();
con.Close();
 
MessageBox.Show("Saved!");
 
}
My query fails to execute successfully. Please help.:confused:
Posted
Updated 4-Feb-14 1:10am
v2
Comments
King Fisher 4-Feb-14 7:07am    
Error?
Member 10564850 4-Feb-14 7:31am    
No error the query doesn't add anything

1 solution

Okay, the issue looks as though it's probably to do with the names that you have allocated to things. Try changing it to this:
SQL
string s = "INSERT INTO [Students Records] (Name,Age,Class,Gender,[Guardian's Name],[Contact.No],Address,Email-Address) VALUES(@Name,@Age,@Class,@Gender,@Guardian,@Contact,@Address,@Email-Address)";
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand(s,con);
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Name", textBox1.Text);
cmd.Parameters.AddWithValue("@Age", textBox2.Text);
cmd.Parameters.AddWithValue("@Class", textBox3.Text);
cmd.Parameters.AddWithValue("@Gender", textBox4.Text);
cmd.Parameters.AddWithValue("@Guardian", textBox5.Text);
cmd.Parameters.AddWithValue("@Contact", textBox6.Text);
cmd.Parameters.AddWithValue("@Address", textBox7.Text);
cmd.Parameters.AddWithValue("@Email-Address", textBox8.Text);
 
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