Windows Forms: CRUD Web Service Call Function In C#

How to call web service in C# Windows Forms Application.

To call a web service in a C# Windows Forms Application using a WSDL (Web Services Description Language), you can follow these steps:

Open your Visual Studio, then create a simple Windows Forms allows you to insert update delete data from web service.

How to use a Web Service in Windows Form

c# crud web service

C# web service crud operations with entity framework

You need to create a simple web service allows you to get, insert, update, delete data from sql server. If you don't know, you can see this post to know How to Create a Web Service in C#.

Press F5 to run your web service, then open Developer Command Promt for VS 2019 or 2017

wsdl visual command

Entering the command below to generate your web service to class.

wsdl /language:cs https://localhost:[port]/PaymentService.asmx

Copying the class you just generated into your winform project, then add a reference to System.Web.Services.dll

You can also right-click on the project in Solution Explorer, choose "Add" -> "Service Reference". Then, in the dialog that appears, click on "Advanced" and then "Add Web Reference". Here, you can enter the URL of the WSDL file. Visual Studio will generate proxy classes based on the WSDL for you.

After adding the web reference, Visual Studio will generate proxy classes for you. You can now use these classes to interact with the web service methods as if they were local methods.

How to call web service in c# desktop application

Rebuilding your project, then add a bindingsource to the DataGridView to Payment class.

You can invoke methods of the web service using the generated proxy classes. The methods will have the same names as defined in the WSDL.

Declaring a PaymentService allows you to call your web service.

PaymentService payment;

Adding a Load event handler to your form allows you to call GetAll method from web service.

private void frmPayment_Load(object sender, EventArgs e)
{
    payment = new PaymentService();
    paymentBindingSource.DataSource = payment.GetAll();
}

Call web service c# with parameters

Adding a click event handler to the Insert button allows you to insert data to web service.

private void btnInsert_Click(object sender, EventArgs e)
{
    payment.Insert(txtFullName.Text, Convert.ToDecimal(txtAmount.Text), txtNote.Text);
    Clear();
    paymentBindingSource.DataSource = payment.GetAll();
}

Adding a click event handler to the Update button allows you to call Update method from web service to update data.

private void btnUpdate_Click(object sender, EventArgs e)
{
    paymentBindingSource.EndEdit();
    Payment obj = paymentBindingSource.Current as Payment;
    if (obj != null)
    {
        payment.Update(obj.Id, obj.FullName, obj.Amount ?? 0, obj.Note);
        Clear();
        paymentBindingSource.DataSource = payment.GetAll();
    }
}

Adding a Clear method allows you to clear textbox data.

void Clear()
{
    txtFullName.Text = string.Empty;
    txtAmount.Text = string.Empty;
    txtNote.Text = string.Empty;
}

Adding a click event handler to the Delete button allows you to call Delete method from web service.

private void btnDelete_Click(object sender, EventArgs e)
{
    Payment obj = paymentBindingSource.Current as Payment;
    if (obj != null)
    {
        if (MessageBox.Show("Are you sure want to delete this record?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
        {
            payment.Delete(obj.Id);
            Clear();
            paymentBindingSource.DataSource = payment.GetAll();
        }
    }
}

Through this c# consume soap web service example, i showed you how to consume web service in windows application c#.