Hi , I need a help on the below error . I am trying to insert values into table and also want to update Gridview simultaneously. protected void btnSubmit_Click(object sender, EventArgs e) {
SqlConnection con = new SqlConnection(@"Data Source=SONY-VAIO\SQLEXPRESS;Initial Catalog=Employee;Integrated Security=True"); SqlCommand cmd = new SqlCommand("insert into Employe(Employee_Name,Gender,Mobile,Address,ity,State) values(@Employee_Name,@Gender,@Mobile,@Address,@City,@State",con); con.Open(); cmd.CommandType = CommandType.Text; cmd.Parameters.AddWithValue("@Employee_Name",txtEmpName.Text.ToString()); cmd.Parameters.AddWithValue("@Gender",RdGender.SelectedValue.ToString()); cmd.Parameters.AddWithValue("@Mobile",txtMobile.Text.ToString()); cmd.Parameters.AddWithValue("@Address",txtAddress.Text.ToString()); cmd.Parameters.AddWithValue("@City",ddlCity.SelectedValue.ToString()); cmd.Parameters.AddWithValue("@State",ddlState.SelectedValue.ToString()); int result = cmd.ExecuteNonQuery(); if (result > 0
{
FillGridView(); }
else{Response.Write("Wrong insertion");}
pasting
Incorrect syntax near '@State'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '@State'.
Source Error:
|
Source File: c:\Website_Project\mtEmployee\Employeemt.aspx.cs Line: 40
VulpesPosted Jan 27, 2014, 3:47 PM
cmd.Parameters.AddWithValue("@State",ddlState.SelectedValue.ToString());
If the SelectedValue property returned 'null' then this would obviously be a problem but I think you'd get a different error.
If you haven't tried it already, I'd still try correcting the query to see whether the error disappears.
ALOK SHARMAPosted Jan 27, 2014, 3:25 PM
VulpesPosted Jan 27, 2014, 2:49 PM