Por Weiting
Por Weiting

Reputation: 167

how to do a insert statement in web service asp.net

i am suppose to receive the data from android to my web service and my web service is going to insert the data into my data base. how am i going to do about it to insert the data from my web service to database.

this is my start of code :

 [WebMethod]
    public StudentTransactions GetStudentTransactions(string Name, string CLass, string NRIC, int StallNo, float AmountSpent)
     {
        SqlCommand sql1 = new SqlCommand("INSERT Into StudentTransactions (Name, CLass,NRIC,StallNo,AmountSpent) VALUES (Name,CLass,NRIC,StallNo,AmountSpent)");
         using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ncpsdbbConnectionString2"].ConnectionString))
         {
             conn.Open();

             {

             }
         }

Upvotes: 0

Views: 706

Answers (1)

TGH
TGH

Reputation: 39248

using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ncpsdbbConnectionString2"].ConnectionString))
    {
        SqlCommand command = new SqlCommand("INSERT Into StudentTransactions (Name, CLass,NRIC,StallNo,AmountSpent) VALUES (@Name,@CLass,@NRIC,@StallNo,@AmountSpent)");
        command.Connection.Open();
        command.ExecuteNonQuery();
    }

Add the values and params like so:

command.Parameters.AddWithValue("@ParamName", [Some Value]);

Try the above. Read more here: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx

Upvotes: 1

Related Questions