Click here to Skip to main content
15,916,412 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I have a windows form with some textboxes and dropdownlists and a gridview.The datagrid in binded using a stored procedure from a web service.The web service is added in another web application.I have added the web reference. But I don't know how to pass the parameters from windows form to web service. Please help me.I am new to c#.Thanks in advance.

Correct me if have done anything wrong in my code since I am new to c#.

What I have tried:

 private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string productname = "All", FromDate = dateTimePicker1.Text, Todate = dateTimePicker2.Text, IN02 = "All", IN01 = "All", IN04 = "All", Type = "All";
                if (txt_product.Text.Trim() != "")
                {
                   productname = txt_product.Text.Trim();
                }
                if (ddl_pa.SelectedValue.ToString().Trim() != "")
                {
                    IN02 = ddl_pa.SelectedValue.ToString().Trim();
                }
                if (ddl_Pr.SelectedValue.ToString().Trim() != "")
                {
                    IN01 = ddl_Pr.SelectedValue.ToString().Trim();
                }
                if (txt_batn.Text.Trim() != "")
                {
                    IN04 = txt_batn.Text.Trim();
                }
                if (ddl_Type.SelectedValue.ToString().Trim() != "")
                {
                   Type = ddl_Type.SelectedValue.ToString().Trim();
                }
                productdata.product  productservice = new productdata.product();
//here I need to call the the web method and pass parameters
}
 catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }



This is my webmethod


[WebMethod]
  public DataSet GetData(string productname, string FromDate, string Todate, string IN02, string IN01, string IN04, string Type)
    {
        DataSet ds = objcon.GetDataSet("exec SP0128");
        return ds;

    }
Posted
Updated 26-Oct-17 18:49pm
v3
Comments

1 solution

You need to first create a service reference from your winform app In visual Studio right click on your project and click > Add Service Reference

From there you put the address of your service (url) in the address textbox and you can get the methods on the service , give your service a proper name using the box at the bottom , then click OK , this will generate libraries for you to interact with your service.

Then you can use
Servicenamespace being the name you specified when adding your service reference.
servicenamespace.Client client = new servicenamespace.Client();
client.GetData(.......);


Footnote:
Keep in mind your web method in the code specified above does not do anything with the variables parsed to it , I would highly recommend reading up on EntityFramework for interacting with databases.

When comparing strings dont use == or != , rather use .Equals(""); Its a far more flexible and nearter methods and for checking empty strings use string.IsNullOrEmpty("Stringvalue");
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900