Sindu_
Sindu_

Reputation: 1455

Insert data to a table - web service in visual studio

I am trying to use a web service to insert data to a table using visual studio. When I eun it, select the method and type in the parameters and then click on the Invoke button I get the following errors. I've checked the syntax of the insert statement and also tried using different syntax's. But I'm getting the same errors. What am I doing wrong?

System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'User'.
  at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
  at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
  at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
  at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
  at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
  at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
  at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
  at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
  at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
  at Service.register(String fname, String lname, String email, String num, Int32 locID)

//Web Method to Insert values to table//

[WebMethod]
public void register(string fname, string lname, string email, string num, int locID)
{
    SqlConnection conn;
    conn = ConnectionManager.GetConnection();
    conn.Open();
    string cmdString = "INSERT into User values(@fname,@lname,@email,@num,@locID)";
    SqlCommand sqlCommand = new SqlCommand(cmdString, conn);
    sqlCommand.CommandType = CommandType.Text;

    sqlCommand.Parameters.Add("@fname", SqlDbType.Text).Value = fname;
    sqlCommand.Parameters.Add("@lname", SqlDbType.Text).Value = lname;
    sqlCommand.Parameters.Add("@email", SqlDbType.Text).Value = email;
    sqlCommand.Parameters.Add("@num", SqlDbType.Text).Value = num;
    sqlCommand.Parameters.Add("@locID", SqlDbType.Text).Value = locID;
    sqlCommand.ExecuteNonQuery();
    conn.Close();
}

///Connection Manager Class///

public class ConnectionManager
 {
    public static SqlConnection NewCon;
     public static string ConStr ="Data Source=ACER-PC\\SQLEXPRESS;Initial Catalog=DisasterAlert;Integrated Security=True";

    public static SqlConnection GetConnection()
   {
        NewCon = new SqlConnection(ConStr);
        return NewCon;
   }

 }

Upvotes: 0

Views: 1726

Answers (1)

user900202
user900202

Reputation:

"user" is a reserved word of SQL. try this

"INSERT into [User] values(@fname,@lname,@email,@num,@locID)"

Upvotes: 1

Related Questions