Hello all,

I want to ask for you, how to i create function with optional parameter in webservice and how i call this function in my application. Thanks

this is my code...

Webservice:

[WebMethod(Description = "Input data ke Database")]
    public bool inputAllData(string myTable, [Optional, DefaultParameterValue("")] string field1, [Optional, DefaultParameterValue("")] string field2, [Optional, DefaultParameterValue("")] string field3,
                            [Optional, DefaultParameterValue("")] string param1, [Optional, DefaultParameterValue("")] string param2)
    {
        bool retVal = false;
        string query = "";

        try
        {
            query = "INSERT INTO " + myTable + " (" + field1 + ", " + field2 + ") " +
                    "VALUES ('" + param1 + "', '" + param2 + "')";
            if (Database.ExecNonQueryConnOra(query) != false)
            {
                retVal = true;
            }
            
        }
        catch (Exception)
        { }

        return retVal;
    }

My Application:

private void btnSave_Click(object sender, EventArgs e)
        {
            SMSystemForm.Service myService = new SMSystemForm.Service();

            try
            {
                if (myService.inputAllData("ref_propinsi", "ID", "NAMA", txtID.Text, txtNama.Text, "") == true)
                {
                    MessageBox.Show("Sukses Input");
                }
                else
                {
                    MessageBox.Show("Gagal input");
                }
            }
            catch(Exception)
            {}
        }

I'm musing Visual studio 2005. When i call my function from webservice show error:
No overload for method 'inputAllData' takes '5' arguments.

Please help me.. I stuck in here.. Thanks for you help...

You can't use optional parameters in WebMethod. Take a look at an alternative - Method overloading.

You can't use optional parameters in WebMethod. Take a look at an alternative - Method overloading.

Thanks for your answer. Please give me example of its use..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.