Subhash Sharma

Subhash Sharma
Subhash Sharma

This is Subhash Sharma(Software Engineer) Blog

Welcome to this blog and find every solution.............

Search This Blog

Software Engineer(Subhash Sharma)

Software Engineer(Subhash Sharma)
Software Engineer

Thursday, January 20, 2011

webservices Bind GridView

//connectionStrings>
add name="Master" connectionString="Data Source=SF-74\SQLEXPRESS;user id=;pwd=;Trusted_Connection=true" />

/connectionStrings>

//use some NameSpaces

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Xml;
using System.Data;

[WebMethod]
public XmlDocument GetDataFromDB()
{
string errorMessage = "";
XmlDocument myDatas = new XmlDocument();
//Connection string is stored in the web.config file as an appSetting
string connectionString = ConfigurationManager.ConnectionStrings["Master"].ConnectionString;
SqlConnection dbConnection = null;
// Open a connection to the database
try
{
dbConnection = new SqlConnection(connectionString);
dbConnection.Open();
}
catch (Exception ex)
{
errorMessage = ex.Message;
}
if (errorMessage == "")
{
// Build an insert command
string SQL = "select * From sa";
SqlCommand GetCustomerCmd = new SqlCommand(SQL, dbConnection);

try
{
SqlDataAdapter custDA = new SqlDataAdapter();
custDA.SelectCommand = GetCustomerCmd;
DataSet custDS = new DataSet();
custDA.Fill(custDS, "ProfileContact");
myDatas.LoadXml(custDS.GetXml());
dbConnection.Close();

}
catch (System.Exception ex)
{
errorMessage = ex.Message;

}
finally
{
dbConnection.Dispose();
}
}
return myDatas;
}


////////////aspx.cs

localhost.WebService2 proxy = new WebApplication1.localhost.WebService2();

protected void Page_Load(object sender, EventArgs e)
{
XmlDocument myServiceDoc = new XmlDocument();
System.Xml.XmlNode neNode;
//Adding the resulting XML from WebMethod to a user created XmlNode
neNode = proxy.GetDataFromDB();
//Creating a Dataset
DataSet myDataSet = new DataSet();
//The XmlNode is added to a byte[]
byte[] buf = System.Text.ASCIIEncoding.ASCII.GetBytes(neNode.OuterXml);
System.IO.MemoryStream ms = new System.IO.MemoryStream(buf);
//The XML is readed from the MemoryStream
myDataSet.ReadXml(ms);

GridView1.DataSource = myDataSet.Tables[0];
GridView1.DataBind();

//GridView1.DataSource = proxy.GetDataFromDB();
//GridView1.DataBind();

}

No comments:

Post a Comment