Overblog Follow this blog
Administration Create my blog
Balavardhan Reddy Narani

GOTO statement in C#

May 20 2009 , Written by Balavardhan Reddy Published on #ASP.Net

The goto statement transfers the program control directly to a labeled statement. It takes one of the following forms:

goto identifier;
goto case constant-expression;
goto default;


identifier    A label.
constant-expression    A switch-case label.


In the first form, the identifier indicates a label located in the current body, the same lexical scope, or an enclosing scope of the goto statement.

A common use of goto is to transfer control to a specific switch-case label or the default label in a switch statement.

The goto statement is also useful to get out of deeply nested loops.

A warning message may be issued if the label has never been referenced in the program. For more information on labels

For an example of using goto to transfer control to a specific switch-case label, see the switch example.

The following example demonstrates using goto to break out from nested loops.

using System;
public class Ex_GOTO
   public static void Main()
      int x = 5, y = 1;
      int count = 0;
      string[,] myArray = new string[x,y];

      for (int i = 0; i < x; i++)
           for (int j = 0; j < y; j++)
                myArray[i,j] = (++count).ToString();      
     Console.Write("Enter the number to search for: ");

     string myNumber = Console.ReadLine();

     for (int i = 0; i < x; i++)
           for (int j = 0; j < y; j++)
                if (myArray[i,j].Equals(myNumber))
                      goto Found;

      Console.WriteLine("The number {0} was not found.", myNumber);
       goto Finish;

             Console.WriteLine("The number {0} is found.", myNumber);

             Console.WriteLine("End of search.");


Sample Output
Enter the number to search for: 4
The number 4 is found.
End of search.

Read more

Datetime Formates and Date time manipulations in SQL Server

May 19 2009 , Written by Balavardhan Reddy Published on #SQL Server2005

Different Datetime formates in sqlserver

print getdate()  -- May 19 2009 3:21AM


print convert(varchar(max),getdate(),101) -- 05/19/2009


print convert(varchar(max),getdate(),102) -- 2009.05.19


print  convert(varchar(max),getdate(),103) -- 19/05/2009


print convert(varchar(max),getdate(),104) -- 19.05.2009


print convert(varchar(max),getdate(),105)  --  19-05-2009

print convert(varchar(max),getdate(),106) -- 19 May 2009


print convert(varchar(max),getdate(),107) -- May 19, 2009


print convert(varchar(max),getdate(),108) -- 03:21:06


print convert(varchar(max),getdate(),109) -- May 19 2009 3:21:06:283AM


print convert(varchar(max),getdate(),110) -- 05-19-2009


print convert(varchar(max),getdate(),111) -- 2009/05/19


print convert(varchar(max),getdate(),112) -- 20090519


print convert(varchar(max),getdate(),113) -- 19 May 2009 03:21:06:283


print convert(varchar(max),getdate(),114) -- 03:21:06:283


print convert(varchar(max),getdate(),120) -- 2009-05-19 03:21:06


print convert(varchar(max),getdate(),121) -- 2009-05-19 03:21:06.283


print convert(varchar(max),getdate(),127) -- 2009-05-19T03:21:06.283

--Get current Year  

  print year(getdate())

--Get current Month

  print month(getdate())

--Get current day  

  print day(getdate())





print datepart(m,getdate())

print datepart(d,getdate())

print datepart(yy,getdate())




--date add

print dateadd(d,-1,getdate()) --get yesterday

print dateadd(d,1,getdate()) --get tomorrow




--Get end date of the month

print dateadd(d, -1,convert(datetime, convert(varchar,datepart(m,getdate())+1)+'/01/'+convert(varchar,datepart(yyyy,getdate()) )))

--Month Name

print DATENAME(month, GETDATE())

--Next year

print dbo.Date(year(getdate())+1, 1,1)

-- Previous Year
print dbo.Date(year(getdate())-1, 1,1)

--Get the all weeks of the year, i.e. Week start date and end date of the 53 weeks

declare @st datetime

declare @day varchar(10)

declare @nextdate int

declare @ws datetime

declare @we datetime

declare @enddate datetime

declare @date_int int

declare @weekid int

set @enddate = dbo.Date(year(getdate())+1, 1,1)

set @st = dbo.Date(year(getdate()), 1,1)

set @weekid = 1

select @day = datename(dw,@st)


create table #temp_weekdates(week_name varchar(10), week_sdate datetime,week_edate datetime)

while (@st < @enddate)


   IF (@day = 'Tuesday')

        set @nextdate = 5


  IF(@day = 'MONDAY')

      set @nextdate = 6


  IF(@day = 'WEDNESDAY')

      set @nextdate = 4


  IF(@day = 'THURSDAY')

      set @nextdate = 3


  IF(@day = 'FRIDAY')

     set @nextdate = 2


  IF(@day = 'SATURDAY')

     set @nextdate = 1


  IF(@day = 'SUNDAY')

    set @nextdate = 7


  set @ws = @st

  set @we = dateadd(day,@nextdate,@st)

  set @date_int = convert(varchar(22),@we,112)


if (@date_int > convert(varchar(22),@enddate,112))


declare @lastweekdays int

set @lastweekdays = 31 - day(@st)

set @we = dateadd(day,@lastweekdays,@st)


insert into #temp_weekdates values

set @st = dateadd(day,1,@we)

select @day = datename(dw,@st)

set @weekid = @weekid + 1


select * from #temp_weekdates

drop table #temp_weekdates





--All the Best

Read more