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)

Tuesday, 26 December 2017

Alter table using sql queries in sql server


Add column in table

>> ALTER TABLE table_name ADD column_name column-definition;
     ALTER TABLE employees ADD last_name VARCHAR(50);

Change column type in table
>> ALTER TABLE table_name  ALTER COLUMN ColumnName ColumnType
   ALTER TABLE mstVillage ALTER COLUMN Lat decimal(18,9)

Add multiple columns in table
>> ALTER TABLE table_name ADD column_1 column-definition,column_2 column-definition,... column_n column_definition;
  ALTER TABLE employees ADD last_name VARCHAR(50), first_name VARCHAR(40);


Modify column in table
 >> ALTER TABLE table_name ALTER COLUMN column_name column_type;
     ALTER TABLE Emp ALTER COLUMN Col_name VARCHAR(75) NOT NULL;


Drop column in table
>> ALTER TABLE tbl_Name DROP COLUMN column_name;
   ALTER TABLE Emp DROP COLUMN Col_name;


Rename column in table
>> sp_rename 'table_name.old_col_name', 'new_col_name', 'COLUMN';
   sp_rename 'employees.last_name', 'lname', 'COLUMN';


Rename table
>> sp_rename 'old_tbl_name', 'new_tbl_name';

   sp_rename 'employee', 'emp';

Monday, 25 December 2017

Running Counter formats using jquery

////////////////////////// HTML


<span id="counterLbl" class="count">306789948.26</span>



/////////// INR Format JS

function Count_Running() {
                        $('.count').each(function () {
                            $(this).prop('Counter', 0).animate({
                                Counter: $(this).text()
                            }, {
                                duration: 4000,
                                easing: 'swing',
                                step: function (rnCnt) {
                                    var va = INR_RunningCnt_Format(rnCnt);
                                    $(this).text(va);
                                }
                            });
                        });
                    }
                    function INR_RunningCnt_Format(nStr) {
                        nStr += '';
                        x = nStr.split('.');
                        x1 = x[0];
                        x2 = x.length > 1 ? '.' + x[1] : '';
                        var rgx = /(\d+)(\d{3})/;
                        var z = 0;
                        var len = String(x1).length;
                        var num = parseInt((len / 2) - 1);

                        while (rgx.test(x1)) {
                            if (z > 0) {
                                x1 = x1.replace(rgx, '$1' + ',' + '$2');
                            }
                            else {
                                x1 = x1.replace(rgx, '$1' + ',' + '$2');
                                rgx = /(\d+)(\d{2})/;
                            }
                            z++;
                            num--;
                            if (num == 0) {
                                break;
                            }
                        }
                        return x1 + x2;

                    }

////// Result -          30,67,89,948.26

//////////////// Other formate JS
function Count_Running() {
                        $('.count').each(function () {
                            $(this).prop('Counter', 0).animate({
                                Counter: $(this).text()
                            }, {
                                duration: 4000,
                                easing: 'swing',
                                step: function (now) {
                                    var va = Other_Format(now);
                                    $(this).text(va);
                                }
                            });
                        });
                    }

function Other_Format(nStr) {
            nStr += '';
            x = nStr.split('.');
            x1 = x[0];
            x2 = x.length > 1 ? '.' + x[1] : '';
            var rgx = /(\d+)(\d{3})/;
            while (rgx.test(x1)) {
                x1 = x1.replace(rgx, '$1' + ',' + '$2');
            }
            return x1 + x2;
        }

/////////Result-  306,789,948.26

Thursday, 21 December 2017

Gridview export to excel in formatted way using asp.net c#

///////////// Export to excel

private void ExportToExcel_Farmatted(DataTable table)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Reports.xls");
        ///Supporting HTML
        HttpContext.Current.Response.Charset = "utf-8";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
        //sets font and family
        HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");
        HttpContext.Current.Response.Write("<BR><BR><BR>");
        //sets the table border, cell spacing, border color, font of the text, background, foreground, font height
        HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' borderColor='#000000' cellSpacing='0' cellPadding='0' " +
          "style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>");
        //am getting my grid's column headers

        for (int j = 0; j < table.Columns.Count; j++)
        {      //write in new column
            HttpContext.Current.Response.Write("<Td>");
            //Get column headers  and make it as bold in excel columns
            HttpContext.Current.Response.Write("<B>");
            HttpContext.Current.Response.Write(table.Columns[j].ColumnName);
            HttpContext.Current.Response.Write("</B>");
            HttpContext.Current.Response.Write("</Td>");
        }
        HttpContext.Current.Response.Write("</TR>");
        foreach (DataRow row in table.Rows)
        {//write in new row
            HttpContext.Current.Response.Write("<TR>");
            for (int i = 0; i < table.Columns.Count; i++)
            {
                HttpContext.Current.Response.Write("<Td>");
                HttpContext.Current.Response.Write(row[i].ToString());
                HttpContext.Current.Response.Write("</Td>");
            }

            HttpContext.Current.Response.Write("</TR>");
        }
        HttpContext.Current.Response.Write("</Table>");
        HttpContext.Current.Response.Write("</font>");
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.End();
    }


How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...