[WebMethod(Description = "Get all customers from customer table")]
public DataSet GetLatestCustomers()
{
using (SqlConnection connection = new SqlConnection
(ConfigurationManager.ConnectionString"dataConnectionString"].ConnectionString))
{
string Query = "SELECT * FROM [Customers] ORDER BY [CustomerID] DESC";
SqlCommand command = new SqlCommand(Query, connection);
command.CommandType = CommandType.Text;
connection.Open();
SqlDataReader reader = command.ExecuteReader();
DataTable myTable = new DataTable("myTable");
myTable.Columns.Add("CustomerID", typeof(string));
myTable.Columns.Add("CompanyName", typeof(string));
myTable.Columns.Add("ContactName", typeof(string));
myTable.Columns.Add("ContactTitle", typeof(string));
myTable.Columns.Add("Address", typeof(string));
myTable.Columns.Add("City", typeof(string));
myTable.Columns.Add("Region", typeof(string));
myTable.Columns.Add("PostalCode", typeof(string));
myTable.Columns.Add("Country", typeof(string));
myTable.Columns.Add("Phone", typeof(string));
myTable.Columns.Add("Fax", typeof(string));
while (reader.Read())
{
myTable.Rows.Add(new object[]
{
reader["CustomerID"].ToString(), reader["CompanyName"].ToString(),
reader["ContactName"].ToString(), reader["ContactTitle"], reader
["Address"], reader["City"], reader["Region"], reader["PostalCode"],
reader["Country"],reader["Phone"], reader["Fax"] });
}
//myTable.Load(reader);
myTable.AcceptChanges();
DataSet ds = new DataSet();
ds.Tables.Add(myTable);
ds.AcceptChanges();
return ds;
}
}
Put your class in the App_Code folder. I am using a GridView control to show data on the page.