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