0

Im trying to create an ASMX Web Service in Visual Web Developer.I have successfully Implemented the following

  • Created a Database with columns Email,Name
  • Created a Service

I have also specified connection string web.config like

<connectionStrings>
  <add name="mydb" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|Database.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
    </connectionStrings>

But i get an SQL Exception "Incorrect Syntax Near @Name" after entering the data in the service form

[WebMethod]
public string insertuser(String UserName,String Email)
{

    string connectionString = ConfigurationManager.ConnectionStrings["mydb"].ConnectionString;

    SqlConnection sqlCon = new SqlConnection(connectionString);
    SqlCommand nonqueryCommand = sqlCon.CreateCommand();
    sqlCon.Open();
    nonqueryCommand.CommandText = "INSERT  INTO UID (Email, Name) VALUES (@Email, @Name";

    // Add Parameters to Command Parameters collection
    nonqueryCommand.Parameters.Add("@Email", SqlDbType.VarChar, 10);
    nonqueryCommand.Parameters.Add("@Name", SqlDbType.VarChar, 20);


    nonqueryCommand.Parameters["@Email"].Value = UserName;
    nonqueryCommand.Parameters["@Name"].Value = Email;


    nonqueryCommand.ExecuteNonQuery();
    return "done";

}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
techno
  • 6,100
  • 16
  • 86
  • 192

3 Answers3

1

as andomar has correctly pointed out you are missing closing bracket in your sql command

just one thing you can generate insert command in you sql and just paste the generated sql into your code.

IE your code should look like

nonqueryCommand.CommandText = "INSERT INTO UID (Email, Name) VALUES (@Email, @Name)";

cpoDesign
  • 8,953
  • 13
  • 62
  • 106
0

Add closing parenthesis:

VALUES (@Email, @Name";
Andomar
  • 232,371
  • 49
  • 380
  • 404
0

You are missing your closing bracket:

VALUES (@Email, @Name)";
Neil Knight
  • 47,437
  • 25
  • 129
  • 188