RSS

Allow only alphanumeric in textbox using javascript and regular expression

function validate(evt) {
     var theEvent = evt || window.event;
     var key = theEvent.keyCode || theEvent.which;
     key = String.fromCharCode(key);
     var regex = /^[0-9a-zA-Z\s\b]+$/;
     if (!regex.test(key)) {
         theEvent.returnValue = false;
         if (theEvent.preventDefault) 
             theEvent.preventDefault();
      }
 }

<asp:TextBox ID="txtVal" runat="server" onKeyPress="validate(e);"></asp:TextBox>
 
Leave a comment

Posted by on April 26, 2016 in .NET

 

Extending using Extension methods

static class MyExtensionMethods
{
     public static int Negate(this int value)
     {
        return -value;
     }

     public static int Multiply(this int value, int multiplier)
     {
        return value * multiplier;
     }
}

static void Main(string[] args)
{
      // Passing arguments in extension methods
      int i3 = 10;
      Console.WriteLine(i3.Multiply(2));
}
 
Leave a comment

Posted by on March 16, 2016 in .NET

 

Stored Procedure vs Function

Stored Procedure Function
Return type is not must, Can return zero, single or multiple values or table(s) Return type is must, and it can return one value which is mandatory. UDFs that return tables can be treated as another rowset. This can be used in JOINs with other tables.
SP can have input/output parameters Support only input parameters
Allow Select as well as DML statements.

Allow Select but not DML statements.

Note: In case of multi-table valued functions it can contain DML statements affecting Table Variables.

Stored procedure can execute function. Function cannot execute stored procedure.
Cannot be the part of Select query as a column. Can be the part of select query as a column
Stored Procedures cannot be embedded in the SQL statements like WHERE/HAVING/SELECT Functions can embedded in the SQL statements like WHERE/HAVING/SELECT
We can use exception handling using Try….Catch block in SP. We can’t use Try….Catch block in UDF
We can use transactions in SP We can’t user transaction in UDF
Can have up-to 21000 input parameters UDF can have up-to 1023 input parameters

Why we can’t execute stored procedure inside a function?

Answer:

  1. Stored Procedure may contain DML statements.
  2. Function can’t contain DML statements.
    So executing Function inside stored procedure will never break rule 1.
    But executing stored procedure inside function may break rule no 2.

Functions are computed values and cannot perform permanent environmental changes to SQL Server (i.e. no INSERT or UPDATE statements allowed). It returns error like “Invalid use of a side-effecting operator ‘INSERT’ within a function.” So ultimately strict rule is made by Sql team: we can’t execute stored procedures inside function.

 
Leave a comment

Posted by on March 14, 2016 in .NET

 
Image

Load events in Asp.net

load

 
Leave a comment

Posted by on February 18, 2016 in .NET

 

Read XML Data for datasource

using System.Xml.Linq;

<?xml version=”1.0″ encoding=”utf-8″ ?>
<StatusTypes>
<StatusType Name =”All” value=”0″/>
<StatusType Name =”Over-vote” value=”1″/>
<StatusType Name =”Mixed Status” value=”2″/>
<StatusType Name =”Not Received” value=”3″/>
<StatusType Name =”Over Reported” value=”4″/>
<StatusType Name =”Under Reported” value=”5″/>
<StatusType Name =”In Balance” value=”6″/>
</StatusTypes>

string fullName = HttpContext.Current.Server.MapPath(
“~/Modules/Pxy/StatusDataSource.xml”);
XDocument doc = XDocument.Load(fullName);
return (from d in doc.Root.Elements(“StatusType”)
select new KeyValuePair<string,int>( d.Attribute(“Name”).Value, int.Parse( d.Attribute(“value”).Value))
).ToList();

statusDataSource DataSource = new statusDataSource();
if (ddlStatus.Items.Count > 0)
ddlStatus.Items.Clear();

ddlStatus.DataSource = null;
foreach (KeyValuePair<string, int> St in DataSource.Status)
ddlStatus.Items.Add(new ListItem(St.Key, St.Value.ToString()));

 
Leave a comment

Posted by on February 5, 2016 in .NET

 

Shadowing in C#

  • In Method override  both methods (base & child class methods) have the same name, same number and same type of parameter in the same order with the same return type. The overridden base method must be virtual, abstract or override
  • but without these we can do using new keyword, this mechanism is called Shadowing
  • In the shadowing or method hiding, the child class has its own version of the function, the same function is also available in the base class.
  • Using this concept we can provide a new implementation for the base class method without overriding it.
  • Showing is used to protect against subsequent base class modification, We can change the access modifier
  • There is no control of a base class on shadowing
  • We can also use shadowing and method overriding together using the virtual and new keywords
public class BaseClass 
{ 
    public string GetMethodOwnerName() 
    { 
        return "Base Class"; 
    } 
} 
public class ChildClass : BaseClass
{
    public new virtual string GetMethodOwnerName()
    {
        return "ChildClass";
    }
}
public class SecondChild : ChildClass
{
    public override string GetMethodOwnerName()
    {
        return "Second level Child";
    }
} 

static void Main(string[] args) 
{ 
    ChildClass c = new ChildClass(); 
    Console.WriteLine(c.GetMethodOwnerName()); 
}

Output: ChildClass

We can’t use the new and override keywords together. If you do then the compiler throws a compilation error

Shadowing  Example (using new keyword in child class method)

static void Main(string[] args) 
{ 
    BaseClass c = new ChildClass(); 
    Console.WriteLine(c.GetMethodOwnerName()); 
}

Output: Base Class

 
Leave a comment

Posted by on October 7, 2015 in .NET

 

ref vs out in C#

While writing a code, we often come across situations where we need to return multiple values from a single function/method. But a method can only return a single value.The question is how do we overcome such situation.Answer is simple, use reference types.

Parameters are always passed by value to a method by default.If we want to pass them by reference then we can use either out or ref keyword.

Ref

The ref keyword is used to pass an argument as a reference. This means that when value of that parameter is changed in the method, it gets reflected in the calling method. An argument that is passed using a ref keyword must be initialized in the calling method before it is passed to the called method.

Ref keyword will pass parameter as a reference this means when the value of parameter is changed in called method it get reflected in calling method also. There is no “boxing” when a value type is passed by reference

Out

The out keyword is also used to pass an argument like ref keyword, but the argument can be passed without assigning any value to it. An argument that is passed using an out keyword must be initialized in the called method before it returns back to calling method.

Out keyword also will pass parameter as a reference but here out parameter must be initialized in called method before it return value to calling method

public ActionResult Index()
{
    int val1 = 0; //must be initialized for ref
    int val2 = 0; //optional for out
    Example1(ref val1);
    ViewBag.Message += val1 + "\n"; // val1=0
    Example2(out val2);
    ViewBag.Message += val2 + "\n"; // val1=2
    return View();
}
static void Example1(ref int value) //called method
{
    //value = 1; // optional
}
static void Example2(out int value) //called method
{
    value = 2; //must to assign value
}

These ref and out parameters are useful whenever your method wants to return more than one value.

Both the method definition and the calling method must explicitly use the ref / out keyword

Several inbuilt methods as “TryParse” (one of my favourite) use out and not ref, may be the internal implementation of library mainly uses ref.

Properties/Indexers/Dynamic member access cannot be passed to ref or out parameters since internally they are functions/methods and not members/variables

ref tells the compiler that the object is initialized before entering the function, while out tells the compiler that the object will be initialized inside the function.

Ref and out in method overloading

Both ref and out cannot be used in method overloading simultaneously. However, ref and out are treated differently at run-time but they are treated same at compile time (CLR doesn’t differentiates between the two while it created IL for ref and out). Hence methods cannot be overloaded when one method takes a ref parameter and other method takes an out parameter. The following two methods are identical in terms of compilation.

class MyClass
{
   public void Method(out int a) // compiler error “cannot define overloaded”
   {
       // method that differ only on ref and out"
   }
   public void Method(ref int a)
   {
       // method that differ only on ref and out"
   }
}

However, method overloading can be done, if one method takes a ref or out argument and the other method takes simple argument. The following example is perfectly valid to be overloaded.

class MyClass
{
          public void Method(int a)
          {
          }
          public void Method(out int a)
          {
                   // method differ in signature.
          }
}

Ex:

public static void Foo()
{
      int val = 0;
      Example1(val);
      Console.WriteLine(val); // Still 0!
      Example2(ref val);
      Console.WriteLine(val); // Now 2!
      Example3(out val);
      Console.WriteLine(val); // Now 3!
}

static void Example1(int value)
{
      value = 1;
}
static void Example2(ref int value)
{
      value = 2;
}
static void Example3(out int value)
{
      value = 3;
}
Ref Out
The parameter or argument must be initialized first before it is passed to ref. It is not compulsory to initialize a parameter or argument before it is passed to an out.
It is not required to assign or initialize the value of a parameter (which is passed by ref) before returning to the calling method. A called method is required to assign or initialize a value of a parameter (which is passed to an out) before returning to the calling method.
Passing a parameter value by Ref is useful when the called method is also needed to modify the pass parameter. Declaring a parameter to an out method is useful when multiple values need to be returned from a function or method.
It is not compulsory to assign a parameter value before leaving calling method. A parameter value must be assigned within the calling method before its use.
When we use REF, data can be passed bi-directionally. When we use OUT data is passed only in a unidirectional way (from the called method to the caller method).
Both ref and out are treated differently at run time and they are treated the same at compile time.
Properties are not variables, therefore it cannot be passed as an out or ref parameter.

The out and ref keywords are useful when we want to return a value in the same variables as are passed as an argument

 
Leave a comment

Posted by on September 10, 2015 in .NET

 
 
Follow

Get every new post delivered to your Inbox.

Join 25 other followers

%d bloggers like this: