Error during Update operation on SQL Server 2012 using ADO.NET

74 Views Asked by At

I'm trying to send two values from android to ASP.NET Web api server in order to update the SQL server database table using these two values via ADO.NET.the problem is that I'm having this error and I couldn't fix it so please help me with that and thank's in advance! :) P.S: I'm pretty sure that the communication Android/server works fine because I have already transmitted data from Server to Android Here is the ASP.NET Code:

[HttpPost]
    [ActionName("AddBox")]
    public void AddBox(DBProductCompany produit)
    {

        SqlConnection myConnection = new SqlConnection();
        //myConnection.ConnectionString = @"Server=.\SQLSERVER2008R2;Database=DBCompany;User ID=sa;Password=Tpg@1234;";
        myConnection.ConnectionString = @"Data Source=PcHeds;Database=DBCompany;User ID=sa;Password=hedi;";
        //SqlCommand sqlCmd = new SqlCommand("INSERT INTO tblEmployee (EmployeeId,Name,ManagerId) Values (@EmployeeId,@Name,@ManagerId)", myConnection);
        SqlCommand sqlCmd = new SqlCommand();
        sqlCmd.CommandType = CommandType.Text;
        sqlCmd.CommandText = "UPDATE DBProductCompany SET Login = @Login WHERE Serial = @Serial";
        sqlCmd.Connection = myConnection;

       // sqlCmd.Parameters.AddWithValue("@SensorType", produit.SensorType);
        //sqlCmd.Parameters.AddWithValue("@QrCode", produit.QrCode);
        sqlCmd.Parameters.AddWithValue("@Serial", produit.Serial);
        //sqlCmd.Parameters.AddWithValue("@DateCreated", produit.DateCreated);
        sqlCmd.Parameters.AddWithValue("@Login", produit.Login);



        myConnection.Open();
        int rowInserted = sqlCmd.ExecuteNonQuery();
        myConnection.Close();
    }

And here is the error: The parameterized query '(@Serial nvarchar(4000),@Login nvarchar(9)) Update DBProductCompany' expects the parameter '@Serial' Which was not supplied.

Here is the Android client side:(just to understand what I'm Sending

class RequestTask extends AsyncTask<String, String, String>{
     // URL ===> 192.168.134.1:57888/api/Employees/GetTemperatureBySerial?SensorSerial=
    @Override
    protected String doInBackground(String... uri) {
        String state = null;
        HttpClient httpClient = new DefaultHttpClient();

        SharedPreferences sharedpreferences = getSharedPreferences
                  (LogIn.PREF_NAME, Context.MODE_PRIVATE);

                  String Serial = sharedpreferences.getString(LogIn.PREF_SERIAL, "");
                  String Login = sharedpreferences.getString(LogIn.PREF_USERNAME, "");
        try {
            HttpPost request = new HttpPost(uri[0]);
            StringEntity params =new StringEntity("{\"Serial\":18EEAE9F-7D7F-4538-A2E0-EBE225C798AA,\"Login\":\""+Login+"\"}");
            request.addHeader("content-type", "application/json");
            request.addHeader("Accept","application/json");
            request.setEntity(params);
            HttpResponse response = httpClient.execute(request);
            if(response.getStatusLine().getStatusCode()!=204){
                state = "Done";
            }
            else{
                state = "Undone";
            }

            // handle response here...
        }catch (Exception ex) {
            // handle exception here
        } finally {
            httpClient.getConnectionManager().shutdown();
        }

        return state;
    }
0

There are 0 best solutions below