Wednesday, July 22, 2020

How to check if a set of key and value already exists in a dictionary

Dictionary<int, string> dict = new Dictionary<int, string>();
// initial value for demonstration
dict.Add(1, "Test");
dict.Add(2, "Test2");

use below method to check whether a key is exist or not

  if (!dict.ContainsKey(1))
        dict.Add(1, "Test");

 if (!dict.ContainsValue("Test"))
        dict.Add(1, "Test");

Monday, June 29, 2020

Error when pasting text in IE

The code for the event handler that is called when pasting some text into the input control throws an error because 'e.originalEvent.clipboardData' or  "Invalid argument" is undefined in Internet Explorer.


This code applies the fix and works fine

_searchInput.on('paste', function (e) {
var data;
if (window.clipboardData && window.clipboardData.getData) { // IE
data = window.clipboardData.getData('Text');
}
else if (e.originalEvent.clipboardData && e.originalEvent.clipboardData.getData) { // other browsers
data = e.originalEvent.clipboardData.getData('text/plain');
}

Tuesday, February 25, 2020

disable Parameter_Sniffing at the database level

The Parameter_Sniffing option can be disabled using the below ALTER DATABASE statement:

ALTER DATABASE SCOPED CONFIGURATION SET PARAMETER_SNIFFING = OFF;



from the Options tab of the Database Properties window, by changing the Parameter_Sniffing value to OFF as follows:



Monday, February 24, 2020

search text file using c# and display the line number and the complete line that contains the search keyword


Sample Input:

just a sample line
another sample line
first matching Order line
not a match
...here's a Order  match
Order 123

foreach (var match in File.ReadLines(@"c:\LogFile.txt")
                          .Select((text, index) => new { text, lineNumber = index+ 1 })
                          .Where(x => x.text.Contains("Order ")))
{
    Console.WriteLine("{0}: {1}", match.lineNumber, match.text);
}



Output:

3: first matching Order line
5: ...here's a Order  match
6: Order 123

Sunday, February 23, 2020

How can i call a server side button click function from client side?

option is to simulate Click of a button using javascript.
<asp:Button ID="savebtn" runat="server" OnClick="savebtn_Click" style="display:none" />
HTML markup will look like this:

<button id="btnsave" onclick="fncsave()">Save</button>
now simulate a Click using JS

<script type="text/javascript">
     function fncsave()
     {
        document.getElementById('<%= savebtn.ClientID %>').click();
     }
</script>

Friday, February 21, 2020

How use hidden field value in updatepanel

Any asynchronous round trips to the server will cause only those controls within the UpdatePanelitself to update on the UI, so even though the code-behind runs and updates the hidden fields, on the front end they stay the same because they sit outside the panel.

Try moving the hidden fields within the <ContentTemplate> tag

<asp:UpdatePanel ID="upTripsGrid" runat="server" UpdateMode="Always">
  <ContentTemplate>
         <input type="hidden" runat="server" id="hidCurrentDate" value="" />
         <input type="hidden" runat="server" id="hidTripIds" value="" />
  </ContentTemplate>
</asp:UpdatePanel>

Wednesday, February 19, 2020

How do I copy to the clipboard in JavaScript?

html page: 

 <button onclick="copyToClipboard();return false">copy to the clipboard</button> 
 <asp:HiddenField runat="server" ID="hdnCopyContent" ClientIDMode="Static" value="clipboard in JavaScript" />

Javascript function

function copyToClipboard() {     
        var text = $("input[Id$='hdnCopyContent']").val();
        if (text != undefined || text != null || text != "") {       
            alert(text);
        }       
        if (window.clipboardData && window.clipboardData.setData) {
            // IE specific code path to prevent textarea being shown while dialog is visible.
            return clipboardData.setData("Text", text);

        } else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
            var textarea = document.createElement("textarea");
            textarea.textContent = text;
            textarea.style.position = "fixed";  // Prevent scrolling to bottom of page in MS Edge.
            document.body.appendChild(textarea);
            textarea.select();
            try {
                return document.execCommand("copy");  // Security exception may be thrown by some browsers.
            } catch (ex) {
                console.warn("Copy to clipboard failed.", ex);
                return false;
            } finally {
                document.body.removeChild(textarea);
            }
        }
    }

Change default Port on Next.js app

 If any other app or process is running on port 3000 , you will get this error in your terminal Port 3000 is already in use. error Command f...