Click here to Skip to main content
15,914,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.

I have written a code below:

C#
protected void SubmitButton(object sender, EventArgs e)
{
    SqlConnection Conn = new SqlConnection();

    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["PersonTest"].ConnectionString;
    SqlCommand Cmd = new SqlCommand();
    Cmd.CommandText = "INSERT INTO Perosn [Name,Family] Values (@Name,@Family)";

    Cmd.Connection = Conn;
    Conn.Open();
    Cmd.ExecuteNonQuery();
    Conn.Close();
}


but when debug the solution this exception appears :

SqlException was unhandled by user code.
Incorrect syntax near 'Name,Family'.


what shall i doooo!!!?
Posted

you have not passed values to the argument @Name,@Family

so pass argument values to it using


Cmd.CommandText="INSERT INTO Perosn [Name,Family] Values (@Name,@Family)";
Cmd.Parameters.AddWithValue("@Name", txtName.Text);
Cmd.Parameters.AddWithValue("@Family", txtFamily.Text);

Cmd.Connection = Conn;
Conn.Open();
Cmd.ExecuteNonQuery();
Conn.Close();
 
Share this answer
 
Comments
mohammad ehsan 18-Jul-12 2:02am    
thank of your attention ,
but still the same error occures.
Hi..
You have either not pass the parameters @Name,@Family... To Fetch the data...
Orelse in your command hope the table name spelling is misspelled as Perosn instead of Person i think..
So check it out..
 
Share this answer
 
Hi,
Try this:
C#
protected void SubmitButton(object sender, EventArgs e)
{
     SqlConnection Conn = new SqlConnection();
 
     Conn.ConnectionString = ConfigurationManager.ConnectionStrings["PersonTest"].ConnectionString;
     SqlCommand Cmd = new SqlCommand();
     Cmd.CommandText = "INSERT INTO Perosn (Name, Family) Values (@Name,@Family)";
     //probem was(Name, Family) brackets and you din't added the parameters.
     Cmd.Parameters.AddWithValue("@Name", txtName.Text);
     Cmd.Parameters.AddWithValue("@Family", txtFamily.Text);
     Cmd.Connection = Conn;
     Conn.Open();
     Cmd.ExecuteNonQuery();
     Conn.Close();                
}



--Amit
 
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