Archive for the ‘ASP.NET’ Category

HTTP Error 404.3 – Not Found The page you are requesting cannot be served because of the extension configuration.

April 21, 2015 Leave a comment

Resolve this MIME issue by adding the extension in web.config

 <compilation debug="true" targetFramework="4.0" />
 <remove fileExtension=".json" />
 <mimeMap fileExtension=".json" mimeType="application/json" />


Categories: ASP.NET Tags: , ,

Ajax call before form submit c#

July 28, 2013 4 comments

Let me share an issue i faced.

I have a textbox with google auto complete plugin.


I need to store the place i typed in, its latitude, longitude in to the database.

So google have the javascript api to get the latitude and longitude, but it should be called before the form button click to store that in Hidden fields(my way), so i can get the values in server side(c#).

I tried the OnClientClick, but the hidden fields are not set with lat and long. The form is always submitting before the result is came from the ajax call :(.

I found a solution.

var geocoder;
function SetLatLng() {
geocoder = new google.maps.Geocoder();
           var address1 = document.getElementById("location1").value;
geocoder.geocode({ 'address': address1 }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
return false;
<asp:Button ID="FirstButton" OnClientClick="SetLatLng(); return false;" runat="server" Text="Click" />
<asp:Button ID="SecondButton" OnClick="buttonSubmit_Click" runat="server" Text="Click" style="display:none" />
<asp:HiddenField ID="HFLat" runat="server" Value="" />
<asp:HiddenField ID="HFLng" runat="server" Value="" />

Make 2 buttons, one for running the javascript, if the value is returned from the ajax call, click the second button programmatically 😛

Happy coding 🙂

Awesome plugin for multiple file upload ajax

July 2, 2013 Leave a comment

Got a simple and nice plugin for uploading multiple files.


Here is my source code

You can see detailed options here :

Happy coding 🙂

Markers on map javascript c#

June 27, 2013 Leave a comment

Sometimes we gets trouble to mix Javascript and C#.
Here is my source to display dynamic markers on google map.

See live here

I called an aspx page to get some places as C# List, and convert the list to Javascript array from the ajax result.
So we can show markers on map using the normal google api functions.


Serialize and Deserialize JSON using JSON.NET

May 22, 2013 Leave a comment

An awesome tool to convert json string to objects and back to json – JSON.NET
Get it from here

   WebClient wc = new WebClient();         
   string jsonStr = wc.DownloadString("http://example/api/GetAllBrandIds.aspx");
   JObject obj = JObject.Parse(jsonStr);
   JToken jt = obj["IdType"];
   BrandSet bs;
   List<BrandSet> list = new List<BrandSet>();
   foreach(JToken j in jt)
   bs = new BrandSet(); 
   bs.ID = Convert.ToInt32((string)(j["Id"]));                
   bs.ImageType = Convert.ToInt32((string)(j["ImageType"]));
  //Serialize back to json
  string jsonStr = JsonConvert.SerializeObject(list);

Happy coding:)

Read Excel file from ASP.NET C#

May 15, 2013 1 comment

Really excited to get a snippet that reads the excel file as database table:)

Sample code below

        string connStrExcel = "";
        if (type.Equals("xlsx"))
          connStrExcel = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=YourExcelFilePath.xlsx;Extended Properties=Excel 12.0";
          connStrExcel = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=YourExcelFilePath.xls;Extended Properties=Excel 8.0";
        OleDbConnection oledbConn = new OleDbConnection(connStrExcel);
        DataTable tab = new DataTable();

        {   //Open connection
           // Create OleDbCommand object and select data from worksheet Sheet1
            OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn);
           // Create new OleDbDataAdapter
            OleDbDataAdapter oleda = new OleDbDataAdapter();
            oleda.SelectCommand = cmd;
            // Fill the DataTable from the data extracted from the worksheet.
        finally             '

Happy Coding:)

Categories: ASP.NET Tags:

Android Push Notification ASP.NET C#

April 23, 2013 2 comments

The below code will send push notification to an android phone using ASP.NET C#



string msg = “Message from ASP.NET”

WebRequest tRequest;

tRequest =


tRequest.Method =


tRequest.ContentType =



string.Format(“Authorization: key={0}”, GoogleAppID));

String collaspeKey = Guid.NewGuid().ToString(“n”);

String postData = string.Format(“registration_id={0}&data.msg={1}&collapse_key={2}”, DeviceID, msg, collaspeKey);

Byte[] byteArray = Encoding.UTF8.GetBytes(postData);

tRequest.ContentLength = byteArray.Length;

Stream dataStream = tRequest.GetRequestStream();

dataStream.Write(byteArray, 0, byteArray.Length);


WebResponse tResponse = tRequest.GetResponse();

dataStream = tResponse.GetResponseStream();

StreamReader tReader = newStreamReader(dataStream);

String sResponseFromServer = tReader.ReadToEnd();





IPhone Push Notification from ASP.NET C# Moon APNS

April 18, 2013 3 comments

Consider a scenario, I need to send a push notification to IPhone when I add a new card (card id and card name) at back end(from ASP.NET C# Web application)

At First, reference these Moon APNS dlls.

Try the code below, it fetches all the iphone device ids from table, and send message to each devices.

internal void SendPushNotificationIPhone(int CardId, string CardName, string Path)




DataTable tab = datalogic.GetDeviceTokens(); // Getting all device ids from the table

var p = new List<NotificationPayload>();

for (int i = 0; i < tab.Rows.Count; i++)


string message = “Card Added”;

var payload = new NotificationPayload(tab.Rows[i][0].ToString(), message, 1, “default”);

payload.AddCustom(“ID”, CardId);   // Custom fields as id and card name

payload.AddCustom(“CardName”, CardName);



var push = new PushNotification(true, Path, “password”);

//path is iphone app’s p12 certificate file, put password for that certificate also,

var rejected = push.SendToApple(p);  // error list

foreach (var item in rejected)






catch (Exception ep)


//  Error



Categories: ASP.NET

Sort json Array with LINQ and JSON.NET

March 2, 2013 Leave a comment

string jsonStr = dal.GetUsersByType(userId, userType);
JObject json = JObject.Parse(jsonStr);

IEnumerable<JToken> users = from p in json[“Users”]
orderby p[“Firstname”]
select p;

return JsonConvert.SerializeObject(users);

I got it. Some complex join queries may not allow you to sort (order by) a particular column.

So my way is , first get the json array, convert to Json object using JSON.NET.

Then sort it using LINQ and serialize back to json string.

Sample code is shown above.

Happy Coding 🙂

Categories: ASP.NET Tags:

DataReader to XML

January 19, 2013 Leave a comment

code snippet to convert reader to XML

reader = comm.ExecuteReader();

DataTable dataTable = new DataTable();

//convert the datatable to xml
StringWriter xmlWriter = new StringWriter();


Categories: ASP.NET Tags: ,