Nilesh Ghadge


Leave a comment

[FIX] The source was not found, but some or all event logs could not be searched.  Inaccessible logs: Security

I recently lost a good amount of hair on trying to resolve an issue while trying to execute an application. This application was running successfully on one server and I was trying to configure it to run on this new server too. While doing so, the application refused to run and did prompt me this follow exceptions. After tons and tons of digging (includes pulling out my hair too), I found that solution was a simple one line command.

Unhandled Exception: System.Security.SecurityException: The source was not found, but some or all event logs could not be searched.  Inaccessible logs: Security.     at System.Diagnostics.EventLog.FindSourceRegistration(String source, String machineName, Boolean readOnly, Boolean wantToCreate)     at System.Diagnostics.EventLog.SourceExists(String source, String machineName, Boolean wantToCreate)     at System.Diagnostics.EventLog.SourceExists(String source)

The  FIX:

Run the following line of code (replace MyEventSourceName to your application name) in an elevated PowerShell command prompt.

[System.Diagnostics.EventLog]::CreateEventSource(“MyEventSourceName”, “Application”)


Leave a comment

Email Validation Regex

Every now and then I come across a situation where I need to use regex and especially during email validations. I think I have found a good email validation regex and therefore sharing it.

Regex:

"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z"

C# Code Snippet:

var emailId = "test@test.com";
bool isEmail = Regex.IsMatch(emailId.ToLower().Trim(), @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase);


Leave a comment

How to run all unit tests contained in one file in Visual Studio?

Get the file path of the .cs file from where you want to run the tests. This can be done by right-clicking the file in the solution explorer and choosing the ‘Copy Path’ option

Open Test Explorer.

Click on the Search Bar and select ‘File Path’ filter.

Enter the copied file path and press Enter.

All the tests from that file will be visible and you may run those tests.

ts


Leave a comment

How to display a loading/hourglass gif in javascript for AJAX events

Suppose you need to initiate a request from a web service and while you await its response, you want to display an hourglass or a loading gif indicating the system is still processing that request. In this case, all you have to do is link an gif image to a div and show/hide based on how you want it.

 

To choose from various loading gifs, I find this link very good – http://ajaxload.info/

You can create a gif you like here and use it in your code.

 


 

$(document).ready(function () {
$(‘body’).append(‘<div id=”ajaxBusy”><p><img src=”loading.gif”></p></div>’);

$(‘#ajaxBusy’).css({
display: “none”,
margin: “0px”,
paddingLeft: “0px”,
paddingRight: “0px”,
paddingTop: “0px”,
paddingBottom: “0px”,
position: “absolute”,
left:”28%”,
right: “10px”,
top: “40%”,
width: “auto”,
height: “auto”
});

});

 


 

In AJAX code:

var ajaxSettings = {

‘url’ : ‘http://dummy.com/dummWebService&#8217;,

‘beforeSend’: function (request){

…….

$(‘#ajaxBusy’).show();                                       //this will show the gif before the request is being sent out

};

 

$.ajax(ajaxSettings)

.done(function (data, statusText, jqxhr) {

})

.fail(function (jqxhr, statusText, errorThrown) {

})

.always(function (data, status, jqxhr) {

$(‘#ajaxBusy’).hide();                     //this will hide the gif once the request has completed

})

},


Leave a comment

How to replace single quote(‘) in javascript for SQL queries

Scenario: You need to query upon a value (say a name) having a single quote(‘) using a SQL query from javascript/jquery?

For example, you need to search by a name like Mc’Daniel or something like that and since it has a single quote in the name, it breaks your SQL query, replacing the single quote by a two single quotes would allow the query to run correctly on SQL.

 

var name = $.trim($(‘#txtBox_firstName’).val());

var sqlQuery = ” SELECT * FROM dbo.dummyTable WHERE firstName = ‘”  + name.replace(/’/g, “””)) + “‘”

 


Leave a comment

Get a substring between two characters in javascript

Scenario: You have a string and you have to get a substring between those two characters

Example String: sip:1234@dummy.domain.com:8080

Expected output: 1234


 

Javascript:

var testString=”sip:1234@dummy.domain.com:8080″;

var startIndex = testString.indexOf(“:”) + 1;

var endIndex = testString.indexOf(“@”, startIndex);

var resultString = testString.substring(startIndex, endIndex);


Leave a comment

Allow only numbers in a textbox in javascript

<html>
    <head>
        <script type=”text/javascript”>
            //the below function will return true if the pressed key is between 0-9 else will return false
            function isNumberEvent(evt){
             var charCode = (evt.which) ? evt.which : event.keyCode
             if (charCode > 31 && (charCode < 48 || charCode > 57))
                return false;
             return true;
          }      
        </script>
    </head>
   <body>
        <div>
            <input type=”text” id=”txtBox_forNumbers” onkeypress=”return isNumberEvent(event)” />
        </div>
   </body>
</html>


Leave a comment

Controlling accordion functionality by checkboxes

Scenario: Suppose you need an accordion which has check boxes in them and checking the boxes should take you to the next div in the accordion. Also, checking them off should change the color of the div headers and when all the divs in the accordion are checked off, the next/submit button should be enabled.


 

Jquery:

$(‘input[name=CheckBoxGroup]’).click(function () {
     ChangeDivColor();
     CheckNextEnable();
});

$(function () {
                $(‘#accordion’).accordion({
                    heightStyle: “content”,
                    activate: function (event, ui) {
                        var active = $(“#accordion”).accordion(“option”, “active”);
                        $(‘#chkBox_’ + (active + 1)).prop(“checked”, false);
                        $(‘#chkBox_’ + (active + 1)).parent().parent().prev().css(“background-color”, “transparent”);
                        CheckNextEnable();
                    }
                });
            });

function ChangeDivColor() {
            if ($(‘#chkBox_1’).is(‘:checked’)) {
                $(‘#chkBox_1’).parent().parent().prev().css(“background-color”, “green”);

            } else {
                $(‘#chkBox_1’).parent().parent().prev().css(“background-color”, “transparent”);
            }

            if ($(‘#chkBox_2’).is(‘:checked’)) {
                $(‘#chkBox_2’).parent().parent().prev().css(“background-color”, “green”);
            } else {
                $(‘#chkBox_2’).parent().parent().prev().css(“background-color”, “transparent”);
            }
            var active = $(“#accordion”).accordion(“option”, “active”);
            $(“#accordion”).accordion(“option”, “active”, active + 1);
        }

        function CheckNextEnable() {
            if (($(‘#chkBox_1’).is(‘:checked’)) && ($(‘#chkBox_2’).is(‘:checked’)) ) {
                $(‘#nextButton’).removeAttr(‘disabled’);
            } else {
                $(‘#nextButton’).attr(‘disabled’, ‘disabled’);
            }
        }


HTML Code:

<div id=”accordion”>                                                                            
    <h3></h3>
       <div>
           <span>Text 1</span>
           <div style=”text-align:right”><input id=”chkBox_1″ type=”checkbox” name=”CheckBoxGroup” value=”Check 1″ /></div>
        </div>                                      
     <h3></h3>
        <div>
             <span >Text 2</span>
              <div style=”text-align:right”><input id=”chkBox_2″ type=”checkbox” name=”CheckBoxGroup” value=”Check 2″ /></div>
         </div>                                      
 </div>

 

 


Leave a comment

Disable back button from browser

Suppose there are two pages, page1.html and page2.html and the navigation is page1->page2

Scenario 1:
When you have adequate information that you no longer want to come back from page2 once you leave page1.
<script>
window.history.forward();
function noBack(){window.history.forward();}
</script>

<body onload="noBack();">
</body>

Scenario 2:
You are on page2 and you have to make a decision whether or not to go back to page 1
<script>
window.location.hash="no-back-button"; 
window.location.hash="Again-No-back-button";//again because google chrome don't insert first hash into history 
window.onhashchange=function(){window.location.hash="no-back-button";}
</script>


Leave a comment

Boondi Raita

Boondi Raita

Image
This is the one I got from my friend, Sheetal, who described me how to prepare it. The below measurements would suffice for 3 servings.

Ingredients:
Boondi: 1 bowl
Yogurt(Dahi) – 3 bowls
Chilli Powder: 1 1/2 teaspoon
Jeera Powder or Crushed Jeera (Cumin Seeds): 1 teaspoon
Salt as per taste

Preparation Time:
10 minutes

Procedure:
1. Soak 1 bowl of boondi in water for about 5-7 minutes and then take out all the water from it.
2. Take 3 bowls of yogurt(dahi) and mix 1 teaspoon of crushed jeera / jeera powder and 1 1/2 chilli powder.
3. Mix half-cup of water to dahi and then add the soaked boondi to it.
4. Serve with coriander/cilantro.

Bon Appétit!!

Design a site like this with WordPress.com
Get started