Tuesday 30 January 2018

Highlight asp.net controls using required field validate with javascript

/////////////////////html

  <asp:TextBox ID="TxtUserName" OnTextChanged="Txtuser_TextChanged" CssClass="form-control" runat="server" AutoPostBack="True"></asp:TextBox>

<asp:RequiredFieldValidator ID="RFVTxtUserName" runat="server" ControlToValidate = "TxtUserName" Display="Dynamic" ValidationGroup="ValReg"/>





//////////JS
<script type="text/javascript">
        function Validate(ValGrp) {
            var FLAG;
            debugger;
            if (Page_Validators.length > 0) {
                for (var i = 0; i < Page_Validators.length; i++) {
                    var val = Page_Validators[i];
                    var ctrl = document.getElementById(val.controltovalidate); //Validate                   
                    if (ctrl != null && ctrl.style != null) {
                        if (ctrl.value == "" && (ctrl.getAttribute('style') != 'display: none;') && (ctrl.getAttribute('style') != 'border: 1px solid red; display: none;')) {
                            if (Page_Validators[i].validationGroup == ValGrp ) {
                                ctrl.style = "border: 1px solid red";
                                FLAG = 1;
                            }
                        }
                        else {
                            ctrl.style.borderColor = '';
                        }
                    }
                }
            }
            if (FLAG == 1) {
                return false;
            }
            else {
                return true;
            }
        }

        </Script>

Monday 15 January 2018

Write Exception log on catch portion in asp.net C#

////////  Call exp log
protected void Page_Load(object sender, EventArgs e)
    {
        try
        {

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

    }

/////////// Write log

public static void WriteErrorLog(Exception ex)
    {
        var line = Environment.NewLine + Environment.NewLine;
        string ErrorlineNo = ex.StackTrace.Substring(ex.StackTrace.Length - 7, 7),
                  Errormsg = ex.GetType().Name.ToString(),
                   extType = ex.GetType().ToString(),
                    expUrl = HttpContext.Current.Request.Url.ToString(),
                    hostIp = "",
             ErrorLocation = ex.Message.ToString();

        if (HttpContext.Current.Request.UserHostAddress != null)
        {
            Int64 macip = new Int64();
            string macSrc = macip.ToString("X");
            if (macSrc == "0")
            {
                if (Convert.ToString(HttpContext.Current.Request.UserHostAddress) != "127.0.0.1")
                {
                    hostIp = Convert.ToString(HttpContext.Current.Request.UserHostAddress);
                }
            }
        }

        try
        {
            string filepath = HttpContext.Current.Server.MapPath("~/ExceptionDetailsFile/");  //Text File Path

            if (!Directory.Exists(filepath))
            {
                Directory.CreateDirectory(filepath);

            }
            filepath = filepath + DateTime.Today.ToString("dd-MM-yy") + ".txt";   //Text File Name
            if (!File.Exists(filepath))
            {


                File.Create(filepath).Dispose();

            }
            using (StreamWriter sw = File.AppendText(filepath))
            {
                string error = 
"Log Written DateTime :" + " " + DateTime.Now.ToString() + Convert.ToString(line) + 
"Error Line No. :" + " " + ErrorlineNo + Convert.ToString(line) + 
"Error Message :" + " " + Errormsg + Convert.ToString(line) + 
"Exception Type :" + " " + extType + Convert.ToString(line) + 
"Error Location :" + " " + ErrorLocation + Convert.ToString(line) + 
"Error Page Url :" + " " + expUrl + Convert.ToString(line) + 
"User Host IP :" + " " + hostIp + Convert.ToString(line);

                sw.WriteLine("----Exception On " + " " + DateTime.Now.ToString() + "----");
                sw.WriteLine("------------------------------------------------------------");
                sw.WriteLine(Convert.ToString(line));
                sw.WriteLine(Convert.ToString(error));
                sw.WriteLine("-------------*End*-------------");
                sw.WriteLine(Convert.ToString(line));
                sw.Flush();
                sw.Close();

            }

        }
        catch (Exception e)
        {
            e.ToString();
        }
    }

    

Sunday 14 January 2018

Assign exec parameter in a variable using sql query

//////////Funtions/////////

declare @Variable varchar(12)
declare @SqtCommand nvarchar(4000)
set @SqtCommand = 'select @Variable= FiledName from TableName'
exec sp_executeSQl @SqtCommand, N'@Variable varchar(12) output', @Variable output

print @Variable


//////////RESULT////////////


declare @Variable varchar(12)
declare @SqtCommand nvarchar(4000)
set @SqtCommand = 'select @Variable=CAST(MIN(Year)as varchar)+''-''+ cast(MAX(Year) as varchar) from tbl_SurfaceWater inner join mstLocation on tbl_SurfaceWater.LocationGUID=mstLocation.LocationGUID where (Year(GETDATE())-Year) between 0 and 30 and DataTypeCatID=1 and TemplateID=2'
exec sp_executeSQl @SqtCommand, N'@Variable varchar(12) output', @Variable output
print @Variable

/////////Result///////////

Tuesday 2 January 2018

Add a geometry column for fetching lat long using geojson in sql server

////////
ALTER TABLE TableName  ADD GeoLoc geometry

UPDATE [dbo].[TableName]

SET [GeoLoc] = geometry::STPointFromText('POINT(' + CAST([Long] AS VARCHAR(20)) + ' ' + CAST([Lat] AS VARCHAR(20)) + ')', 4326)

Excel Sort values in ascending order using function TEXTJOIN

 Excel ::  Text ::  1,3,5,2,9,5,11 Result :: 1,2,3,5,5,9,11 Formula ::     TEXTJOIN ( ",",1,SORT(MID(SUBSTITUTE( A1 ,","...