Posts

Donate

Webclient - The remote server returned an error: (403) Forbidden

Using downloadstring of webclient returns an error as stated by the post title. The solution was to add user agent header to the webclient headers. The value of user agent can be found in the mozilla firebug. Solution: string URL = string .Format(your_url); newWebClient.Headers.Add( "user-agent" , "Mozilla/5.0 (Windows NT 6.1; rv:2.0) Gecko/20100101 Firefox/4.0 "); string pageSource = newWebClient.DownloadString(URL); pageSource = System.Web.HttpUtility.HtmlDecode(pageSource);

How To Show External IP Address Using C# And WebClient

I experimented a snippet to get my ISP provided IP address using whatismyip. Using tutorials or code snippets from links, the web request just returned a 404 error. After reading the whatismyip API again,they have changed their automation URL. Below is the code snippet. //previous URL present on most tutorials which is not working //string whatIsMyIp = "http://www.whatismyip.com/automation/n09230945.asp"; //updated URL from whatismyip API string whatIsMyIp = "http://automation.whatismyip.com/n09230945.asp" ; WebClient wc = new WebClient(); UTF8Encoding utf8 = new UTF8Encoding(); string requestHtml = "" ; requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp));

How To Convert Epoch Time To DateTime In C#

Good morning awesome programmers, Me and my fellow teammate encountered a scenario to convert epoch time to datetime. The solution can be found in stack overflow. private void Form1_Load( object sender, EventArgs e) { //long tms = 1308139229; long tms = 1308143650; DateTime dt = FromUnixTime(tms); MessageBox.Show(dt.ToShortDateString()); } public DateTime FromUnixTime( long unixTime) { var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); return epoch.AddSeconds(unixTime); }

Sorting List<T> Generic Collection String In C#

This example uses List generic collection and will sort names in descending order. Names with New will be displayed on top of the list. This is applicable if you will be displaying products names with NEW in it's product name as recently displayed. List< string > Names = new List< string >(); private void FListSort_Load( object sender, EventArgs e) { Names.Add( "Mike" ); Names.Add( "John" ); Names.Add( "Titch" ); Names.Add( "Harold" ); Names.Add( "Klent" ); Names.Add( "Thomas New" ); Names.Add( "Mary" ); Names.Add( "Fultron New" ); Names.Add( "Khayce" ); Names.Add( "Tim" ); Names.Add( "Joker New" ); Names.Add( "Linda" ); Names.Add( "Arthur New" ); Names.Add( "Baby Lee Jones...

Posting Data To WebBrowser Control In C#

Good day! Basically, it is possible to post data to web browser control using C#. The code below, was referenced from MSDN, but i slightly modified some of it's functionalities. string url = "http://example.com" ; string postData = String.Format( "city=DC&Page={0:00}" ,pageNum); byte [] Post = Encoding.UTF8.GetBytes(postData ); string AdditionalHeaders = "Content-Type: application/x-www-form-urlencoded" ; //wbSample is the web browser control //the WebNavigate method is just a simple method i created //which simply assigns url to the browser, the post data and additional headers WebNavigate(wbSample , ListURL, "" , Post, AdditionalHeaders);

JQuery Calculator Example In ASP.NET Web Forms

Image
The image above is a jquery/javascript calculator developed in ASP.NET 4.0 web template. Here's the functions. I'wont be posting all codes since it will took up space in my post. I'll just post the sqrt() and a number scripts. The ASPX markup uses plain css for centering and aligning the buttons and html controls. No asp.net server controls are involved. For the jquery processing, im using the id selector. /* show zero to textbox on page load */ $( "#txtCalc" ).ready( function () { $( "#txtCalc" ).val( "0" ); }); //square root $( "#btnSqrt" ).click( function () { var text = $( "#txtCalc" ).val(); //if invalid input,do not execute codes below if (text.search( "Invalid" ) != -1) { return ; } if (text.length == 1) { if (text == "0" ) { return ; } ...

Random Class In C#.NET Not Generating Proper X And Y Coordinates

I am creating a snippet to change x and y coordinates upon runtime. However,not as i expected,the generated random numbers does not meet my requirements. Here is the C# snippet: switch (odd_eve) { case 0: Random rand = new Random(); count = rand.Next(0, 300); break ; case 1: Random rand1 = new Random(); count = rand1.Next(310, 600); break ; default : } Based from the code above,the random generates a slanting x and y coordinates. The solution i come up came from the idea of a fellow developer to declare one Random object in one class. Random rand = new Random(); //declared as global variable //function snippet switch (odd_eve) { case 0: count = rand.Next(0, 300); break ; case 1: count = rand.Next(310, 600); break ; default : } Works like a charm!!!

How To Debug JavaScript Code In Visual Studio 2008 Or Visual Studio 2010

Here are the simple steps to debug js code in your aspx markup. 1. Open Visual Studio 2008/2010 2. Click Tools then options 3. Expand debugging node then click checkboxes Manages,Native,Script. 4. In your web.config file,make sure to set compilation debug to true. <compilation debug= "true" > <assemblies> <add assembly= "System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /> <add assembly= "System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <add assembly= "System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /> <add assembly= "System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /> </assemblies> </compilation> 5. Choo...

Microsoft SQL Server, Error 14234

Lately, I installed an SQL Server 2005 database on my clients server machine,and when i created a database maintenance plan,i got the error as stated: The specified '@subsystem' is invalid (valid values are returned by sp_enum_sqlagent_subsystems). (Microsoft SQL Server, Error 14234) The solution is to install SQL Server 2005 Integration Services.

"Installation Incomplete - The installer was interrupted before could be installed. (Visual Studio Web Setup Project)

Hello! Recently, the ASP.NET Web Forms software i maintained crashed down and can't be opened by the browsers (IE,Firefox and chrome). I tried different kinds of solutions but they don't work. So,i tried the steps below to re-install the web setup package with the error provided as the title of the post. Culprit: virus attack[dangerous or high risk worm/trojan] Solution: 1. Repair windows xp sp2 (using installer cd) XP Repair Install Note: I performed only steps 1-6 2. After repairing, I uninstalled the IIS 3. Delete the current Inetpub folder in Drive C:\ [make sure to backup any files in the Inetpub folder] 4. Then restart PC after performing steps 2-3 5. Re-install IIS using windows xp sp2 cd 6. Register asp.net 2.0 from command prompt c:\windows\microsoft.net\framework\v2.0.50727>aspnet_regiis -i 7. Restart PC/Server 8. Continue Installing VS Web Setup Project Note: This may also apply to Windows 2003 server or new Windows OS.

Donate