The following code snippet demonstrate how we can use web method to fill the dataset in asp.net.
1. Web Service Code: Write the FillGrid() function in your webservice.
[WebMethod] public XmlDocument FillGrid() { string con = System.Configuration.ConfigurationManager.ConnectionStrings["localcon"].ConnectionString; SqlConnection cn = new SqlConnection(con); DataSet ds = new DataSet(); string query = "select top 10 * from Person.Address"; SqlDataAdapter da = new SqlDataAdapter(query, cn); da.Fill(ds); XmlDocument mydata = new XmlDocument(); mydata.LoadXml(ds.GetXml()); return mydata; } }
2. Include web reference to your web site.
3. Use the following code in Page Load of your aspx code file.
XmlDocument MyDoc = new XmlDocument(); TestService.Service ob = new Service(); System.Xml.XmlNode neNaode = ob.FillGrid(); byte[] buf = System.Text.ASCIIEncoding.ASCII.GetBytes(neNaode.OuterXml); System.IO.MemoryStream ms = new MemoryStream(buf); DataSet myset = new DataSet(); myset.ReadXml(ms);
from the above myset is the output dataset with the records.
Thanks
Shobhit