Windows Forms: CRUD Web Service Call Function In C#
By FoxLearn 9/18/2020 6:02:49 AM 8.25K
Creating a simple Windows Forms allows you to insert update delete data from web service.
How to use a Web Service in Windows Form
C# web service crud operations with entity framework
First, 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
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
How to call web service in c# desktop application
Rebuilding your project, then add a bindingsource to the DataGridView to Payment class.
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#.