Archive

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


<configuration>
 <system.web>
 <compilation debug="true" targetFramework="4.0" />
 </system.web>
 
 <system.webServer>
 <staticContent>
 <remove fileExtension=".json" />
 <mimeMap fileExtension=".json" mimeType="application/json" />
 </staticContent>
 </system.webServer>

</configuration>

Advertisements
Categories: ASP.NET Tags: , ,

Ajax call before form submit asp.net c#

July 28, 2013 4 comments

Let me share an issue i faced.

I have a textbox with google auto complete plugin.

Image

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) {
$('#HFLat').val(results[0].geometry.location.lat());
$('#HFLng').val(results[0].geometry.location.lng());
$('#SecondButton').click();
}
});
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 asp.net

July 2, 2013 Leave a comment

Got a simple and nice plugin for uploading multiple files.

Image

Here is my source code http://sdrv.ms/121hk6G

You can see detailed options here : http://www.albanx.com/ajaxuploader/examples.php?e=2

Happy coding 🙂

Markers on map javascript asp.net c#

June 27, 2013 Leave a comment

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

http://sdrv.ms/11LvXye

See live here

http://sicsglobal.co.in/mytestproject/themarker/

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
http://james.newtonking.com/projects/json-net.aspx

   //Deserialize
   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"]));
   list.Add(bs);
   }
  //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";
        }
        else
        {
          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();

        try
        {   //Open connection
            oledbConn.Open();
           // 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.
            oleda.Fill(tab);
        }
        finally             '
        {
           oledbConn.Close();
        }

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 GoogleAppID = “XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”; //API Key

string DeviceId= “XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”;  //Device Token

string msg = “Message from ASP.NET”

WebRequest tRequest;

tRequest =

WebRequest.Create(“https://android.googleapis.com/gcm/send“);

tRequest.Method =

“post”;

tRequest.ContentType =

“application/x-www-form-urlencoded;charset=UTF-8”;

tRequest.Headers.Add(

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

dataStream.Close();

WebResponse tResponse = tRequest.GetResponse();

dataStream = tResponse.GetResponseStream();

StreamReader tReader = newStreamReader(dataStream);

String sResponseFromServer = tReader.ReadToEnd();

tReader.Close();

dataStream.Close();

tResponse.Close();

🙂