Sunday 6 January 2013

jQuery to Validate File Upload Extension in File Upload Control



<script src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function() {
$('#<%=fileupload1.ClientID %>').change(function() {
var fileExtension = ['jpeg', 'jpg', 'png', 'gif', 'bmp'];
if ($.inArray($(this).val().split('.').pop().toLowerCase(), fileExtension) == -1) {
alert("Only '.jpeg','.jpg', '.png', '.gif', '.bmp' formats are allowed.");
}
})
})
</script>

Tuesday 1 January 2013

C# - Difference between Convert.tostring and .tostring() method

The basic difference between them is “Convert.ToString(variable)” handles NULL values even if variable value become null but “variable.ToString()” will not handle NULL values it will throw a NULL reference exception error.

 Example

//Returns a null reference exception for str.
string strque;
object i = null;
strque = i.ToString();
//Returns an empty string for str and does not throw an exception. 
string strque;
object i = null;
strque = Convert.ToString(i);

So as a good coding practice using “convert” is always safe.