Friday, 14 February 2014

How to make Asp.net Textbox Readonly in JavaScript

Solution:
script type="text/javascript">
    function makeReadOnly(){
        var textbox = document.getElementById("txtName");
        textbox.readOnly = "readonly";                          //readOnly is case-sensitive
        }

<body onload=" makeReadOnly ()">
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
    </div>
    </form>
</body>
The reason behind calling makeReadOnly function in body tag is to make textbox readonly when page is loading. You can also make textbox readonly after page load by just calling the makeReadOnly function on any textbox or button control.

That’s it!!…..Happy Programming...

Friday, 7 February 2014

Program to catch and process an error n times in C#

I am going to implement a class which will catch error only once and process an error more than once which would depend on user, it means how many times user want to process that error. In this class 'n' represent how many times you want to process an error. If you want to process an error three times, you just need to change '3' at the place of 'n'.

using System;

class student
{
    public static void Main(String[] args)
    {
        int a = 3;
        int b = 0;
        int result;
        for (int i = 0; i < n; i++)
        {

            try
            {
                result = a / b;
                Console.WriteLine(result);
            }

            catch (Exception ex)
            {
                Console.WriteLine("error is" + ex.Message);
            }

        }
    }
}

That’s it!!…..Happy Programming...

Wednesday, 5 February 2014

What is the difference between ExecuteNonQuery and ExecuteScalar in SQL

ExecuteNonQuery 
  1. Used mainly for action queries(insert,delete,update,create,alter). 
  2. Returns an int value indicating the number of affected rows.
  3. Return value is optional and can be assigned to an integer variable.
Example :
public void updateEmail()
{
        SqlConnection con = new SqlConnection(connString);
        String sqlQuery = "UPDATE Employee SET email='a@b.com' WHERE id=1;
        SqlCommand cmd = new SqlCommand(sqlQuery, con);
        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
         finally
         {
            con.Close();
         }
}
 
ExecuteScalar 
  1. Used in queries where we have to read a single value.
  2. Returns an object.
  3. Return value is compulsory and should be assigned to a variable of required type.
Example:
public int getSomeNumber()
{
        int count=0;
        SqlConnection con = new SqlConnection(connString);
        String sqlQuery = "SELECT COUNT(*) FROM dbo.myTable";
        SqlCommand cmd = new SqlCommand(sqlQuery, con);
        try
        {
            con.Open();
            //return type is System.Object, a typecast is must
            count = (Int32)cmd.ExecuteScalar();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
         finally
         {
            con.Close();
         }
         return count;
}


That’s it!!…..Happy Programming...

Tuesday, 4 February 2014

How to Convert Date into different format in C#

There are some situations where it is useful to convert a Date into a formatted character string within a application. 
Note:- Confusion between “m” and “M”, small case “m” represent minutes while “M” represent Month. This is most common cause of error while converting String to date and back date to string. In short ddMMyy is not equal to ddmmyy .
using System;

class DateFormat
{
    static void Main()
    {
       String dateFormat1=DateTime.Now.ToString("MM/dd/yyyy");
       Console.WriteLine(dateFormat1);  
       
       String dateFormat2=DateTime.Now.ToString("dd/MM/yyyy");
       Console.WriteLine(dateFormat2);
       
       String dateFormat3=DateTime.Now.ToString("MMM d, yyyy");
       Console.WriteLine(dateFormat3); 
       
       String dateFormat4=DateTime.Now.ToString("dddd, MMMM d");
       Console.WriteLine(dateFormat4); 
       
       String dateFormat5=DateTime.Now.ToString("MM/dd/yy");
       Console.WriteLine(dateFormat5); 
       
       String dateFormat6=DateTime.Now.ToString("dd MMM yyyy");
       Console.WriteLine(dateFormat6); 
       
       String dateFormat7=DateTime.Now.ToString("dd.MM.yyyy");
       Console.WriteLine(dateFormat7); 
       
       String dateFormat8=DateTime.Now.ToString("yyMMdd");
       Console.WriteLine(dateFormat8);
       
       String dateFormat9=DateTime.Now.ToString("dddd");
       Console.WriteLine(dateFormat9); // get only current day
       
       String dateFormat10=DateTime.Now.ToString("MMMM");
       Console.WriteLine(dateFormat10); // get only current month       
    } 
} 

That’s it!!…..Happy Programming...