Sunday, 7 June 2015

How to create instance of an interface

In this article, i am going to show what is the way to create an instance of a interface. Actually, there is no direct way to create an instance of a interface. This is because they are incomplete (like templates) and creation of an object is completely meaningless here. If we want to do something like that, we must create an instance of class / type that implements that interface. For Example :- ILIST(T) interface provided in System.Collections.Generic namespace showing the below error while i was trying the same.


As i told you before, we must have to use it as a variable which points to a class which implements that interface to workaround this kind of problem. Something like that:-


Here, List(T) is a class which implementing ILIST(T) interface. Please remember that, ILIST(T) and ILIST are not same, the only difference is that first one is generic in type and we can pass any type in place of T. 

That’s it!!…..Happy Programming...




Saturday, 31 January 2015

Why SQL Server allows NULLs for the BIT datatype

During the interview my friend was asked, why SQL Server allows NULLs for the BIT datatype.
I like to explain things with analogies and this is no different. Consider a table holding job applicants details for a Job Board Website. Something Like this:-


CREATE TABLE dbo.tblJobApplicants (
  CandidateID int,
  JobID int,
  DateApplied date,
  isApproved bit
)

-- Applicant is approved
INSERT INTO dbo.tblJobApplicants
  SELECT
    1,
    1,
    GETDATE(),
    1

-- Applicant is rejected
INSERT INTO dbo.tblJobApplicants
  SELECT
    2,
    1,
    GETDATE(),
    0

-- Applicant is in progress means just applied
INSERT INTO dbo.tblJobApplicants
  SELECT
    3,
    1,
    GETDATE(),
    NULL
The key field to note here is of course the BIT field which indicates the approval or disapproval of the applicant. Obviously, when a candidate applies to a job, the status of the applicant isn't known  means the applicant has not been accepted, nor rejected. I think, it is the only end of the application in which this field can make a meaningful value. Hopefully, this example helps explain just when you might require a NULL bit field.

That’s it!!…..Happy Programming...

Saturday, 17 January 2015

How to compare two dates in ASP.NET using JAVASCRIPT

In this article, i am going to show how to validate two dates in asp.net using JavaScript. It also make sure that difference between two dates should not be more than n number of days. In my case, i specified 7 but you can specify it according to your need.

function ValidateDate() {
                var startDate = document.getElementById("<%=txtDatefrom.ClientID%>");
                var endDate = document.getElementById("<%=txtDateTo.ClientID%>");
                //Get 1 day in milliseconds
                var oneDay = 1000 * 60 * 60 * 24;
                
                // to make sure end date is greater than or equal to start date
                if ((Date.parse(startDate.value) > Date.parse(endDate.value))) {
                    $(endDate).css('border', '1px solid red');
                    return false;
                }

                else {
                    // to make sure difference between two dates should not be more than 7 days
                    if (Math.round((Date.parse(endDate.value) - Date.parse(startDate.value)) / oneDay) > 7) {
                        $(endDate).css('border', '1px solid red');
                        return false;
                    }
                    $(endDate).css('border', '1px solid #d3cfc7');
                    return true;
                }
            }

Sunday, 9 November 2014

Validate File format and Size in ASP.NET using JAVASCRIPT

In this article, i am going to show how to valid file type and size of the file upload control in asp.net using JavaScript.


JavaScript Function

  <script type="text/javascript">
        // allowed file formats
        var validFilesType = ["bmp", "gif", "png", "jpg", "jpeg"];
        function ValidateFileTypeWithSize() {
            var fluploadImage = document.getElementById("<%=fluploadImage.ClientID%>");
            var lblMsg = document.getElementById("<%=lblMsg.ClientID%>");
            lblMsg.style.color = "red";
            lblMsg.innerHTML = '';
            var path = fluploadImage.value;
            // to get file extenstion like bmp, gif etc.
            var extension = path.substring(path.lastIndexOf(".") + 1, path.length).toLowerCase();
            var isValidFile = false;
            for (var i = 0; i < validFilesType.length; i++) {
                if (extension == validFilesType[i]) {
                    isValidFile = true;
                    // mention maximum size 
                    if (fluploadImage.files[0].size > 4194304) {
                        fluploadImage.value = ''; // to clear fileuploader content
                        lblMsg.innerHTML = "Each image file can't exceed 4 MB";
                    }
                    break;
                }
            }

            // execute only in case of any file format which was not mentioned in 'validFilesType' variable
            if (!isValidFile) {
                fluploadImage.value = ''; // to clear fileuploader content
                lblMsg.innerHTML = "File Not Valid. Please upload a File with" +
                 " extension:\n\n" + validFilesType.join(", ");
            }
            return isValidFile;
        }
    </script>


Controls

  <asp:FileUpload ID="fluploadImage" onchange="ValidateFileTypeWithSize()" runat="server" />
            <asp:Label ID="lblMsg" runat="server"></asp:Label>
Output


That’s it!!…..Happy Programming...

Saturday, 25 October 2014

Something about Empty String

In this article, i am going to explain meaning of an Empty String in int type column.
create table tblEmptyString (SrNo int null)
insert into tblEmptyString (SrNo) values (1)
insert into tblEmptyString (SrNo) values (2)
insert into tblEmptyString (SrNo) values ('')
insert into tblEmptyString (SrNo) values (null)

select * from tblEmptyString

Output

As we can see, implicit conversion of the empty string to a value of 0 happened here. These Issues sometimes can create problems for us. So from now, we have to care about Empty String more.

That’s it!!…..Happy Programming...

Saturday, 18 October 2014

CSS tooltip on hover

In this article, i am going to show how to apply custom styles on Tooltip.

Aspx Code

   <table style="width: 300px;">
        <tr>
            <td>Username:
            </td>
            <td>
                <a href="#" accesskey="Please enter your username" class="tooltipshow">
                    <input id="txtUsername" type="text" /></a>
            </td>
        </tr>
        <tr>
            <td>Password:
            </td>
            <td>
                <a href="#" accesskey="Please enter your password" class="tooltipshow">
                    <input id="txtPassword" type="text" /></a>
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <input id="btnSubmit" type="button" value="Login" />
            </td>
        </tr>
    </table>

 CSS Code

.tooltipshow {
display:inline;
position:relative;
text-decoration:none;
top:0;
left:10px;
}

.tooltipshow:hover:after {
background:rgba(0,0,0,.8);
border-radius:5px;
top:-5px;
color:#fff;
content:attr(accesskey);
left:160px;
position:absolute;
z-index:98;
width:150px;
padding:5px 15px;
}

.tooltipshow:hover:before {
border:solid;
bottom:20px;
content:"";
left:155px;
position:absolute;
z-index:99;
top:3px;
border-color:transparent #000;
border-width:6px 6px 6px 0;
}

In CSS, we have set the content attribute to content: attr(accesskey); this property will display the tooltip by using the accesskey attribute of the anchor tag. Whatever we pass in to the accesskey attribute of the anchor tag it will be displayed as a tooltip. Have something to add to this post? Share it in the comments.
 Output

That’s it!!…..Happy Programming...