Tuesday, 29 April 2014

How to get previous page URL using ASP.NET with C#

How to get previous page URL using ASP.NET with C# ?


string referer = Request.UrlReferrer.ToString();

Using Request.UrlReferrer, we can get the Previous page URL of the current request. Previous Page information is available in the Request.UrlReferrer property only if user is redirected to the current page from some other page and is available in the !Page.IsPostBack of Page Load 
event of the form. 

One thing to remember is that UrlReferrer will change to your pagename when you post back
 to the server.. so you should maybe store the value in ViewState on the first page load of the
 page if you are wanting to redirect the user to the previous page after a couple postbacks to
 the same page.

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) //check if the webpage is loaded for the first time.
        {
            ViewState["PreviousPageURL"] = Request.UrlReferrer; 
            //Saves the Previous page url in ViewState
        }
    }

If you want to reach the previous page ,

if (ViewState["PreviousPage"] != null) //Check if the ViewState contains Previous page URL
        {
            Response.Redirect(ViewState["PreviousPage"].ToString());
           //Redirect to Previous page by retrieving the PreviousPage Url from ViewState.
        }

ViewState is used for retaining values between multiple requests for the same page. This is the default method that the page uses to preserve page and control property values between
 round trips.

The situations where it does work include the following methods of a browser loading 
a URL:
  • clicking on a straight HTML <a href> link;
  • submitting a form, using POST or GET, from a submit button, <input type=image> or client-side script (form.submit())

The situations where it doesn't work:
  • using Response.Redirect / Server.Transfer;
  • clicking on a Favorite, History, or the recently-typed URLs list;
  • clicking on 'Home' in IE's toolbar, or an item in IE's 'Links' toolbar;
  • using location.href or location.replace() in client-side JScript/JavaScript/VBScript;
  • using HierMenus (details);
  • typing the URL directly in the browser and hitting Enter or clicking 'Go';
  • launching a clickable URL from an e-mail or MS Office document;
  • using Response.AddHeader  or <meta http-equiv=refresh> to redirect;
  • loading the URL with XML

Monday, 28 April 2014

HOW TO ALIGN DIFFERENT WAYS IN WPF?

HOW TO ALIGN DIFFERENT WAYS IN WPF?


GRID 

 In Grid alignment we divide the screen in to static rows and columns like HTML tables and position elements in those rows and column area.


<Grid>

<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>

<Label Grid.Column="0" Grid.Row="0" Background="Red">1st column 1st row </Label>
<Label Grid.Column="1" Grid.Row="0" Background="LightGreen">2nd Column 2nd row</Label>
<Label Grid.Column="0" Grid.Row="1" Background="Red">1st column 2nd row</Label>
<Label Grid.Column="1" Grid.Row="1" Background="LightGreen">2nd Column 2nd row</Label>

</Grid>





STACK PANEL

Arranges control in vertical or horizontal format.

<StackPanel Orientation="Vertical">
      <Label Background="Red">Red </Label>
      <Label Background="LightGreen">Green </Label>
      <Label Background="LightBlue">Blue </Label>
      <Label Background="Yellow">Yellow </Label>
</StackPanel>





WRAP PANEL

Aligns elements in a line until the border is hit , then wraps in to the next line.

<WrapPanel Orientation="Horizontal">
<Label Width="125" Background="Red">Red 1</Label>
<Label Width="100" Background="LightGreen">Green 1</Label>
<Label Width="125" Background="LightBlue">Blue 1</Label>
<Label Width="50" Background="Yellow">Yellow 1</Label>
<Label Width="150" Background="Orange">Orange 1</Label>
<Label Width="100" Background="Red">Red 2</Label>
<Label Width="150" Background="LightGreen">Green 2</Label>
<Label Width="75" Background="LightBlue">Blue 2</Label>

</WrapPanel>






DOCK PANEL

 Aligns controls in five different regions: top, bottom, left, right and center.



<DockPanel>
<Label DockPanel.Dock="Top" Height="100" Background="Red">Top 1</Label>
<Label DockPanel.Dock="Left" Background="LightGreen">Left</Label>
<Label DockPanel.Dock="Right" Background="LightCyan">Right</Label>
<Label DockPanel.Dock="Bottom" Background="LightBlue">Bottom</Label>
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center"> Demo of Dock panel</TextBlock>

</DockPanel>


CANVAS

 Positions elements absolutely using co-ordinates.

<Canvas Margin="273,130,144,99">
<TextBlock> Canvas position </TextBlock>

</Canvas>



Sunday, 27 April 2014

How to find next month first day in c#?

How to find next month first day in c#?



.ASPX


<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lbl1" runat="server" Text="VamsiDotnetStudio" Font-Bold="true" Font-Size="XX-Large"
            ForeColor="Crimson"></asp:Label>
    </div>
    <br />
    <br />
    <div>
        <asp:Button ID="Button1" runat="server" BackColor="#66CCFF"  Font-Bold="true" OnClick="Button1_Click"
            Text="Click" />
    </div>
    <br />
    <asp:Label ID="Label1" runat="server" BackColor="Yellow" Font-Bold="true" Font-Names="arial" Text="Check Here"></asp:Label>
    </form>
</body>

.ASPX.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        {
            DateTime today = DateTime.Today;
            DateTime tempDate = today.AddMonths(1);
            DateTime tempDate2 = new DateTime(tempDate.Year, tempDate.Month, 2);
            DateTime nextmonthfirstday = tempDate2.AddDays(-1);
           
            Label1.Text = "Today : " + today.ToLongDateString();
            Label1.Text += "<br /><br />Next month first day = ";
            Label1.Text += nextmonthfirstday.ToLongDateString();
        }
    }
}


 OUTPUT:




Tuesday, 22 April 2014

HOW TO PRINT DIAMOND SHAPE WITH STARS IN C SHARP

HOW TO PRINT DIAMOND SHAPE WITH STARS IN C SHARP ?

DESCRIPTION: 

IN THIS I AM GOING TO SHOW HOW TO DISPLAY DIAMOND SHAPE IN CONSOLE APPLICATION WITH STARS IN C#

OPEN CONSOLE APPLICATION 
(Program.cs)


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DIMOND
{
    class Program
    {
        static void Main(string[] args)
        {

            int i, j, n;
            Console.WriteLine("Enter no for printing star in diamond shape");
            n = int.Parse(Console.ReadLine());
            for (i = 1; i <= n; i++)
            {
                for (j = n; j >= i; j--)
                {
                    Console.Write(" ");
                }
                for (j = 1; j <= i; j++)
                {
                    Console.Write("*");
                }
                for (j = 1; j <= i; j++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }


            for (i = 1; i <= n; i++)
            {
                for (j = 0; j <= i; j++)
                {
                    Console.Write(" ");
                }
                for (j = n; j >= i; j--)
                {
                    Console.Write("*");
                }
                for (j = n; j >= i; j--)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}

OUTPUT:







HOW TO HIGHLIGHT THE DATE IN CALENDAR USING DATABASE ?

HOW TO HIGHLIGHT THE  DATE  IN CALENDAR USING DATABASE ?


DESCRIPTION:
 INN THIS POST I AM GOING TO TELL HOW TO HIGHLIGHT THE DATE 
IN CALENDER CONTROL USING SQL SERVER 


CREATE DATA BASE :

CREATE DATABASE EMP
USE EMP

CREATE TABLE EMP
(
      Task_Id int Identity(1,1),
      Task_Date Date,
    Posted_On Date
)


select * From EMP


Insert into EMP values('2014-04-30',GETDATE())
INSERT INTO  EMP VALUES('2014-07-21',GETDATE())
Insert into EMP values('2014-04-12',GETDATE())
Insert into EMP values('2014-04-06',GETDATE())


CREATE .ASPX :


<body>
    <form id="form1" runat="server">
    <div>
     <asp:Calendar ID="Calendar1" runat="server" SelectedDate=" " OnDayRender="Calendar1_DayRender" OnVisibleMonthChanged="Calendar1_VisibleMonthChanged">
        </asp:Calendar>
        <br />
        <br />
        <asp:Label ID="lblerrmsg" runat="server" ForeColor="Red"></asp:Label>
   
    </div>
    </form>
</body>


CREATE ASPX.CS :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected DataSet dsHolidays;
    //MySqlConnection objConn;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Calendar1.VisibleDate = DateTime.Today;
            FillHolidayDataset();
        }
    }

    protected void FillHolidayDataset()
    {
        DateTime firstDate = new DateTime(Calendar1.VisibleDate.Year,
            Calendar1.VisibleDate.Month, 1);
        DateTime lastDate = GetFirstDayOfNextMonth();
        dsHolidays = GetCurrentMonthData(firstDate, lastDate);
    }

    protected DateTime GetFirstDayOfNextMonth()
    {
        int monthNumber, yearNumber;
        if (Calendar1.VisibleDate.Month == 12)
        {
            monthNumber = 1;
            yearNumber = Calendar1.VisibleDate.Year + 1;
        }
        else
        {
            monthNumber = Calendar1.VisibleDate.Month + 1;
            yearNumber = Calendar1.VisibleDate.Year;
        }
        DateTime lastDate = new DateTime(yearNumber, monthNumber, 1);
        return lastDate;
    }

    protected DataSet GetCurrentMonthData(DateTime firstDate, DateTime lastDate)
    {
        DataSet dsMonth = new DataSet();

        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=EMP;Integrated Security=true;");
        String query = "SELECT Task_Date FROM EMP WHERE Task_Date >= @firstDate AND Task_Date < @lastDate";
        SqlCommand cmd = new SqlCommand(query, con);
        cmd.Parameters.Add(new SqlParameter("@firstDate", firstDate));
        cmd.Parameters.Add(new SqlParameter("@lastDate", lastDate));

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        try
        {
            da.Fill(dsMonth);
        }
        catch (Exception ex) { lblerrmsg.Text = ex.Message; }
        return dsMonth;
    }
    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        DateTime nextDate;
        if (dsHolidays != null)
        {
            foreach (DataRow dr in dsHolidays.Tables[0].Rows)
            {
                nextDate = (DateTime)dr["Task_Date"];
                if (nextDate == e.Day.Date)
                {
                    e.Cell.BackColor = System.Drawing.Color.Pink;
                }
            }
        }
    }
    protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
    {
        FillHolidayDataset();
    }

}



 OUTPUT:




Receive All Free Updates Via Facebook.