简体   繁体   中英

WebService ASP.NET C#

I have created a WebMethod in a WebService that uses stored procedures to find whatever you are searching for.

[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. I have a button that, when clicked, needs to call this method and populate a GridView.

I have the following code in my ASP.NET web application (on button click):

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.

Thank you in advance for your help.

Please use this

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

instead of

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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