How to Display data using Generic Handler(.ashx) in Silverlight

Posted by Sudhak under Silverlight on 5/4/2010 | Views : 7806 | Status : [Member] | Replies : 1
Hi,

I want to display data from database using handler in silverlight application ., i.e display data into silverlight datagrid. How I can make a call to this handler in silverlight application

My handler(.ashx) file looks like the below one

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace CustomDatagrid.Web
{
/// <summary>
/// Summary description for Handler2
/// </summary>
public class Handler2 : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
GetSchoolData();
}
public DataSet GetSchoolData()
{
SqlConnection cn = new SqlConnection("data source=hits20\\sqlexpress;integrated security=sspi;initial catalog=EducareDBReArc");
SqlCommand command = new SqlCommand("select id,school_id from tblschoolimage", cn);
SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;

}

public bool IsReusable
{
get
{
return false;
}
}
}
}

How i can return this dataset to silverlight application and try to bind to the datagrid?

Thanks,
Sudha




Responses

Posted by: Exhaustpipe on: 5/25/2010 [Member] Starter

Up
0
Down
Short answer:
You can not.

Longer answer:
Silverlight does not work with DataSet(s), DataTable(s), and the like.
What you can do is return xml with that handler, parse it, and then use it as a source for a grid.

You can use this code in your handler:

SqlConnection cn = new SqlConnection("data source=hits20\\sqlexpress;integrated security=sspi;initial catalog=EducareDBReArc");
SqlCommand command = new SqlCommand("select id,school_id from tblschoolimage FOR XML AUTO, ELEMENTS, ROOT('Root') ", cn);
cn.open();
XmlReader xmlRead = command.ExecuteXmlReader();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlRead);
string strText = xmlDoc.OuterXml;
context.Response.ContentType = "text/xml";
context.Response.Output.Write(strText);
xmlRead.Close();
con.Close();

That way your handler will return that xml in string format as a context response, and you can "capture it" using HttpWebRequest or WebClient (very hard to use this one because of security override you need to perform, which is not considered good thing anyway)...

I will leave parsing of that string to you :)

Best regards, Ex.

Sudhak, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response