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...

Thursday, 30 January 2014

What is the difference between declare variable inside or outside the loop in C# ASP.NET

Sometimes user Search these type of questions :-
  • Difference between declaring variables before or in loop
  • Is it better to declare a variable inside a loop
  • Is there is any performance difference between declare a variable outside or inside a loop 
Solution:-

In Short, Performance-wise both cases are compiled to the same IL, so there's no difference.

Let's See Example
Case 1: 
                int num;
                for (int i = 0; i < count; i++) 
                {
                        num = i;
                }


Case 2:
for (int i = 0; i < count; i++) { int num = i; }
 I think only difference between above two cases is, defining variables inside the loop makes it visibility local to that loop only because declaration does not cause any perceivable difference in performance.Personally, I Prefer the second one because it reduces scope of variables to where they are needed, which is good thing. 


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

Friday, 24 January 2014

How to avoid the cache of Textbox in C# ASP.NET

Sometimes user want to disable history from the text box. Suppose when user enter information like credit card number and again if user enter another number then no previous value should be available in the text box's history.
Here 5,8 are those numbers which were entered by user for this textbox, this clearly shows textbox is caching history.
I am going to disclose two ways to solve this problem which are shown below:-

1. By Using Asp.Net Property :
 
<asp:TextBox ID="txtNumber" runat="server" AutoCompleteType="Disabled"></asp:TextBox>

2. By Using JavaScript Code :
<asp:TextBox ID="txtSearch" runat="server" onfocus="disableautocomplete(this.id);" ></asp:TextBox>
JavaScript Function
function disableautocomplete(id) {
            var passwordControl = document.getElementById(id);
            passwordControl.setAttribute("autocomplete", "off");
        }

I think, best option would be to write a JavaScript function rather than just using 'AutoCompleteType' Property bcoz it ensures working in all browsers rather than just major browsers.


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