Friday, May 27, 2011

ISNULL() Function in C#




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace NullableDataTypes
{
public class Program
{
//Like DataBase ISNULL() Function the "??" operator works
//e.g. ISNULL(null,1) o/p will be 1
//Similarly val3= val1 ?? val2;
//val3 will be val2 if al1=null
public static Object IsNull( object objReplace, object objSearch)
{
Object _newVal = 0;
if (objSearch is string)
{
string val = objReplace as string;
_newVal = val ?? objSearch as string;
}
else if (objSearch is int)
{
int? val = objReplace as int?;
_newVal = val ?? objSearch as int?;
return _newVal;
}
else if (objSearch is double)
{
double? val = objReplace as double?;
_newVal = val ?? objSearch as double?;
return _newVal;
}
else if (objSearch is float)
{
float? val = objReplace as float?;
_newVal = val ?? objSearch as float?;
return _newVal;
}
else if (objSearch is decimal)
{
decimal? val = objReplace as decimal?;
_newVal = val ?? objSearch as decimal?;
return _newVal;
}
else if (objSearch is long)
{
long? val = objReplace as long?;
_newVal = val ?? objSearch as long?;
return _newVal;
}
else if (objSearch is bool)
{
bool? val = objReplace as bool?;
_newVal = val ?? objSearch as bool?;
return _newVal;
}
else
{
_newVal = -1;
return _newVal;
}
return _newVal;
}
static void Main(string[] args)
{
/**
* We are storing the returned value in Annoynoumus Type variable
*/
var b = IsNull(null, "Bimlesh");
Console.WriteLine("{0}",b);
Console.Read();
}
}
}

Tuesday, May 24, 2011

How to Check Null Values in C#?



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace NullableDataTypes
{
class Program
{
public static bool CheckNull(object obj)
{
bool _isNull = false;
if (obj is string)
{
//Only Checks For String value
}
else
{
int? X = obj as int?;
//OR int? X = (int?)arr[i];
//Note: X.HasValue returns false for reference type and Null values.
if (X.HasValue)
{
_isNull = false ;
}
else { _isNull = true; }
}
return
_isNull;
}
static void Main(string[] args)
{
bool isnull=Program.CheckNull
("");
Console.WriteLine("Is Supplied Value SPACE is null? -{0}", Program.CheckNull(" "));
Console.WriteLine("Is Supplied Value Bimlesh is Null? -{0}", Program.CheckNull("Bims"));
Console.WriteLine("Is Supplied Value NULL is null? -{0}", Program.CheckNull
(null));
Console.WriteLine("Is Supplied Value 1 is null? -{0}", Program.CheckNull
(1));
Console.
Read();
}
}
}

Friday, May 20, 2011

What is Nullable Numeric DataType?

Defination: Nullable DataTypes are the Types Which can hold Null Value e.g Int, Decimal, Float ,long, bool
Why Required?
Many times we are not confirmed that coming values are Null or Proper.
e.g If Table colm is allowed null and its Type is Int in that case assignment of data into int Type will
be fail (int=Null)
Syntex: Type + ? e.g int?,bool?,long? etc
Nullable Numeric DataTypes e.g Float ,Int, long,Decimal
int? _nullableName;
int _name1;
_nullableName = null;
_name1 = (int)_nullableName;
Console.WriteLine(_name1.ToString());
//Note: If Nullable DataType is having Null value and we are assinging it to some Not Nullable type ,it will throw exception
int standardInteger = 123;
int?
nullableInteger;
decimal standardDecimal = 12.34M;
// Implicit conversion from int to int?
nullableInteger =
standardInteger;
// Explicit conversion from int? to int
standardInteger = (int)
nullableInteger;
// Explicit cast from decimal to int?
nullableInteger = (int?)standardDecimal;

Wednesday, May 18, 2011

How To Insert XML file in DataBase Using OpenXML


DECLARE @idoc int
DECLARE @doc varchar(1000)

--Create a Temporary Table
Create Table #TempTable([Name] VARCHAR (100),Age VARCHAR (100), Address VARCHAR (100),Hobby VARCHAR (100),BANKCODE VARCHAR (100),BANKACCT VARCHAR (100))

--Assignment of Xml data in a string/xml dataType E.G DECLARE @doc varchar(1000)/DECLARE @doc xml
SET @doc ='Please Refer to Attached Image For XMl Data'

-- Create internal DOM representation of the XML document.
--This Will return a XMlDocument Object which can be iterated with the help of X-Path Query
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc

--Inserting Data into Temporary Table #TempTable
Insert into #TempTable
SELECT * FROM
--Reading @idoc with OPENXML
OPENXML (@idoc, 'Verification/PersonalDetail', 2)
WITH ([Name] VARCHAR (100),Age VARCHAR (100), Address VARCHAR (100),Hobby VARCHAR (100),BANKCODE VARCHAR (100),BANKACCT VARCHAR (100))

--Removing Compiled Xml Document Object from memory
EXEC sp_xml_removedocument @idoc

SELECT * FROM #TempTable
DROP TABLE #TempTable