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";
}
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