Returning JSON with ASP.NET and $.getJSON - asp.net

I have a page that connects to the Northwind database to get a list of categories...
protected void Page_Load(object sender, EventArgs e)
{
var cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnnString"].ConnectionString);
var cmd = new SqlCommand("SELECT CategoryID, CategoryName FROM Categories ORDER BY CategoryName", cnn);
using (cnn)
{
cnn.Open();
var dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (dr.HasRows)
{
var sb = new StringBuilder();
var sw = new StringWriter(sb);
using (JsonWriter jw = new JsonTextWriter(sw))
{
jw.Formatting = Formatting.Indented;
jw.WriteStartArray();
while (dr.Read())
{
jw.WriteStartObject();
jw.WritePropertyName("CategoryID");
jw.WriteValue(dr.GetInt32(0));
jw.WritePropertyName("CategoryName");
jw.WriteValue(dr.GetString(1));
jw.WriteEndObject();
}
jw.WriteEndArray();
jw.Flush();
Response.Write(sb);
}
}
}
}
This works just as expected. In my calling page I have a dropdownlist and I'm trying to use jQuery to populate it. Maybe I'm doing this wrong. I'm assuming that it takes the entire text from the .aspx file which includes the and strings and causes it to be invalid? If so, what is the correct way to do this, or what is it that I am doing wrong?
$.getJSON('GetCategories.aspx', function (data) {
alert('success!');
$.each(data, function (key, val) {
$('#ddlCategories').append('<option></option>').val(key).html(val);
});
});
The alert never happens so it craps out getting the file. The file name is correct as Chrome Inspector gives me no console errors.

You should look into HttpHandlers, as they have much lower overhead and can be restricted to just the output you want.
I use a HttpHandler as an example in another answer here Accessing Image from App_data folder

Related

ASP.NET Cache always returns null

I am using SQLCacheDependency in my ASP.NET application with Query Notifications.
I followed this article to set up my database with success.However whenever I am trying to store data in the cache object.It just does not hold value.It is always null .I am not getting any errors or exceptions.
Here is my code
Global.asax
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
System.Data.SqlClient.SqlDependency.
Start(ConfigurationManager.ConnectionStrings["McdConn"].ToString());
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
System.Data.SqlClient.SqlDependency.
Stop(ConfigurationManager.ConnectionStrings["McdConn"].ToString());
}
public static class CacheManagement
{
public static DataTable CreateCache(string cacheName, string tableName, string query)
{
DataTable dtResult = new DataTable();
try
{
string connectionString = ConfigurationManager.ConnectionStrings["McdConn"].ToString();
dtResult = HttpContext.Current.Cache[cacheName] as DataTable;
if (dtResult == null)
{
dtResult = new DataTable();
using (var cn = new SqlConnection(connectionString))
{
cn.Open();
var cmd = new SqlCommand(query, cn);
cmd.Notification = null;
cmd.NotificationAutoEnlist = true;
SqlCacheDependencyAdmin.EnableNotifications(connectionString);
if (!SqlCacheDependencyAdmin.GetTablesEnabledForNotifications(connectionString).Contains(tableName))
{
SqlCacheDependencyAdmin.EnableTableForNotifications(connectionString,tableName);
}
var dependency = new SqlCacheDependency(cmd);
//SqlDataAdapter ad = new SqlDataAdapter(cmd);
//ad.Fill(dsResult);
SqlDataReader reader = cmd.ExecuteReader();
dtResult.Load(reader);
HttpContext.Current.Cache.Insert(cacheName, dtResult, dependency);
}
}
}
catch (Exception ex)
{
Exception_Log.ExceptionMethod("Web", "CacheManagement.cs", "CacheManagement", ex);
}
return dtResult = HttpContext.Current.Cache[cacheName] as DataTable;
}
}
Code Behind
var dtCachedCategories = HttpContext.Current.Cache["tbl_CategoryMaster_Cached"] as DataTable;
if (dtCachedCategories == null)
{
dtCachedCategories = CacheManagement.CreateCache("tbl_CategoryMaster_Cached","dbo.tbl_CategoryMaster_Languages", "Select * from dbo.tbl_CategoryMaster_Languages");
}
The above always returns null.
Can anyone help me in pointing out what could be missing?
Well there's a lot you can do to debug your code and arrive at a conclusion. It seems like your cached item is getting removed too frequently.
1.) Use CacheItemPriority.NotRemovable to Cache.Insert() to make sure ASP.NET doesn't removes
your item whenever it feels so. use the Insert() method explained here. Check this MSDN
article too.
2.) To find out the reason why your cached item is getting removed , log this removal action using
CacheItemRemovedCallback delegate option of your Cache.Insert() method. Check this Insert method
overload version and also this link.
3.) Make sure your dtresult as well as your reader is not null. Check the lines:
SqlDataReader reader = cmd.ExecuteReader(); & dtResult.Load(reader); , together with your logs.
4.) Check your application Pool recycle time. This link has everything related to App pool settings ( IIS 7 +).
5.) This link has a solution for App pool of IIS 6: http://bytes.com/topic/net/answers/717129-c-asp-net-page-cache-getting-removed-too-soon
Also, try using HttpRuntime.Cache method to see if it works.
System.Web.HttpRuntime.Cache.Insert(cacheName, dtResult, dependency);

trouble using httphandler to retrieve image url

I am trying to write a httphandler to retrieve image url and later display it on a image control i am using this code but its not working
OleDbConnection mDB = new OleDbConnection(
ConfigurationManager.ConnectionStrings["AccessConnection"].ConnectionString);
mDB.Open();
OleDbCommand cmd = new OleDbCommand("select pProductId from Products where pProductId=" + context.Request.QueryString["ImageID"], mDB);
OleDbDataReader rdr = cmd.ExecuteReader();
rdr.Read();
context.Response.BinaryWrite((byte[])rdr[0]);
mDB.Close();
context.Response.End(); */
sorry that i caused a confusion earlier in the SELECT statement the pProductId does not contain the URL of the image instead pProductImage is the field that contain the URL. i am using the Id to identify which image to display accordingly.
this is my expected output:
<img src="ImgHandler.ashx?pProductId=2" alt="" />
i cant place image this is the link to my error msg:http://imgur.com/Cix67
This is a two steps answer.
In the page, you can do something like this:
using (OleDbConnection mDB = new OleDbConnection(ConfigurationManager.ConnectionStrings["AccessConnection"].ConnectionString))
{
mDB.Open();
using (OleDbCommand cmd = new OleDbCommand("select pProductId from Products where pProductId=" + context.Request.QueryString["ImageID"], mDB))
{
using (OleDbDataReader rdr = cmd.ExecuteReader())
{
rdr.Read();
context.Response.Write("<img src=\"ImgHandler.ashx?pProductId=");
context.Response.Write((int)rdr[0]); // I suppose pProductId is an int, adapt accordingly
context.Response.Write("\" />");
}
}
}
and in the HTTP handler implicitly triggered, something like this:
public class MyHandler : IHttpHandler
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
using (OleDbConnection mDB = new OleDbConnection(ConfigurationManager.ConnectionStrings["AccessConnection"].ConnectionString))
{
mDB.Open();
// select the file data (I put pData here as a placeholder, and assume it will be a byte[])
using (OleDbCommand cmd = new OleDbCommand("select pData from Products where pProductId=" + context.Request.QueryString["ImageID"], mDB))
{
using (OleDbDataReader rdr = cmd.ExecuteReader())
{
rdr.Read();
context.Response.ContentType = "image/png";// put the content type here
context.Response.BinaryWrite((byte[])rdr[0]);
}
}
}
}
}
Note the using pattern which ensures proper resources cleanup once used. Also, ideally you could cache the data on the client using proper cache HTTP headers (like if-modified-since) but this is another story ...

Why ComboBox items need to be reselected to obtain Value?

I am using ext.net 1.3 controls in my ASP.NET 4.0 application. I have several ComboBox controls on my Web Form. The page is supposed to perform two tasks, Insert and Update. There are no issues when a new record is saved, but when I try to fill the ComboBox controls with an existing database values, various issues pop up. The most troubling is this one:
The ComboBox displays the Text from the database, but it neither gets populated nor I am able to pick the ComboBox Value Member. This is because it is not populated. I have written code to populate ComboBox in the Page Load event.
I am using this code to pick a value from the database and show it on the ComboBox:
string Query = "SELECT CustName FROM CustomerMaster WHERE CustID = " + Session["CustomerID"];
var result = DB.Single<Customer>(Query);
CustomerComboBox.setValue = result.CustName;
This code successfully retrieves the Customer Name and displays in the ComboBox. What it is not doing is that it is not selecting from the ComboBox Item List and neither populating the ComboBox.
If I try to retrieve the Value Member of the Text using:
CustomerComboBox.SelectedItem.Value;
it gives error.
To make it work, I need to click on the ComboBox again to make it populate and than I manually select the same customer name from the list to pick the value.
How to get rid of this issue?
-- Edited --
The code to fill ext.net ComboBox is this:
public void FillExtComboList(string ParameterFlag, ComboBox DropDownName)
{
string Query = "";
using (TransactionScope transactionScope = new TransactionScope())
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cncustomer"].ConnectionString.ToString()))
{
con.Open();
SqlDataReader dr;
try
{
if (ParameterFlag == "Customer")
{
Query = "SELECT CustName FROM CustomerMaster";
}
//Check whether the Drop Down has existing items. If YES, empty it.
if (DropDownName.Items.Count > 0)
DropDownName.Items.Clear();
SqlCommand cmd = new SqlCommand(Query, con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
Ext.Net.ListItem extLI = new Ext.Net.ListItem();
extLI.Text = dr[0].ToString();
DropDownName.Items.Add(extLI);
}
dr.Close();
con.Close();
}
catch (Exception ex)
{
con.Close();
// RunCustomScript("alert('" + ex.Message.ToString() + "')", callingPageObjectName);
}
} /* End of SQL Connection */
transactionScope.Complete();
} /* End of Transaction Scope */
}
On Page Load event, the ComboBox control is filled with above method.
I don't see an instruction to fill the combo box, only to set its selected value. Arent you missing a CustomerComboBox.DataSource = someList or something like that?
<-- EDIT -->
Sorry, I thought the setValue was the code on your page load...
OK, this is not be the answer to your problem, but an important performance fix.
you should do this when loading the combo:
when executing the SQL Query:
Query = "SELECT CustID, CustName FROM CustomerMaster"
when filling the combo:
Ext.Net.ListItem extLI = new Ext.Net.ListItem();
extLI.Value = dr["CustId"].ToString();
extLI.Text = dr["CustName"].ToString();
DropDownName.Items.Add(extLI);
so when you want to select an item, you just do this:
CustomerComboBox.setValue = Session["CustomerID"];
and avoid going back to the database to get the customer name.
Now, could you share the code you have to handle the combobox click? Since it does fill the combo, it may throw some ligth to us. Also, try adding
CustomerComboBox.DataBind()
And, come to think of it, I see on Page_Load you use "DropDownName" and later on you use "CustomerComboBox". Could that be the problem?
If I understand you correctly try this code:
protected void Page_Load(object sender, EventArgs e) {
FillExtComboList(DropDownName);
// Set value that you want
DropDownName.SetValueAndFireSelect("Test 3");
}
public void FillExtComboList(ComboBox DropDownName) {
try {
//Check whether the Drop Down has existing items. If YES, empty it.
if (DropDownName.Items.Count > 0)
DropDownName.Items.Clear();
for (int i = 0; i < 10; i++) {
Ext.Net.ListItem extLI = new Ext.Net.ListItem();
extLI.Text = "Test " + i;
DropDownName.Items.Add(extLI);
}
} catch (Exception ex) {
// RunCustomScript("alert('" + ex.Message.ToString() + "')", callingPageObjectName);
} /* End of Transaction Scope */
}

How can i write a datatable content on web page in XMl foramte in asp.net?

How can i write a datatable content on web page in XMl foramte in asp.net?
i also need to customize the datatable before writing to web page.
Edited:-
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "text/xml";
DataTable dt = new DataTable("XML");
String email = EmailAddress.Text.ToString();
dt.Load(obj.GetXML("XYZ#gmail.com"));
//now i want this dt to dosplayed in the XMl form on the same page, how can i achieve this?
'XmlDocument xml = new XmlDocument();
'XmlTextReader xr=new XmlTextReader(
'WriteGoogleMap(dt.ToString(), Response.OutputStream);
'Response.End();
//System.Xml.
}
Try this :
public string GetXml(string urlBase)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
settings.Indent = true;
StringBuilder output = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(output, settings))
{
writer.WriteStartDocument();
writer.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");
// Repeat this code:
writer.WriteStartElement("url");
writer.WriteElementString("loc", "[your url]");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
}
return output.ToString();
}
Then use the result to feed a label :
<script runat="server" type=text/C#>
protected void Page_Load(object sender, EventArgs e)
{
myLabel.Text = HttpUtility.HtmlEncode(
GetXml("http://www.dotnetperls.com/")
);
}
</script>
[Edit]
I think you should start by the beginning. Do not mix all concepts in your single question. Spend time for learning the basics (read books, take courses, etc.). In your specific case, I suggest you to split the work in several layers:
A data layer the is responsible to build a datatable
A business or service layer, capable of creating the xml document from the databable
A presentation layer that will contains :
a custom http handler (.ashx) that will return the xml
a visual page that can show the html representation of the Xml
good luck
DataTable has method named:
WriteXml

Silverlight OOB WebBrowser Exception

I've got an oob app with a webbrowser on it.
The webbrowser source is databound with a URI defined by me. The URI has a path to a webpage from my server that displays a PDF file from its hardrive.
Note that all this is done on a local network.
URI example: uri = new Uri(#"http://ServerName/ProjectName/PDFViewer.aspx?pdf=somePDF.pdf");
Page code-behind:
protected void Page_Load(object sender, EventArgs e)
{
string myURL = Request.Url.ToString();
string[] ParamArray = Regex.Split(myURL, "pdf=");
string Params = ParamArray[ParamArray.Length - 1];
if (Params.Length > 0)
{
Filename = Regex.Replace(Params, #"//", #"\\"); ;
if (File.Exists(Filename))
{
Response.ContentType = "Application/pdf";
Response.WriteFile(Filename); //Write the file directly to the HTTP content output stream.
Response.End();
}
else
this.Title = "PDF Not Found";
}
}
protected void Page_Load(object sender, EventArgs e) { string myURL = Request.Url.ToString(); string[] ParamArray = Regex.Split(myURL, "pdf="); //If the URL has parameters, then get them. If not, return a blank string string Params = ParamArray[ParamArray.Length - 1]; if (Params.Length > 0) { //to the called (src) web page Filename = Regex.Replace(Params, #"//", #"\\"); ; if (File.Exists(Filename)) { Response.ContentType = "Application/pdf"; Response.WriteFile(Filename); //Write the file directly to the HTTP content output stream. Response.End(); } else this.Title = "PDF Not Found"; } }
The first time I set the WebBrowser source everything it displays the PDF. But when I set the URI one second time the app throws an exception: Trying to revoke a drop target that has not been registered (Exception from HRESULT: 0x80040100).
I've done a few tests and here are the results:
1º new Uri(#"http://ServerName/ProjectName/PDFViewer.aspx?pdf=somePDF.pdf");
2º new Uri(#"http://ServerName/ProjectName/PDFViewer.aspx?pdf=someOtherPDF.pdf"); ->error
1º new Uri(#"http://ServerName/ProjectName/PDFViewer.aspx?pdf=somePDF.pdf");
2º new Uri(#"http://www.google.com"); ->error
1º new Uri(#"http://www.google.com");
2º new Uri(#"http://www.microsoft.com");
2º new Uri(#"http://ServerName/ProjectName/PDFViewer.aspx?pdf=somePDF.pdf");
3º new Uri(#"http://ServerName/ProjectName/PDFViewer.aspx?pdf=someOtherPDF.pdf"); ->error
I also forgot to say that when running the app from my browser (using a HTMLHost) the pages display just fine. Opening the pages using a browser will also work well.
It must be some problem with my aspx page. Any ideas?
Pedro
I've managed to resolve this by creating a new browser for each page. If you know of a more elegant solution please share.
I am not sure if I'm following the question/problem correctly but maybe loading the pages async and then assigning to webbrowser? Forgive me if I am off-base here.
public void ShowLink(string linkUrl)
{
if (App.Current.IsRunningOutOfBrowser)
{
var pageRequest = new WebClient();
pageRequest.DownloadStringCompleted += pageRequest_DownloadStringCompleted;
pageRequest.DownloadStringAsync(new Uri(linkUrl, UriKind.Absolute));
}
}
void pageRequest_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
webBrowserLink.NavigateToString(e.Result.ToString());
}

Resources