Wednesday, September 26, 2007

Binding Data Source for asp DropDownList with Dictionary

While I was trying to bind data source (which is dictionary) I got a way to bind data.

The drop down control in .aspx page




DataBinding in .aspx.cs

Dictionary cityDataList = new Dictionary();
cityDataList.Add("DEL", "Delhi");
cityDataList.Add("BOM", "Mumbai");
cityDataList.Add("MAA", "Chennai");
cityDataList.Add("GOI", "Goa");

// Binding dictionary data with DropDownList
foreach (string key in cityDataList.Keys)
{
CityList.Items.Add(new ListItem(cityDataList[key], key));
}

Monday, July 09, 2007

Catch Error Response xml in case of web services exception

While I was working on web services consumption, there was a issue in web services that if I send any wrong request xml, web service server send a web exception "(500) Internal Server Error." But the actual response xml was not catched through it.
For catching this I was using HTTP Debugger Pro. But HTTP Debugger has one drawback that we cannot use it in release version (It is used only for developement server.) :(
Then I try to catch this issue at the programming logic.

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(requestUrl);

req.Method = "POST";
req.ContentLength = request.Length;
req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Headers.Add("SOAPAction", headerUrl);
req.KeepAlive = false;

Stream str = req.GetRequestStream();
StreamWriter writer = new StreamWriter(str);
writer.Write(request);
writer.Flush();
Stream resp = Stream.Null;
WebResponse response = null;
try
{
response = req.GetResponse();
resp = response.GetResponseStream();
}
catch (WebException ex)
{
HttpWebResponse errResp = ex.Response as HttpWebResponse;

if (errResp == null)
{
throw new WebException("Response was null");
}
using (StreamReader reader = new StreamReader(errResp.GetResponseStream()))
{
string error = reader.ReadToEnd();

throw new WebException(ex.Message + "Error response XML: " + error);
}
}



And Guys it works :)