简体   繁体   English

WebService ASP.NET C#

[英]WebService ASP.NET C#

I have created a WebMethod in a WebService that uses stored procedures to find whatever you are searching for. 我已经在WebService中创建了一个WebMethod,该WebMethod使用存储过程来查找您要搜索的内容。

[WebMethod]
public DataSet getMyData(string search)
{
    using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"))
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("searchingads", conn);
        SqlDataAdapter da;
        DataSet ds = new DataSet();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@search", search);
        da = new SqlDataAdapter(cmd);
        da.Fill(ds, "MyData");

        conn.Close();


        conn.Close();
        return ds;
    }

I don't know how to call this method from an ASP.NET application. 我不知道如何从ASP.NET应用程序调用此方法。 I have a button that, when clicked, needs to call this method and populate a GridView. 我有一个按钮,单击时需要调用此方法并填充GridView。

I have the following code in my ASP.NET web application (on button click): 我的ASP.NET Web应用程序中包含以下代码(单击按钮时):

WebService1 service = new WebService1();

GridView2.DataSource = service.getMyData(TextBox1.Text);
GridView2.DataBind();

Label1.Text = service.HelloWorld();

The label switches to "hello world" when the button is clicked, but it doesn't give me any table when I do a search. 单击该按钮时,标签将切换为“ hello world”,但进行搜索时不会显示任何表格。

Thank you in advance for your help. 预先感谢您的帮助。

Please use this 请用这个

GridView2.DataSource = service.getMyData(TextBox1.Text); GridView2.DataSource = service.getMyData(TextBox1.Text);

instead of 代替

GridView2.DataSource = service.IskanjeOglasov(TextBox1.Text); GridView2.DataSource = service.IskanjeOglasov(TextBox1.Text);

If you have tested your search logic and its returning the data then try by assigning the data table as data source not the data set. 如果您已经测试了搜索逻辑及其返回的数据,请尝试将数据表分配为数据源而不是数据集。

GridView2.DataSource = ((DataSet)service.getMyData(TextBox1.Text)).Tables[0];
GridView2.DataBind();

Here I have removed the check part of dataset. 在这里,我删除了数据集的检查部分。 This might work. 这可能有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM