3
Answers

Error

ALOK SHARMA

ALOK SHARMA

10y
846
1

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:

Line 38:        cmd.Parameters.AddWithValue("@State",ddlState.SelectedValue.ToString()); Line 39:        Line 40:        int result = cmd.ExecuteNonQuery(); Line 41:         if (result > 0) Line 42:         {

Source File: c:\Website_Project\mtEmployee\Employeemt.aspx.cs Line: 40

Answers (3)