Validate textbox only numeric or number in JQuery

By | JQuery

If you want to validate text for for entering only number or numeric values then here is jquery/javascript code.(Tested)

Below Is for All text box on page

$(document).ready(function () {
            $("input[type='text']").keypress(function (e) {
                if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
                    return false;
                }
            });
        });

And if you want to apply on specific text box then you can use selecter ID or class as you wish.



$(document).ready(function () {
            $("#txtbox1").keypress(function (e) {
                if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
                    return false;
                }
            });
        });

Function For Comma separate string to Column in SQL Server

By | SQL

Here is a user defined function that we have created to convert comma separate string values to Table Column,
Mostly we use for IN statement in Select QUERY like

select * from Emp WHERE PKId IN(fn_SplitParameters('1,2,3',','))

CREATE FUNCTION [dbo].[fn_SplitParameters](@String varchar(max), @Delimiter char(1))           
returns @temptable TABLE (ID int,items varchar(max))           
as           
begin           
declare @idx int           
declare @slice varchar(max)           
 declare @ID int           
  set  @ID=0       
select @idx = 1           
if len(@String)&lt;1 or @String is null  return           
           
while @idx!= 0           
begin           
set @idx = charindex(@Delimiter,@String)           
set @ID=@ID+1  
if @idx!=0           
set @slice = left(@String,@idx - 1)           
else           
set @slice = @String     
if(len(@slice)&gt;0)      
insert into @temptable(ID,Items) values(@ID,@slice)           
set @String = right(@String,len(@String) - @idx)           
 if len(@String) = 0 break           
end       
return           
end

How to Create a byte array from stream in C#?

By | C#
Moslty we want to save images or any file in database as byte, so we can convert any stream in byte array in c#, here below a method to convert.

 public static byte[] StreamToBytes(Stream input)
        {
            byte[] buffer = new byte[16 * 1024];
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                return ms.ToArray();
            }
        }

Convert List or any type of object to XML string data in C#

By | C#, MVC
Here is a method to conver any type of object to string xml, this method will take parameters of T type and will return XML string.

public static string ConvertToXML<T>(T obj)
        {
            try
            {

                using (StringWriter stringWriter = new StringWriter(new StringBuilder()))
                {
                    System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
                    xmlSerializer.Serialize(stringWriter, obj);
                    XDocument xDoc = XDocument.Parse(stringWriter.ToString());
                    xDoc.Declaration = null;
                    return xDoc.ToString();
                }

            }
            catch (Exception ex)
            {
                return ex;
            }
        }

XML Data Insert,Update with MERGE in sql server

By | MVC, SQL
First of all you have to select XML data into a temptable, that will use as SOURCE table in merger statement.

SELECT
		ISNULL(x.v.value('Id[1]', 'int'), '') AS Id,
		ISNULL(x.v.value('Name[1]', 'bit'), '') AS Name,
		ISNULL(x.v.value('Address[1]', 'bit'), '') AS Address,
		ISNULL(x.v.value('Status[1]', 'nvarchar(max)'), '') AS Status,
		ISNULL(x.v.value('MoNumber[1]', 'nvarchar(max)'), '') AS MoNumber,
		ISNULL(x.v.value('Location[1]', 'nvarchar(max)'), '') AS Location,
		ISNULL(x.v.value('Email[1]', 'nvarchar(max)'), '') AS Email
		INTO #TempData
	FROM @XMLData.nodes('/ArrayOfEmployeeAccessModel/EmployeeModel') x (v);

And then you can user MERGER Statement to insert or update according to condition.

MERGE tbl_UpdatedEmployee AS TARGET
		USING #TempData AS SOURCE 
		ON (
		TARGET.PKId = SOURCE.Id 
		) 
		WHEN MATCHED  THEN 
		UPDATE 
		SET TARGET.Name = SOURCE.Name,
                    TARGET.Address= SOURCE.Address,
			
		WHEN NOT MATCHED BY TARGET THEN 
		INSERT (Name,Address,Status,MoNumber,Location,Email) 
		VALUES (SOURCE.Name,SOURCE.Address,SOURCE.Status,SOURCE.MoNumber,SOURCE.Location,SOURCE.Email)
		
Also if you want to get INSERTED or UPDATED rows or data than you can get as resultset, but for this first of all you have to declare a table variable on top then you can get output as table and return as resultset.
DECLARE @output TABLE (Id int,Email VARCHAR(50),Password NVARCHAR(10))
 In Above statement you can declare the columns name which you want to take as output columns, above defined a table variable.
 And now you have to add a statement at the last of merge statement, and you have to put ; (semicolon) at last of OUTPUT data statement.

MERGE tbl_UpdatedEmployee AS TARGET
		USING #TempData AS SOURCE 
		ON (
		TARGET.PKId = SOURCE.Id 
		) 
		WHEN MATCHED  THEN 
		UPDATE 
		SET TARGET.Name = SOURCE.Name,
                    TARGET.Address= SOURCE.Address,
			
		WHEN NOT MATCHED BY TARGET THEN 
		INSERT (Name,Address,Status,MoNumber,Location,Email) 
		VALUES (SOURCE.Name,SOURCE.Address,SOURCE.Status,SOURCE.MoNumber,SOURCE.Location,SOURCE.Email)

               OUTPUT INSERTED.Id,INSERTED.Address,INSERTED.Status INTO @output;
And now you can select your table variable as resultset.
SELECT * FROM @output

For More Detail about MERGE Visit Microsoft MSDN Weblink
Link