且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用带有c#的asp.net在web服务中的webmethod中给出默认值

更新时间:2023-09-13 16:41:22

[WebMethod(Description = "Creates A new entry in a DataBase")]
     public string  NewItem(string AdreseeEmail, string senderEmail, string phoneNum, string boxMachineName,  decimal onDeliveryAmount)
    {    
        SqlConnection MyConnection = new SqlConnection(StrConnection);
        SqlCommand MyCommand = new SqlCommand("ProCreateDelivery", MyConnection);
 
        MyCommand.CommandType = CommandType.StoredProcedure;
        MyCommand.Parameters.AddWithValue("@adreseeEmail",AdreseeEmail);
        MyCommand.Parameters.AddWithValue("@senderEmail",senderEmail);
        MyCommand.Parameters.AddWithValue("@phoneNum",phoneNum);
        MyCommand.Parameters.AddWithValue("@boxMachineName",boxMachineName);
         if (onDeliveryAmount == null)
            {
                MyCommand.Parameters.AddWithValue("@onDeliveryAmount", 0);
            }
            else
            {
                MyCommand.Parameters.AddWithValue("@onDeliveryAmount", onDeliveryAmount);
            }
       try
       {
            MyConnection.Open();
 
            if (MyCommand.ExecuteNonQuery() != 0)
 
            {
                strResult = "Created Successfully ..!";
            }
            strResult = " Created Successfully ..!";
        
        }
       catch(Exception ex)
        {
 
            strResult = ex.ToString();
       
       }
        MyConnection.Close();
        return strResult;    
    }


WebMethod不支持默认参数。您可以重载该方法,或者如果值超出范围,例如0,检查并在方法中提供默认值。
WebMethod doesn't support default parameters. You could overload the method or if a value is out of range, say 0, check for that and supply the default value within the method.