Tuesday, April 14, 2015

asp.net call SQL Function

public string Test(string userId)
        {
         
            string sqlSelect = "Select [dbo].[Test](@user_id)";

            SqlParameter[] parms = {
                new SqlParameter("@user_id", SqlDbType.VarChar)};

            parms[0].Value = userId;

            try
            {
                object returnValue = null;
                returnValue = SQLHelper.ExecuteScalar(SQLHelper.ConnectionStringLocalTransaction, CommandType.Text, sqlSelect, parms);

                return Convert.ToString(returnValue);
            }
            catch (Exception ex)
            {
                throw new BaseException(ex.Message, ex);
            }
            finally
            {
                // conn.Close();
            }        
        }

 public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand cmd = new SqlCommand();

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                //try
                //{
                PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
                object val = cmd.ExecuteScalar();
                cmd.Parameters.Clear();
                connection.Close();
                return val;
                //}
                //finally
                //{
                //    connection.Close();
                //}
            }
        }

 private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
        {

            if (conn.State != ConnectionState.Open)
                conn.Open();

            cmd.Connection = conn;
            cmd.CommandText = cmdText;

            if (trans != null)
                cmd.Transaction = trans;

            cmd.CommandType = cmdType;

            if (cmdParms != null)
            {
                foreach (SqlParameter parm in cmdParms)
                {
                    if (parm.SqlValue == null) { parm.SqlValue = DBNull.Value; }
                    cmd.Parameters.Add(parm);
                }
            }
        }

Create Function in sql

ALTER FUNCTION [dbo].[Test]
(
@user_id varchar(100)
)
RETURNS INT
AS
BEGIN
-- Declare the return variable here
DECLARE @vParaVal INT

-- Add the T-SQL statements to compute the return value here
SELECT @vParaVal = department_id from Test where [user_id] = @user_id

RETURN @vParaVal

END

remove foreign key constraint in sql


ALTER TABLE mst
DROP CONSTRAINT FK_mst

Friday, April 10, 2015

JavaScript isNaN() Function

Check whether a number is an illegal number:

var a = isNaN(123) + "<br>";
var b = isNaN(-1.23) + "<br>";
var c = isNaN(5-2) + "<br>";
var d = isNaN(0) + "<br>";
var e = isNaN("Hello") + "<br>";
var f = isNaN("2005/12/12") + "<br>";

var res = a + b + c + d + e + f;

false
false
false
false
true
true

Thursday, April 9, 2015