将参数从Windows窗体传递到C#中的Web服务 [英] Pass parameters from a windows form to web service in C#

查看:95
本文介绍了将参数从Windows窗体传递到C#中的Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个带有一些文本框和下拉列表以及gridview的窗体。使用来自Web服务的存储过程绑定的数据网格。在另一个Web应用程序中添加了Web服务我添加了网络参考。但我不知道如何将参数从Windows窗体传递给Web服务。请帮助我。我是c#的新手。提前谢谢。



如果我的代码中有任何错误,请纠正我,因为我是c#的新手。



我的尝试:



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);
            }







这是我的webmethod








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;

    }

推荐答案

您需要先从winform应用程序创建服务引用In visual Studio右键单击您的项目,然后单击>添加服务参考



从那里您将服务地址(网址)放在地址文本框中,您可以获得服务上的方法,为您的服务提供合适的服务使用底部的框命名,然后单击确定,这将生成库以供您与服务进行交互。



然后您可以使用

Servicenamespace是您在添加服务引用时指定的名称。

servicenamespace.Client client = new servicenamespace.Client();

client.GetData(.... ...);





脚注:

请记住上面指定的代码中的web方法对解析它的变量不做任何事情,我强烈建议您阅读EntityFramework以与数据库进行交互。



比较字符串时不要使用==或!= ,而是使用.Equals();它是一个更灵活,更灵活的方法,用于检查空字符串使用string.IsNullOrEmpty(Stringvalue);
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");


这篇关于将参数从Windows窗体传递到C#中的Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆