Thursday, June 27, 2013

Theme In C# asp.net


  1. Open Visual Studio File -->New--> Website (Create Website)
  2. Add App_Code Folder
  3. Inside App_Code Folder Add Class (ThemeClass.cs) 
  4. Add master Page
  5. Then Add Default .aspx Use Master Page
ThemeClass.Cs

public class themeclass : System.Web.UI.Page
{
   
    protected void Page_PreInit(object sender, EventArgs e)
    {
        if (Session["Theme"] != null)
        {
            Page.Theme = Session["Theme"].ToString();
        }
    }
   
}

I have Used inherited Class By Page



Default.aspx

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

 <div>
        <table class="tablecss" align="center" width="30%">
            <tr>
                <td colspan="2" align="center">
                    <h3>
                        Registration Form</h3>
                </td>
            </tr>
            <tr>
                <td align="right">
                    UserName:
                </td>
                <td>
                    <asp:TextBox ID="txtUser" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td align="right">
                    Email:
                </td>
                <td>
                    <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td align="right">
                    Date of Birth:
                </td>
                <td>
                    <asp:TextBox ID="txtDOB" runat="server"></asp:TextBox>
                </td>
            </tr>           
            <tr>
                <td align="right">
                    Address:
                </td>
                <td>
                    <asp:TextBox ID="txtAddress" runat="server" Columns="17" Rows="8" TextMode="MultiLine"></asp:TextBox>
                </td>
            </tr>
        </table>
    </div>
</asp:Content>



Default.CS

public partial class _Default : themeclass
{
    protected void Page_Load(object sender, EventArgs e)
    {
       

    }
}

System.Web.UI.Page is replaced by Class Name

MasterPage

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div id="header" runat="server" style="align: center">
            <asp:DropDownList ID="ddlchangecolor"  runat="server" OnSelectedIndexChanged="ddlchangecolor_SelectedIndexChanged"
                OnTextChanged="ddlchangecolor_TextChanged">
                <asp:ListItem Text="White" Value="White"></asp:ListItem>
                <asp:ListItem Text="Blue" Value="Blue"></asp:ListItem>
                <asp:ListItem Text="Red" Value="Red"></asp:ListItem>
                <asp:ListItem Text="Purple" Value="Purple"></asp:ListItem>
            </asp:DropDownList>
            <asp:Button ID="btnBlue" runat="server" Text="Change Theme" BackColor="Blue" Font-Bold="true"
                OnClick="btnBlue_Click" />
        </div>
        <%--   <asp:ScriptManager ID="ScriptManager1" runat="server" />--%>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
        <div id="footer" runat="server">
        </div>
    </div>
    </form>
</body>
</html>


MasterPage.CS
using System;

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

    }
    protected void ddlchangecolor_SelectedIndexChanged(object sender, EventArgs e)
    {      
    }
    protected void btnBlue_Click(object sender, EventArgs e)
    {
        Session["Theme"] = ddlchangecolor.SelectedItem.Value.ToString().Trim();
     
        Server.Transfer(Request.FilePath);
    }
    protected void ddlchangecolor_TextChanged(object sender, EventArgs e)
    {
      
    }
}

 In The App_Theme Folder 
Add Skin With Blue, Purple, Red , White
Add CSS With Every Skin
Blue.CSS Under Blue Folder
body
{
    background-color:Blue;
}


Purple.CSS Under PurpleFolder
body
{
    background-color:
Purple;
}




Red.CSS Under Red Folder
body
{
    background-color:
Red;
}


White.CSS Under White Folder
body
{
    background-color:
White;
}


Happy Coding



Wednesday, June 26, 2013

Try Catch in sql server Stored Procedure

As we all use try catch for exception handling in code  similarly we apply in stored procedure

Syntax:
BEGIN TRY
{ sql_statement
|
statement_block }
END TRY
BEGIN CATCH
{ sql_statement
|
statement_block }
END CATCH




Functions to be used in CATCH block are :
  • ERROR_NUMBER: returns the error number, and is the same value of @@ERROR.
  • ERROR_SEVERITY: returns the severity level of the error that invoked the CATCH block.
  • ERROR_STATE: returns the state number of the error.
  • ERROR_LINE: returns the line number where the error occurred.
  • ERROR_PROCEDURE: returns the name of the stored procedure or trigger for which the error occurred.
  • ERROR_MESSAGE: returns the full message text of the error. The text includes the values supplied for any substitutable parameters, such as lengths, object names, or times.

USE AdventureWorks;
GO
BEGIN TRY
-- Generate a divide-by-zero error.
SELECT 1/0;
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_STATE() AS ErrorState,
ERROR_PROCEDURE() AS ErrorProcedure,
ERROR_LINE() AS ErrorLine,
ERROR_MESSAGE() AS ErrorMessage;
END CATCH;
GO

Wednesday, June 19, 2013

Table script With data- Database publishing Wizard

Table script With data- Database publishing Wizard
All you have to download the Database Publishing Wizard

  1. Install it

Server.mappath in C#

Server.MapPath specifies the relative or virtual path to map to a physical directory.
  • Server.MapPath(".") returns the current physical directory of the file (e.g. aspx) being executed
  • Server.MapPath("..") returns the parent directory
  • Server.MapPath("~") returns the physical path to the root of the application
  • Server.MapPath("/") returns the physical path to the root of the domain name (is not necessarily the same as the root of the application)
An example:
Let's say you pointed a web site application (http://www.example.com/) to
C:\Inetpub\wwwroot
and installed your shop application (sub web as virtual directory in IIS, marked as application) in
D:\WebApps\shop
For example, if you call Server.MapPath in following request:
http://www.example.com/shop/products/GetProduct.aspx?id=2342
then:
  • Server.MapPath(".") returns D:\WebApps\shop\products
  • Server.MapPath("..") returns D:\WebApps\shop
  • Server.MapPath("~") returns D:\WebApps\shop
  • Server.MapPath("/") returns C:\Inetpub\wwwroot
  • Server.MapPath("/shop") returns D:\WebApps\shop
If Path starts with either a forward (/) or backward slash (\), the MapPath method returns a path as if Path were a full, virtual path.
If Path doesn't start with a slash, the MapPath method returns a path relative to the directory of the request being processed.
Note: in C#, @ is the verbatim literal string operator meaning that the string should be used "as is" and not be processed for escape sequences.

Delegates in C#

Delegates are pointer toward a function or method.It solved the decoupling problem.

For exampling : when you go to marriage the representative welcomes you and tells someone (References) to look after you. Due to this he easily manage all the guest.

Wouldn’t it be nice to have a delegate that could refer to more than one function at once and invoke them
simultaneously? This would allow the client application to have multiple “listeners” and notify the listeners all
at once when something happens.
In fact, delegates do have this functionality, but you’re more likely to see it in use with .NET events.
Events, which are described in the next chapter, are based on delegates but work at a slightly higher level.
In a typical ASP.NET program, you’ll use events extensively, but you’ll probably never work directly with
delegates.


There are four step to create delegates :-
  1. Declare
  2.  Create
  3. Pointer
  4. Invoke
   public delegate int Pointertoaddfun(int i, int y);
    private int add(int x,int y)
    {
        return x + y;
    }
    private int sub(int x, int y)
    {
        return x + y;
    }
 
    public Pointertoaddfun getpointer(int dir)
    {
        Pointertoaddfun objpoiter=null;
        if (dir == 1)
        { objpoiter = add; }
        if (dir == 2)
        { objpoiter = sub; }
    }

    To watch video : -http://www.youtube.com/watch?v=3ckXHImXMiU