29 June 2011

Maximum Request Length Exceeded ASP.NET

I got this error Maximum request length exceeded while i was trying to upload several files, or a single big size file. As default, max file upload size is 4MB.
We can easily have a solution by not touching our asp.net c# source code.

Just add a single line of code in your Web.config file, and you are done.

<configuration>
  <system.web>
    <httpRuntime maxRequestLength="32768" />
  </system.web>
</configuration>


Now we will not get maximum request length exceeded error in asp.net.

14 April 2011

C# Convert String to Integer

Converting string to int is simple in c#. There are two comman ways to convert string to int.
  • Convert.ToInt32
  • Int32.TryParse
Here is the screen shot of our string to int converter sample application:


Note that our integer number must be between −2,147,483,648 and +2,147,483,647 range.

The c# source code to convert string to integer:
private void btnConvert_Click(object sender, EventArgs e)
{
int numValue = 0;
string strValue = string.Empty;

strValue = tbInput.Text.Trim();

try
{
numValue = Convert.ToInt32(strValue);
lblResult.Text = numValue.ToString();
}
catch (Exception exc)
{
tbInput.Text = string.Empty;
lblResult.Text = string.Empty;
MessageBox.Show("Error occured:" + exc.Message);
}
}       

private void btnTryParse_Click(object sender, EventArgs e)
{
ConvertToIntTryParse();
}

private void ConvertToIntTryParse()
{
int numValue = 0;
bool result = Int32.TryParse(tbInput.Text, out numValue);
if (true == result)
lblResult.Text = numValue.ToString();
else
MessageBox.Show("Cannot parse string as int number");
}

I think IntTryParse is better to convert string to int, because it handles the parse error inside and returns bool value if the string is parsable to int.
IntTryParse return boolean value, so that if it parses the string it returns true, else returns false.

Note the parsing string with Convert.ToInt32 may cause a FormatException if the parse string value is not suitable.

06 December 2010

Fix MySQL Character Encoding Problem UTF-8 PHP

While gathering data from Mysql database i got character encoding problems when my tables have non-english text.
I found a solution that completly solves character encoding problem while you calling your sql queries.

Simply set your character set before you call a sql query.
$myconnection= mysql_connect(DB_SERVER, DB_USER, DB_PASS)
or die(mysql_error());
mysql_select_db(DB_NAME, $myconnection) or die(mysql_error());
mysql_set_charset('utf8',$myconnection);

mysql_set_charset method sets your character set before the calls.
Don't forget to encode your php files to utf-8, or what encoding you want to encode with.

25 May 2009

ASP.NET How To access Master Page Variables Class Members C#.NET

Sometimes we need to access to a variable in master page file.
For example I need to change the body onload event in html.

In the asp.net file, we simply cast our master page as its class name.

in master page aspx we add the code below.

<body onload="<%=myloadstr %>" >

in master page cs code we add the code below.
we declare it as public to access the variable.

public string myloadstr = "";


And add the code below in your asp.net cs source file.

((myMasterPage)this.Master).myloadstr = "here some javascript for load";

myMasterPage is the name of my masterpage class. We cast our asp.net page its masterpage, with "this.Master".

22 May 2009

Run PHP On Local Machine

When I want to develop a php website, it is always be hard to run and test the php code on local machine.
After I search how to run php on local machine, I found an awesome tool for this.


XAMPP is a great open source software that helps you run php on local, and also it runs MySQL Server service, Filezilla FTP.

Simply download xampp and install it, I select the run apache as service option.

By its default, it installs into C:\xampp directory,
you can copy your php files into C:\xampp\htdocs directory, and open in your browser.

16 December 2008

C# MS Access Connection String OLE DB OleDbConnection

The Connection String for MS Access can be set as ;
 private constr =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=yourdbfile.mdb;
Jet OLEDB:Database Password=yourpassword;";
And the C# code to connect to MS Access database ;
 public bool OpenConnection()
{
OleDbConnection con; con = new OleDbConnection(constr);

if (con.State != ConnectionState.Open)
{
try
{
con.Open(); return true;
}
catch (Exception) { return false; }
}
else return false;
}
C# Access Connection String OLE DB OleDbConnection password field is the password you set up in access database.

Mobile Phone Hide Your Number

To hide your number when you are calling someone with your mobile phone,
simply add *31# in the beginning of the number you will call.

hide number
*31#