adsense

Wednesday, December 15, 2021

Solving ERR_CONNECTION_REFUSED

 If you are starting to setup IIS on a new machine you will encounter this issue. There are few things to resolve the issue.

1. Try changing the port number in your project

Project Properties → Web → Servers → Project Url:

2. click Create Virtual Directory button

3. In IIS,  add bindings for https to the default website 



cheers

Samitha




Sunday, November 21, 2021

CSS form action selector

 Only recently I got to know that we can actually style form action. The trick is to use body with form action as shown below. Even though  have used the class name id names can also be used.


body form[action="/SomeURL/DoSearch/"] .className {

    display: none !important;

    visibility:hidden !important;

}

Cheers,

Samitha

Sunday, November 14, 2021

Windows 2019 Server MSSQL Error 'The underlying provider failed on Open'

 If you are trying to migrate your .Net applications to a Windows 2019 Server you will most likely to encounter   MSSQL Error 'The underlying provider failed on Open'.

This can be fixed by Defining a new Windows Firewall rule for SQL Server (and for port 1433) on the server machine.


Cheers,

Samitha

Sunday, October 31, 2021

use ADO.NET via Context.Database

The DbContext.Database property allows us to perform ADO.NET as sown below. 

 //Execute Reader

    using (var ctx= new DatabaseContext())
    using (var cmd= ctx.Database.GetDbConnection().CreateCommand())
    {
        cmd.CommandText = "SELECT * From Table1";
        cmd.Database.OpenConnection();
        using (var result = command.ExecuteReader())
        {
            // do something with result
        }
    } 


//Execute SP


using (var ctx= new DatabaseContext())
    using (var cmd= ctx.Database.GetDbConnection().CreateCommand())
    {
               cmd.CommandText = "dbo.someSP"
                _dataContext.Connection.Open()
                cmd.CommandType = CommandType.StoredProcedure

                Dim param As DbParameter = command.CreateParameter()
                param.DbType = DbType.Int16
                param.ParameterName = "@param1"
                param.Value = 'value1"
                cmd.Parameters.Add(param)

                param = cmd.CreateParameter()
                param.DbType = DbType.DateTime
                param.ParameterName = "@param2"
                param.Value = "value2"
                cmd.Parameters.Add(param)

                cmd.ExecuteNonQuery()
                _dataContext.Connection.Close()
        }
    } 


Cheers,

Samita

Sunday, October 17, 2021

find second DIV of the same class

If you need a CSS selector that can be used to find the 2nd DIV of  among several DIVs with the same class, CSS3 provides nth-of-type(n) selector as displayed below.

.row:nth-of-type(2) {


}


Cheers,

Samitha

Sunday, October 3, 2021

SQL sever 2014 compatibility issue when TLS 1.0 and TLS 1.1 is turned off

As there has been several vulnerabilities of  TLS 1.0 and TLS 1.1 it is turned off by default,  even though SQL latest releases supports this SQL 2014 need to be Upgraded SQL Server and the client components to  handle TLS 1..

This link further describes the issue and you will need to inset the relevant service pack for SQL 2014 (2014 SP2 and 2014 SP3.)

 

 

Cheers

Samitha



Saturday, September 4, 2021

Converting ICollection to List

 

We can use the System.Linq namespace to convert ICollection<T> to List<T>    as dsiplays below.

//class definitions

public class Students
{
    public ICollection<Courses> Courses{get;set;}
}

public class Courses
{
    public ICollection<Students> Students{get;set;}
}

//end class definition

//create the List

using System.Linq; 


List<Students> listOfStudents= lstStudents.OfType<Students>().ToList();



cheers,

Samitha

Sunday, August 22, 2021

numeric textbox (first number can't be 0)

 I wanted to validate a textbox with a number not starting with zero. For this I used Regex as displayed below.

       Regex reg = null;
       reg = new System.Text.RegularExpressions.Regex("^[1-9]\d*$")
       return reg.IsMatch(str);

Cheers

Samitha

     

Saturday, August 7, 2021

.net URL validator

If you need to validate an URL starting with http or https you can simply use the following code snippet.

 

    Public Function ValidHttpURL(ByVal inputURL As String) As Boolean
        If Not Regex.IsMatch(inputURL, "^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$", RegexOptions.IgnoreCase) Then
            Return False
        End If
        Return True
    End Function

 

following are the valid test results for above regex

 https://www.google.com -PASS
http://www.google.com - PASS
www.google.com - FAIL
google.com - FAIL
htt://www.google.com - FAIL
http://google.com - PASS

cheers

Samitha

 



Friday, July 30, 2021

asp.net linkbutton open in a new window

 Asp.Net linkbutton can be made to work the same way as a normal hyperlink does. All we have to do is to attach a onClick attribute as displayed below.

 

 string strURL = "";
string strWinTitle = "";
string strWinProperty = "";


strURL =  "https://www.google.com/";
stTitle = "New Window";
strProperty = "toolbar=no,menubar=no,location=no";


linkBtn.Attributes.Add("onClick", "javascript:window.open('" + strURL + "','" + stTitle + "','" + strProperty + "');return false;")


Cheers,

Samitha

Saturday, June 26, 2021

SQL Server read XML attribute

Suppose you have a XML value stored in a table


<?xml version="1.0" encoding="utf-8"?>
<rootElement>
  <param name="p1" value="v1" />
  <param name="p2" value="v2" />
  ...
</rootElement>

 

We can get the value of the p1 parameter as follows


SELECT
  columnNAame.value('(/rootElement/param[@name="p1"]/@value)[1]', 'varchar(50)')
FROM  
  tableName

Notice that the correct datatype should be chosen within the query. If we have a integer value we have to use int as the datatype. In addition if long text is stored for the value proper data length needs to be given.

Cheers

Samitha

Sunday, June 13, 2021

Chrome complains about a missing source map

Recently I have added a Javascript library and found that  Chrome devtools keeps complaining about a missing source map

Initially I have referenced the .js fil as follows

<script src="https://cdn.jsdelivr.net/npm/@linways/table-to-excel@1.0.4/dist/tableToExcel.js"></script>

 The resolution was to replace the .js file with .min.js as follows

 <script src="https://cdn.jsdelivr.net/npm/@linways/table-to-excel@1.0.4/dist/tableToExcel.min.js"></script>

 

Cheers

Samitha

 


Sunday, May 30, 2021

ASP.NET MVC delete multiple records

 When it comes to deleting multiple records in ASP.NET MVC, you have two options,

 1. Connected mode

using (CourseContext db = new CourseContext())
    {
 
        List<Course> courses = db.Courses.Take(5).ToList();

        try
        { 

           db.Courses.RemoveRange(courses);
            db.SaveChanges();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
  

2. Disconnected mode

ListCourse> courses = new List<Course>();
courses.Add(new Course { CourseID = 1 });
courses.Add(new Course{ CourseID = 2 });
 
using (CourseContext db = new CourseContext())
    {
  
        try
        {

            db.Entry(Course).State= System.Data.Entity.EntityState.Deleted;
            db.SaveChanges();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
 
    }


Cheers,

Samitha

Saturday, May 15, 2021

readonly checkbox

 We can use javascript to make a checkbox read only(not disabled) as shown below


<input type="checkbox" checked onkeydown="return false;" onclick="return false;" />

if you want to disable check using keyboard modify above as follows.


<input type="checkbox" checked onclick="return false;" onkeydown="e = e || window.event; if(e.keyCode !== 9) return false;"/>


Cheer

Samitha

Sunday, May 2, 2021

Add icons to links using jquery

If you want to add icons besides your hyperlinks, jQuery can be used to do it as shown below

1. Define Syles

a.pdf { 
    background: url(images/pdf.png) no-repeat left center;
    padding-left: 20px;
    line-height: 16px; /* used to center the image with text */
}
 
a.txt { 
    background: url(images/txt.png) no-repeat left center;
    padding-left: 20px;
    line-height: 16px;
}

a.email {
    background: url(images/email.png) no-repeat left center;
    padding-left: 20px;
    line-height: 16px;
}

2. Add the style using jQuery DOM ready or pageLoad

$(function() {
 
    $("a[href$='.pdf']").addClass("pdf");
 
    $("a[href$='.doc'], a[href$='.txt'], a[href$='.rft']").addClass("txt");
 
    $("a[href^='mailto:']").addClass("email");
 

});

Cheers,

Samitha

Saturday, April 17, 2021

select all elements of a class, except one element

If we want to do some action on selected list of elements except one element in the list we can use jQuery as shown below.

 $(".parentElement:not(#childId)").actionName();

If you want to skip multiple elements

 $(".parentElement:not(#childId1,#childId2)").actionName();

Or you can use not()

$(".parentElement").not("#childId1").actionName();


Cheers,

Samitha

 

Saturday, March 27, 2021

Could not load file or assembly Operation is not supported. (Exception from HRESULT: 0x80131515)

 When running .Net applications you might encounter the following error

 Could not load file or assembly [assembly_name]Operation is not supported. (Exception from HRESULT: 0x80131515)

This error occurs as a  result of a untrusted downloaded file. Follow these simple steps to overcome the issue.


1. Right click the dll and select properties

2. Click unblock button/check box



Regards,

Samitha

Sunday, March 7, 2021

Export html table using jQuery

If you need to export HTML table data to Excel Table to Excel 2 plugin can be used.

Its pretty simple.  Just add

Include JS

<script type="text/javascript" src="../dist/tableToExcel.js"></script>

javascript

<script type="text/javascript">
  $(function () {
      $("#btnExport").click(function(){
        TableToExcel.convert(document.getElementById("table1"), {
            name: "download.xlsx",
            sheet: {
            name: "Sheet1"
            }
          });
        });
  });
</script>
 
Add html Button

<button id="btnExport">Export To Excel</button><br><br>
 
Regards
Samitha

Sunday, February 21, 2021

HTTP Error 500.52 - URL Rewrite Module Error

 When browsing website through IIS you might encounter the following error.

Website is not accessible: HTTP Error 500.52 - URL Rewrite Module Error 

Follow the below steps to resolve this issue.

  • open (IIS) Manager (in windows 10 type "inetmgr" in search windows
  • Navigate to Server name > Sites >your_website > URL rewrite
  • Select each rewrite rule and disable them (Right click  and disable)
  • Enable each rewrite rule (right click and clicking enable)

Cheers

Samitha


Tuesday, February 16, 2021

using carriage return in a HTML tooltip

 If you want to add a line break to HTML tooltip, just press ENTER at the end of each word. Ans example is shown.


<a href="#" title='This

tooltip

shows in

multiple

Lines'>link</a>

Saturday, January 30, 2021

Reset Linkedin link preview cache

 If you have shared your website or an article in LinkedIn and then you decided to change the link preview data then you will see that LinkedIn still displaying old content. This happens as e LinkedIn caches link preview data  for 7 days.

We can resolve this using the LinkedIn Post Inspector. Follow these steps to claerLinkedin link preview cache.

  1. Navigate tohttps://www.linkedin.com/post-inspector/
  2. Input your URL and click on Inspect. preview image will be updated
  3. Retry to share your URL on LinkedIn


 Cheers

Samitha


Tuesday, January 12, 2021

Textbox format number with two decimal places

 There can be many ways to display a number formatted in a textbox. Here I have used jQuery to achieve the expected result.


<script>

$(function () {

    $('#txtAmount').on('input', function(e) {

        if (/^(\d+(\.\d{0,2})?)?$/.test($(this).val())) {

            $(this).data('oldValue', $(this).val());

        } else {

            $(this).val($(this).data('oldValue') || '');

        }

    }).trigger('input');  

});

</script>

HTML

<input type="text"  id="txtAmount" placeholder="Enter Amount" />


Cheers

Samitha