Monday, 27 May 2013

sql query with
tag

select c.CarID, c.SellerID, c.Car_Name,'Car For :'+ c.Car_For +'<br /> Condition :'+ c.Condition as cardetail, c.Price,'Currency Type :'+ c.Currency_Type +'<br /> Car Type :'+ c.[Type] +'<br /> Car Make :'+ c.Make  as car_type,
 'Registered Year :'+ c.RegistrationYear +'<br /> Milage :'+ c.Milage+'<br /> Terminal :'+ c.Terminal+'<br />Engine Capacity :'+ c.Engine_Capacity+'<br /> Colour :'+c.Colour as  CarData, c.ColourTye,  c.Description,
 c.datetime, c.Region, c.Sales_Type
 from dbo.Cars_Details_Master c where SellerID=@id

Substring() in SqlServer


SubString function:

This function is used to get the specified length of string based on specification of start position and required length of string. Generally SubString function will take 3 arguments.

Declaration of SubString function:

SUBSTRING(string, startIndex, length)

In this function

1st Argument specifies the string value

2nd Argument specifies the start position of string from where part of string begins

3rd Argument specifies number of characters to be returned from the specified string

ex:

SELECT OUT = SUBSTRING('abcdefgh', 2, 3)
The output will be "bcd".

SELECT SUBSTRING('STRING FUNCTION', 1, 6)
Result : STRING
SELECT SUBSTRING('STRING FUNCTION', 8, 8)
 Result : FUNCTION
--------------------------------------------------------------------------------------------------



substring();

substring function return some part of string from the string

Syntax:

SUBSTRING(expression,start,length)


DECLARE @ImportDate VARCHAR(8) = '10122012'

SELECT CAST(SUBSTRING(@ImportDate, 5, 4) + SUBSTRING(@ImportDate, 3, 2) + SUBSTRING(@ImportDate, 1, 2) AS DATETIME)
       
   Result:
       
2012-12-10 00:00:00.000


you also write queries like this.....


SET DATEFORMAT DMY
DECLARE @ImportDate VARCHAR(8) = '10122012'

SELECT CAST(STUFF(STUFF(@ImportDate, 5, 0, '-'), 3, 0, '-') AS DATETIME)

SET DATEFORMAT MDY

Result:

2012-12-10 00:00:00.000


--------------------------------------------------------

SET DATEFORMAT DMY

SELECT CAST(STUFF(STUFF('10122012', 5, 0, '-'), 3, 0, '-') AS DATETIME)

SET DATEFORMAT MDY


Result:2012-12-10 00:00:00.000
----------------------------------------------------------



Substring syntax


Substring (expression,startpoint,lenght)

99 represents thee Length of the expression
99 reflects the potential length of a string between the 1st comma and the second comma


declare @string varchar(20)
set @string='A,B,C'

Select substring( SUBSTRING(@string,charindex(',',@string)+1,99),0,charindex(',',SUBSTRING(@string,charindex(',',@string)+1,99)))

Result:
B

------------------------------------------------------------------------------------------------------------------------

create table col (col varchar(255))

insert into col
 select 'A,B,C'
 union all
select 'AA,BBBB,CCC'
union all
select 'AAA,BBBBBBBBBBBBBBBBBBBBBBBBBBBBB,CCC'
union all
select 'AAAAAAA,BBBBBBBB,CCC'

Select substring( SUBSTRING(col,charindex(',',col)+1,99),0,charindex(',',SUBSTRING(col,charindex(',',col)+1,99))) from col

drop table col

Result:
B
BBBB
BBBBBBBBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBB

Polymorphism


Definition 1: The ability to take more than one form.
Definition 2: Same operation may work in different way on other classes.

Basically polimorphism are two types:

1. Compile time polymorphism: for example method overloading
2. Run time polymorphism: for example method overriding

Compile time polymorphism: Method Overloading

In this, calss is this having same method name and different arguments. This is check at compile time only. So we can call it as compile time polimorphism




class Compile
{
public string method(string first)
{
     return first;
}

public string method()
{
      return "no argument";}
}
}


Run Time Polymorphism:Overriding

Method overriding occurs&nbsp;When the child class is having same method name with arguments count and type by one of its superclass

class BaseClass
{
    public virtual void OverrideExample()
    {
        Console.WriteLine("base method");
    }
}

class DerivedClass : BaseClass
{
    public override void OverrideExample()
    {
        Console.WriteLine("derived method");
    }
}

class Test
{
    static void Main(string[] args)
    {
        BaseClass a;
        DerivedClass b;

        a = new BaseClass();
        b = new DerivedClass();
        a.OverrideExample();  // output --> "base method"
        b.OverrideExample();  // output --> "derived method"

        a = new DerivedClass();
        a.OverrideExample();  // output --> "derived method"
        Console.ReadLine();
    }
}

Friday, 24 May 2013

Explanation and Comparison of NULLIF and ISNULL in SQLSERVER


Explanation of NULLIF
Syntax:
NULLIF ( expression , expression )

Returns a null value if the two specified expressions are equal. NULLIF returns the first expression if the two expressions are not equal. If the expressions are equal, NULLIF returns a null value of the type of the first expression. NULLIF is equivalent to a searched CASE function in which the two expressions are equal and the resulting expression is NULL.

Following is good example of NULLIF and CASE from BOL:
USE AdventureWorks;
GO
SELECT ProductID, MakeFlag, FinishedGoodsFlag,
NULLIF(MakeFlag,FinishedGoodsFlag)AS 'Null if Equal'
FROM Production.Product
WHERE ProductID < 10;
GO
SELECT ProductID, MakeFlag, FinishedGoodsFlag,'Null if Equal' =
CASE
WHEN MakeFlag = FinishedGoodsFlag THEN NULL
ELSE MakeFlag
END
FROM Production.Product
WHERE ProductID < 10;
GO

Explanation of ISNULL
Syntax:
ISNULL ( check_expression , replacement_value )

Replaces NULL with the specified replacement value. The value of check_expression is returned if it is not NULL; otherwise, replacement_value is returned after it is implicitly converted to the type of check_expression, if the types are different.

Following is good example of ISNULL from BOL:
USE AdventureWorks;
GO
SELECT AVG(ISNULL(Weight, 50))
FROM Production.Product;
GO

Observation:
Interesting observation is NULLIF returns null if it comparison is successful, where as ISNULL returns not null if its comparison is successful. In one way they are opposite to each other.

Row Data Bound in Gridview



 protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e)
    {

           if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState ==                      DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
        {
            LinkButton lnkEdit = (LinkButton)(e.Row.FindControl("lnkEdit"));
            Label lbllogin_status = (Label)(e.Row.FindControl("lbllogin_status"));
            if (lbllogin_status.Text == "offline" || lbllogin_status.Text == "")
                lnkEdit.Visible = false;
            else
                lnkEdit.Visible = true;
        }
}

Interfaces in C#


INTERFACES
  • An interface is a way to define a set of methods that we implement by a derived class or multiple derived classes.
  • Two classes can implement the same interface in a different way.
  • It is basically a logical structure that describes the functionality (methods) but not its implementation( body).
  • A class must provide the bodies of the methods described in an interface to implement the methods.


We can declare an interface in the following way:
interface name {
return-type method-name1( parameter list);
return-type method-name2( parameter list);

}

We declare an interface with the keyword interface. Here "name" is the name of the interface. In an interface all the methods are public, so there is no requirement for explicit access specifiers.

Now we implement an interface in the following way:
class class-name:interface-name{
// body of the class  }

A class can implement more than one interface. We can declare each interface in a comma-separated list. In this case, the name of the base class is always first.

EX:
Complete Program
using System;
public interface ICalc
{
int add(int a,int b);
int sub(int x,int y);
}
class Calc:ICalc
{
                public int add(int a,int b)
                {
                                return a+b;
                }
public int sub(int x,int y)
                {
                                return x-y;
                }
}
class MainCalc
{
                Calc c=new Clac();
                Console.WriteLine("The Addition Is:" + c.add(2,3));
                                Console.WriteLine("The Subtraction Is:" + c.sub(3,2));
}

The output will be:

The Addition is:5
The Subtraction is:1

Inheritance in the Interfaces

Interfaces can be inherited. Now we look at the example of how to easily inherit an interface:

After inheritance, "second" has the methods of the first interface and it adds printC() to it. Now the class MyClass can access all the three methods:
EX:
Complete Program
using System;
public interface first
{
                void printA();
                                void printB();
}
public interface second:first
{
void printC();
}
class MyClass:second
{

public void printA()
                {
                Console.WriteLine(" Print A");
                }
               
public void printB()
                {
                Console.WriteLine(" Print B");
                }
               
public void printC()
                {
                Console.WriteLine(" Print C");
                }
}
class MyMainClass
{
                public static void main()
                {
                                MyClass a=new MyClass();
                                a.printA();
                                a.printB();
                                a.printC();
                }
}


Thursday, 23 May 2013

upload mp3 file in asp.net using validation Expression


<asp:FileUpload ID="mp3UploadFile" runat="server" />
<asp:RegularExpressionValidator ID="revFluUploadFile" ControlToValidate="mp3UploadFile"
    ValidationExpression="^.+(.mp3|.MP3)$"
    ErrorMessage="Not MP3 type file" runat="server">
</asp:RegularExpressionValidator