0

I want to make WebMethod to my WebForms, beforehand this is my function

[WebMethod]
public DataSet GetCustomersData()
{
    using(SqlConnection conn = new SqlConnection("MyConnString"))
    {
        try
        {
            conn.Open();
            string commandstr = "select * from Customers ORDER BY CustomerID";
            SqlCommand cmd = new SqlCommand(commandstr, conn);
            SqlDataAdapter SqlDa = new SqlDataAdapter(cmd);
            DataSet SqlDs = new DataSet();
            SqlDa.Fill(SqlDs, "TableCustomer");
            if(SqlDs.Tables[0].Rows.Count > 0)
            {
                return SqlDs;
            }
        }
        catch (SqlException)
        {

        }
    }
}

Actually I want to BindData from WebServices to DataGridView in my WebForm, is there any possible way ?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
gema
  • 523
  • 1
  • 7
  • 17
  • DataSet contains a lot of addtional data that would be transferred over the network. (i.e XSD schema etc.) So I would create a list based on the items in the DataSet and returned just this list. – Piotr Piotr Apr 30 '15 at 14:20
  • ASMX is a legacy technology, and should not be used for new development. WCF or ASP.NET Web API should be used for all new development of web service clients and servers. One hint: Microsoft has retired the [ASMX Forum](http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/threads) on MSDN. – John Saunders May 01 '15 at 12:21
  • thank you for your help, therefore I should learn more advance Restful Architecture using ASP.NET. – gema Apr 12 '16 at 11:54

2 Answers2

1

You should create a POCO class and return a list of this class instead. Dataset contains a lots of aditional data.

Community
  • 1
  • 1
Oscar
  • 13,594
  • 8
  • 47
  • 75
0

If you want to use web service methods in your application, you have to separate web service project and web project. Here are the steps you should follow.

  • Create a web service project.
  • Write you web method in your project.
  • Publish your web project.
  • Host your web project in a server. This step will allow you to reach your methods like http://www.example.com/Service1.asmx

When your web service is ready, you should follow these steps. - Create a web application project that contains your web pages. - Add the web service, you created, to your web application project as reference. - Use the web service method to bind data to your datagridview.

Omer Harmansa
  • 29
  • 1
  • 5