Friday, March 27, 2020

Generate number for item such as A0001,A0002 and so on..

public static string NextNumber(string number)
        {
            Regex re = new Regex(@"([a-zA-Z]+)(\d+)");
            Match result = re.Match(number);
            var newChar = string.Empty;
            var newNumber = 1;
            var nextChar = 'G';
            string alphaPart = result.Groups[1].Value;
            string numberPart = result.Groups[2].Value;

            if (alphaPart.Length == 1)
            {
                // if the number has ONE Alphabet.

                var letter = alphaPart.ToCharArray().First();

                // TO increment number parts;
                var numberAlone = Convert.ToInt32(numberPart);
                if (numberAlone == 9999)
                {

                    if (letter == 'Z')
                    {
                        var newFirstChar = 'A';
                        var newSecondChar = 'A';
                        newNumber = 1;
                        var padResult = newNumber.ToString().PadLeft(3, '0');
                        newChar = $"{newFirstChar}{newSecondChar}{padResult}";
                    }
                    else
                    {
                        // To incremenet Alpha part.
                        nextChar = (char)(letter + 1);
                        numberAlone = 1;
                        var paddedResult = numberAlone.ToString().PadLeft(4, '0');
                        newChar = $"{nextChar}{paddedResult}";
                    }
                }
                else
                {
                    nextChar = letter;
                    numberAlone += 1;
                    var paddedResult = numberAlone.ToString().PadLeft(4, '0');
                    newChar = $"{nextChar}{paddedResult}";
                }

            }
            else
            {
                // If the number has TWO Alphabet.

                // To incremenet Alpha part.
                var firstLetter = alphaPart.ToCharArray().First();
                var secondLetter = alphaPart.ToCharArray().Last();
                var numberAlone = Convert.ToInt32(numberPart);
                if (secondLetter != null)
                {
                    if (secondLetter == 'Z')
                    {
                        if (numberAlone == 999)
                        {
                            var newFirstChar = (char)(firstLetter + 1);
                            if (firstLetter == 'Z')
                            {
                                newFirstChar = 'A';
                            }

                            var newSecondChar = 'A';
                            newNumber = 1;
                            var paddedResult = newNumber.ToString().PadLeft(3, '0');
                            newChar = $"{newFirstChar}{newSecondChar}{paddedResult}";
                        }
                        else
                        {
                            numberAlone += 1;
                            var paddedResult = numberAlone.ToString().PadLeft(3, '0');
                            newChar = $"{firstLetter}{secondLetter}{paddedResult}";
                        }
                    }
                    else
                    {
                        if (numberAlone == 999)
                        {
                            var newSecondChar = (char)(secondLetter + 1);
                            newNumber = 1;
                            var paddedResult = newNumber.ToString().PadLeft(3, '0');
                            newChar = $"{firstLetter}{newSecondChar}{paddedResult}";
                        }
                        else
                        {
                            numberAlone += 1;
                            var paddedResult = numberAlone.ToString().PadLeft(3, '0');
                            newChar = $"{firstLetter}{secondLetter}{paddedResult}";
                        }
                    }
                }
            }

            return newChar;
        }

Friday, September 7, 2012

Drawing a circle in SQL Server...

I've just started playing with the Geo spatial data types in SQL Server Express.
The first thing I wanted to do was draw a circle.  How difficult could it be?

I could not find an example on the interweb - so I put one together.

-- Draw Circle

declare @RecCount float = 360
declare @count int = 0
declare @size int = 1
declare @tmpTable table ( geo geometry)
while(@count < @RecCount)
begin
insert into @tmpTable
SELECT geometry::STGeomFromText('POINT('
   +  cast(SIN(RADIANS((CAST(@count AS FLOAT)
            /CAST(@RecCount AS FLOAT))
            * 360)) * @size as varchar)
   + ' '
   + cast(COS(RADIANS((CAST(@count AS FLOAT)
            /CAST(@RecCount AS FLOAT))
            * 360)) * @size as varchar)
   + ')',4326) AS Posn
set @count = @count + 1
end


select geo from @tmpTable






Now choose the 'Spatial Results' tab - you might have never seen this before. It only appears if the results set has a geospatial data type in it.


Draw a Circle

The observant of you might notice, it's not actually a circle.

Happy Coding...!!!






Change Cursor to hand on mouseover using JQuery...



When ever we need to change the mouse cursor using JQuery then we can do it very easily.


In my today's post I will show you how can we change the mouse cursor to the Hand type cursor while user hover mouse on the particular element.
It is very simple just look at the below JQuery code.
 



My Code:


$(document).ready(function()
  {
    $("#elementid").hover(function()
     {
       $(this).css("cursor", "hand");
     });
});


In above code "elementid" is the name of the element on which you want a mouse pointer as a hand while hovering a mouse over it.

Happy Coding...!!!

Wednesday, July 25, 2012

Disable copy paste right click using javascript in asp.net TextBox.

This blog defines How to Disable copy paste and right click using javascript in asp.net TextBox. We make a function in JavaScript to disable right click. When we create right click on the textbox message will be generate to show disable right click.


we can achieve this in 2 ways

1. use this method when u don't want any alerts or message

<asp:TextBox ID="TextBox1" runat="server"
oncopy="return false"
onpaste="return false"
oncut="return false">
asp:TextBox>


2. If you want to show alerts than use this method instead

Right this javascript function in the head section of aspx page, in this function we are disabling right mouse click and ctrl keys

<head runat="server">
<title>Untitled Pagetitle>
<script language="javascript">
function DisableRightClick(event)
{
//For mouse right click 
if (event.button==2)
{
alert("Right Clicking not allowed!");
}
}
function DisableCtrlKey(e)
{
var code = (document.all) ? event.keyCode:e.which;
var message = "Ctrl key functionality is disabled!";
// look for CTRL key press
if (parseInt(code)==17)
{
alert(message);
window.event.returnValue = false;
}
}
script>
head>

Now use this function on the textbox which we want to disable copy paste and right clicking

<body>
<form id="form1" runat="server">
<div>
<strong>
Right click disabled textbox
 strong> <br />
<asp:TextBox ID="TextBoxCopy" runat="server"
onMouseDown="DisableRightClick(event)">
asp:TextBox><br />
<br />
<strong>Ctrl key strong>disabled<br />
<asp:TextBox ID="TextBox2" runat="server"
onKeyDown="return DisableCtrlKey(event)">
asp:TextBox><br />
<br />

Another method to disable<strong> Cut,Copy and paste
strong>in textbox<br />
<br />
<asp:TextBox ID="TextBox1" runat="server"
oncopy="return false"
onpaste="return false"
oncut="return false">
asp:TextBox>
form>
body>

I hope this could be useful for you.

Thanks.

Prevent Cut, Copy and Paste Operations in an ASP.NET TextBox using jQuery

In this article, I will demonstrate how to prevent users from doing Cut, Copy and Paste operations in an ASP.NET TextBox using jQuery.

The event handling approach in jQuery begins by attaching a handler to an event. The jQuery bind() event method does exactly the same and binds one or more events to a handler. The signature of the bind() method is as follows:

bind(eventType,data,handler)
The parameters of the bind() method are as follows:
eventType is a string holding a JavaScript event type such as click, focus, keyup etc.
data is some optional data you want to pass to the handler
handler is the event handler function which will execute when the event is triggered
Let us quickly jump to the solution and see how we can use the bind() event method to write minimal code to prevent cut, copy and paste operations on the textbox.
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
    <title>Prevent Cut, Copy and Paste Operations in a TextBox title>
    <script type='text/javascript' src='http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js'>   
    </script>
    <script type="text/javascript">
     $(function() {
     $('input[id$=tb1]').bind('cut copy paste', function(e) {
     e.preventDefault();
     alert('You cannot ' + e.type + ' text!');
      });
    });
   
    script>
    <body>
    <form id="form1" runat="server">
    <div class="bigDiv">
    <h2>Prevent Cut, Copy and Paste Operations in a TextBox

    :TextBox ID="tb1" runat="server" Text="Text which cannot be copied/cut/pasted"
    ToolTip="Try Copy/Cut/Paste in textbox"/>

   


   
   
Observe how convenient it is to use jQuery to list multiple events (cut, copy, paste) together and bind it to a handler as shown below.
$('input[id$=tb1]').bind('cut copy paste', function(e) {});
If the user performs any of these events on the textbox (tb1), the default behavior is prevented using e.preventDefault() and the user is alerted with a message. The e.type describes the type of event performed.
$('input[id$=tb1]').bind('cut copy paste', function(e) {
e.preventDefault();
alert('You cannot ' + e.type + ' text!');
});

CutCopyPaste
This piece of ‘minimal’ code handles a typical requirement where you are asked to ‘confirm’ an email address and want the user to type it manually, instead of copying and pasting it.
Note: Text can be edited in this textbox, but cut/copy/paste actions are prevented
This demo has been tested on IE7, IE8, Firefox 3, Chrome 2 and Safari 4.
I hope you liked this article and I thank you for viewing it.