Frage

Ok so yesterday I posted some problem I had with some system I'm developing. The problem I have now here, I am going through the worst case scenario and thinking that the user somehow tries to call my web service and forgets to put in the parameters, therefore the web service receives null parameters ... I tried validating with If, and still it threw me an ArgumentException error, so I left the took off the if validation and instead I placed the whole code in a try-catch, the problem is that this "catch does not go into that code block, it just keeps throwing the ArgumentException on plain web text ...

Here is my code:

[WebMethod(Description = "Private Title", EnableSession = false)]
public string[] M102(int p_enclosure, string p_transaction, int p_operation, int p_commodity,
                          string p_bl, string p_inDate, string p_blhouse, string p_anexo29, int p_tranType,
                          string p_vessel, string p_trip, int p_entry, string p_padlock, string p_brands, 
                          string p_appoint, string p_plates, string p_qmark)
{
    string eDate;
    try
    {/*/WebService Code/*/}
    catch (ArgumentException Ex)
    {
        string message;
        string[] error = 
            { 
                "000",
                "DataCaptureError.- " + Ex.Message,
                "Date"
            };
        SqlConnection con8 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
        SqlCommand cmd8 = new SqlCommand();
        cmd8.Connection = con8;
        cmd8.CommandText = "dbo.sp_errorlog";
        cmd8.CommandType = CommandType.StoredProcedure;
        cmd8.Parameters.Add("@p_inTrans", SqlDbType.NChar, 12).Value = "0";
        cmd8.Parameters.Add("@p_enclosure", SqlDbType.NChar, 6).Value = "0";
        cmd8.Parameters.Add("@p_trans", SqlDbType.NChar, 18).Value = "0";
        cmd8.Parameters.Add("@p_method", SqlDbType.NChar, 6).Value = "102";
        cmd8.Parameters.Add("@p_message", SqlDbType.NVarChar, 250).Value = error[1];
        cmd8.Parameters.Add("@vo_message", SqlDbType.VarChar, 250).Direction = ParameterDirection.Output;
        cmd8.Parameters.Add("@vo_errorDate", SqlDbType.DateTime).Direction = ParameterDirection.Output;
        con8.Open();
        cmd8.ExecuteNonQuery();
        con8.Close();
        cmd8.Connection.Close();
        message = "" + cmd8.Parameters["@vo_message"].Value;
        eDate = "" + cmd8.Parameters["@vo_errorDate"].Value;
        eDate = Convert.ToDateTime(eDate).ToString("dd/MM/yyyy HH:mm:ss");
        error[2] = eDate;
        return error;
    }

}

What I'm trying to do is to get that error message and put it in a my DB ... The thing is that it never enters in the CATCH code, it goes straight and spit out the error.

Hope somebody can help me find a way to catch or validate this NULL parameter issue and stick it in the DB

Thanks anyways guys.

War es hilfreich?

Lösung

If you want to allow the user to pass null you would have to make the int parameters nullable. Change the signature to use int? instead of int.

public string[] M102(int? p_enclosure, string p_transaction, int? p_operation, int? p_commodity,
                      string p_bl, string p_inDate, string p_blhouse, string p_anexo29, int p_tranType,
                      string p_vessel, string p_trip, int? p_entry, string p_padlock, string p_brands, 
                      string p_appoint, string p_plates, string p_qmark)

The strings (and other reference types) are fine but any value type parameters cannot receive null arguments which is most likely where the exception occurs.

Then, when dealing with the nullable parameters in the method body, you have to test whether the parameter has a value using the HasValue property and to get the actual value, you will have to use the Value property.

Inside the method body you will have to manually test for null on each parameter, and if any are null that are not supposed to be (i.e. the integer ones) return an error result and log it in your system.

// you'd want to add this check early in the method body
if(!p_enclosure.HasValue || p_transaction == null || !p_operation.HasValue ...
{
    try{
       // log the error
    }catch{
       // ignore errors from the logging system
    }

    //   and stop processing
    return null; // or whatever makes sense in case of invalid arguments
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top