Execution of query??
-
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 can't executed successfully please help ??:confused:
-
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 can't executed successfully please help ??:confused:
-
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 can't executed successfully please help ??:confused:
Member 10564850 wrote:
My query can't executed successfully please help ??
As Griff mentioned on Saturday:
Just remember in future - the better the info you give us, the quicker we can give you a solution!
Once again, you've posted your code, but not the error message, which leaves us to play another game of "guess the error". Assuming the problem is with the column names containing special characters, the quick fix is to wrap the affected column names in square brackets. You'll also need to fix the parameter names:
string s = "INSERT INTO [Students Records] (Name, Age, Class, Gender, [Guardian's Name], [Contact.No], Address, [Email-Address]) VALUES(@Name, @Age, @Class, @Gender, @GuardiansName, @ContactNo, @Address, @EmailAddress)";
A better solution is to change your database structure so that the table and column names do not contain anything other than letters and numbers - no spaces, no punctuation, and no extended characters. The same restrictions apply to parameter names. If that doesn't solve the problem, then you'll need to provide the details of the exception being thrown when you try to execute the query.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
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 can't executed successfully please help ??:confused:
-
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 can't executed successfully please help ??:confused:
thatraja
Code converters | Education Needed No thanks, I am all stocked up. - Luc Pattyn When you're wrestling a gorilla, you don't stop when you're tired, you stop when the gorilla is - Henry Minute
-
Member 10564850 wrote:
My query can't executed successfully please help ??
As Griff mentioned on Saturday:
Just remember in future - the better the info you give us, the quicker we can give you a solution!
Once again, you've posted your code, but not the error message, which leaves us to play another game of "guess the error". Assuming the problem is with the column names containing special characters, the quick fix is to wrap the affected column names in square brackets. You'll also need to fix the parameter names:
string s = "INSERT INTO [Students Records] (Name, Age, Class, Gender, [Guardian's Name], [Contact.No], Address, [Email-Address]) VALUES(@Name, @Age, @Class, @Gender, @GuardiansName, @ContactNo, @Address, @EmailAddress)";
A better solution is to change your database structure so that the table and column names do not contain anything other than letters and numbers - no spaces, no punctuation, and no extended characters. The same restrictions apply to parameter names. If that doesn't solve the problem, then you'll need to provide the details of the exception being thrown when you try to execute the query.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Whoops! Already moved. Sorry! :-\
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
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 can't executed successfully please help ??:confused:
Again, you're putting spaces and special characters in the parameter names. DO NOT USE SPACES AND SPECIAL CHARACTERS IN TABLE NAMES, FIELD NAMES AND PARAMETER NAMES! Your field names should be something like this:
Name, Age, Class, Gender, GuardianName, ContactPhone, Address, EmailAddress
By making the code more "eyeball friendly" to read, you are giving yourself headaches debugging the resulting code. "Eyeball friendly" is NOT "compiler friendly"!
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
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 can't executed successfully please help ??:confused:
Clean-up crew needed, grammar spill... - Nagy Vilmos