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();

}

Method Overloading in WebServices

When u overload a method than replace
//[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[WebServiceBinding(ConformsTo = WsiProfiles.None)]


[WebMethod]
public int Add(int i, int j)
{
return i + j;
}
[WebMethod(MessageName = "Add2")]
public int Add(int i, int j, int k)
{
return i + j + k;
}

when u overload a method u hve to give the Mesaage name that would be unique from ur method name...

aspx.cs page

localhost.WebService1 proxy = new WebApplication1.localhost.WebService1();
proxy.Add(5,4,8);