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

Monday, December 14, 2020

Convert .net object into a JSON string

We can use JsonWriter to return a JSON representation of any .Net object as displayed below.


public class Person

    {

        public Int Id   {set ; get;}

        public String Name{set ; get;}

       public List<PhoneNumbers> Contacts{set ; get;}

         public String ToJSONRepresentation()

        {

            StringBuilder stringBuilder = new StringBuilder();

            JsonWriter jsonWriter = new JsonTextWriter(new StringWriter(stringBuilder));


            jsonWriter.Formatting = Formatting.Indented;

            jsonWriter.WriteStartObject();

            jsonWriter.WritePropertyName("id");

            jsonWriter.WriteValue(this.Id);

            jsonWriter.WritePropertyName("name");

            jsonWriter.WriteValue(this.Name);


            jsonWriter.WritePropertyName("phonenumbers");

            jsonWriter.WriteStartArray();


            int i=0;


            for (i = 0; i < Contacts.Count; i++)

            {

                jsonWriter.WriteStartObject();

                jsonWriter.WritePropertyName("homePhone");

                jsonWriter.WriteValue(addresses[i].homePhone);

                jsonWriter.WritePropertyName("workPhone");

                jsonWriter.WriteValue(addresses[i].workPhone);

                jsonWriter.WritePropertyName("mobilePhone");

                jsonWriter.WriteValue(addresses[i].mobilePhone);

                jsonWriter.WriteEndObject();

            }

            jsonWriter.WriteEndArray();

            jsonWriter.WriteEndObject();

            return stringBuilder.ToString();

        }

      }

}


Cheers

Samitha

Friday, November 20, 2020

SQL get specific element Count in XML

 Consider this XML


Suppose you want to get count of student ids. You can use SQL to get the count as follows.

declare @xml XML;

set @xml ='<Students>

    <Student>

        <Id>001</Id>

        <Fname>Leesa</Fname>

        <Sname>Humpry</Sname>

    </Student>

    <Student>

        <Id>002</Id>

        <Fname>Clara</FName>

        <Sname>Margrette</Sname>

    </Student>

  </Students>';

select @xml.value('count(/Students/Student/Id)', 'INT') AS 'Count'

Cheers

Samitha

Sunday, November 8, 2020

sql server remove duplicates from query

You can use SQL row_number() function to remove duplicate rows from a query.

Create table Employees (fname varchar(100), lname varchar(100))

Insert into Employees Values 
               ('Tim', 'May')
             , ('Clara', 'Magrette') 
             , ('Clara', 'Magrette')
             , ('Jeff', 'Lawry') 
             , ('Brian', 'Adams')
             , ('Paul', 'Oliver')

As you can see there are duplicate records in the Employees table,

Select * from(
SELECT fname 
       ,lname 
       ,row_number() over (partition by fname , lname order by fname , lname ) as rownum 
FROM Employees 
) result
where rownum= 1 -- selects DISTINCT Employees only

Cheers,
Samitha

Wednesday, October 28, 2020

Remove all table rows except first

You can use jQery to easily remove rows from a given table.

//using jQuery find

$(function() {
   $("#tableId").find("tr:gt(0)").remove();
});
//using empty
$(function() {
  $("#tableId > tbody").empty();
});
The second method is more efficient than the first approach.
Regards,
Samitha

Saturday, October 17, 2020

.Net get plain text from HTML

There can be situations where you want to get plain text from HTML content.  We can use Regex and HttpUtility.HtmlDecode as the easiest way to get plain text shown below.

String plainText =HttpUtility.HtmlDecode( Regex.Replace(HtmlString, "<(.|\n)*?>", "") );


Cheers,

Samitha

Thursday, October 1, 2020

Role does not have permission for this action in QNA Maker

 When you are working with Azure QNA Maker you might get the message "role does not have permission for this action in QNA Maker"

Once you have finished creating  QNA,  the entire page (not the  Refresh Button on the QnAMaker.ai/Create page)  needs to be refreshed and the above message will be disappeared.


Cheers

Samitha