Friday, 29 June 2012

Url Routing in Asp.net 3.5


                                            Url Routing in Asp.net 3.5

Note:
means url looks like this if i press pravat sabat then
url would be like this http://localhost:1450/url_rout/User/hhhhh
means not showing pagename.

html code like this:-> take hello.aspx page and RegisterUser.aspx page and do the following code there

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RegisterUser.aspx.cs" Inherits="RegisterUser" %>

<!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>Add New User</title>
    <style type="text/css">
        .style1 {
            width: 80%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <div align="left" class="style1">
            <asp:GridView ID="GridViewUserList" runat="server" AutoGenerateColumns="False"
            DataSourceID="LinqDataSource1" Width="400px">
                <Columns>
                    <asp:TemplateField HeaderText="Registered Users">
                        <ItemTemplate>
                            <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("UniqueName", "~/User/{0}") %>'
                                Text='<%# Eval("Name") %>'></asp:HyperLink>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Unique Name">
                        <ItemTemplate>
                            <asp:Label ID="HyperLink1" runat="server" Text='<%# Eval("UniqueName") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="DataClassesDataContext"
                Select="new (UniqueName, Id, Name)" TableName="Users">
            </asp:LinqDataSource>
        </div>
    </div>
    <div>&nbsp;</div>
    <div align="center">
        <div align="left" style="font-weight:bold;">Add New User</div>
    </div>
    <div align="center">
   
        <table class="style1">
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Full Name</td>
                <td>
                    <asp:TextBox ID="TextBoxFullName" runat="server" Width="200px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Summary</td>
                <td>
                    <asp:TextBox ID="TextBoxSummary" runat="server" Height="50px"
                        TextMode="MultiLine" Width="200px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Unique Name</td>
                <td>
                    <asp:TextBox ID="TextBoxUniqueName" runat="server" Width="200px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:Label ID="LabelMessage" runat="server" Font-Bold="True" Font-Size="Small"
                        ForeColor="Red"></asp:Label>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click1">LinkButton</asp:LinkButton>
                </td>
                <td>
                    <asp:Button ID="ButtonRegister" runat="server" onclick="ButtonRegister_Click"
                        Text="Register" />
                </td>
                <td>
                    &nbsp;</td>s
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>


.cs code-> i am using linq datasource for connectivity



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq;
/// <summary>
/// Implements IDisplay interface which
/// </summary>
public partial class RegisterUser : System.Web.UI.Page, IDisplay
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void ButtonRegister_Click(object sender, EventArgs e)
    {
        string uniqueName = TextBoxUniqueName.Text.Trim();
        DataClassesDataContext dc = new DataClassesDataContext();
        //getting users using unique name given by user.
        var users = from u in dc.Users
                    where u.UniqueName == uniqueName
                    select u;
        if (users.Count<User>() == 0)
        {
            //Registering new user
            User user = new User();
            user.UniqueName = uniqueName;
            user.Name = TextBoxFullName.Text;
            user.Summary = TextBoxSummary.Text;
            dc.Users.InsertOnSubmit(user);
            dc.SubmitChanges();
            LabelMessage.Text = "Congratulations! The Name '" + uniqueName + "' is registered successfully.";
            TextBoxFullName.Text = "";
            TextBoxUniqueName.Text = "";
            TextBoxSummary.Text = "";
            LinqDataSource1.DataBind();
            GridViewUserList.DataBind();
        }
        else
        {
            //unique name not available
            LabelMessage.Text = "Sorry! The Name '" + uniqueName + "' is not available.";
        }
    }
  
    protected void LinkButton1_Click1(object sender, EventArgs e)
    {
        Response.Redirect("~/A");
    }
  
}

now open asax file and do the code like this:

<%@ Application Language="C#" %>

<script runat="server">
    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        RegisterRoutes();
    }
    private static void RegisterRoutes()
    {
        System.Web.Routing.RouteTable.Routes.Add(
                "Add_New_User", new System.Web.Routing.Route("Add_New_User",
                                    new RouteHandler("~/RegisterUser.aspx")));
       
        System.Web.Routing.RouteTable.Routes.Add(
                "user", new System.Web.Routing.Route("User/{name}",
                new UserRouteHandler("~/DisplayUser.aspx")));
       
        System.Web.Routing.RouteTable.Routes.Add(
               "A", new System.Web.Routing.Route("A",
               new RouteHandler("~/hello.aspx")));
    }
    void Application_End(object sender, EventArgs e)
    {
        //  Code that runs on application shutdown

    }
       
    void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e)
    {
        // Code that runs when a session ends.
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer
        // or SQLServer, the event is not raised.

    }
      
</script>

take ,DisplayUser.aspx
and html code like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DisplayUser.aspx.cs" Inherits="DisplayUser" %>

<!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>Display User</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="left">
        <asp:HyperLink ID="HyperLink1" NavigateUrl="~/Add_New_User" runat="server">Back To List</asp:HyperLink>
    </div>
    <div align="center">
        <table style="width:70%;">
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Full Name</td>
                <td>
                    <asp:TextBox ID="TextBoxFullName" runat="server" Width="200px" ReadOnly="true"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Summary</td>
                <td>
                    <asp:TextBox ID="TextBoxSummary" runat="server" Height="50px"
                        TextMode="MultiLine" Width="200px" ReadOnly="true"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Unique Name</td>
                <td>
                    <asp:TextBox ID="TextBoxUniqueName" runat="server" Width="200px" ReadOnly="true"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:Label ID="LabelMessage" runat="server" Font-Bold="True" Font-Size="Small"
                        ForeColor="Red"></asp:Label>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
.cs code

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

public partial class DisplayUser : System.Web.UI.Page, IUserDisplay  
                                                                                         <-// this is important interface to display user
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (UniqueName != null && !UniqueName.Equals(""))
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            var users = from u in dc.Users
                       where u.UniqueName == UniqueName
                       select u;
            if (users.Count<User>() > 0)
            {
                User user = users.First<User>();
                TextBoxFullName.Text = user.Name;
                TextBoxSummary.Text = user.Summary;
                TextBoxUniqueName.Text = user.UniqueName;
            }
        }
    }
    public string UniqueName { get; set; }

}

 if i click link button on below link button then
.cs code of hello pages would be like this


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

public partial class hello : System.Web.UI.Page,IDisplay// this is needed to display routing
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
       
    }
}





RegisterUser.aspx page design ->


Registered UsersUnique Name
Anurag Bajpai anurag
Vijay SP vijay
Pravat Sabat hhhhh
Rajendra NAutiyal Nauti
mukesh mukesh 123
 
Add New User
s
     
Full Name  
Summary  
Unique Name  
   
LinkButton                         

Sunday, 24 June 2012

Use Of Enum In Asp.net Example


                                                Use Of Enum In Asp.net Example



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ClassLibraryEnum;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Status oStatus = Status.Fail;

        oStatus = (Status)Enum.Parse(typeof(Status), "0", true);

        if(oStatus==0)
       {
           response.write("Sucess");
       }
    }
}

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

namespace ClassLibraryEnum
{

    public enum Status
    {
        Sucess,
        Fail,
    }

}

Wednesday, 20 June 2012

sql query Copying table data to another table that should not be repeating terms using Cursor

    sql query Copying table data to another table that should not be repeating terms using Cursor

                 
DECLARE @ACCOUNTID BIGINT
DECLARE @NAME VARCHAR(30)
DECLARE @TEB TABLE(NAME VARCHAR(34),NUM BIGINT)
DECLARE @GETACCOUNTID CURSOR
SET @GETACCOUNTID = CURSOR FOR
SELECT NAME, MBNO
FROM EMP1
OPEN @GETACCOUNTID
FETCH NEXT
FROM @GETACCOUNTID INTO @NAME,@ACCOUNTID
WHILE @@FETCH_STATUS = 0
BEGIN
    IF NOT EXISTS(SELECT 1 FROM @TEB WHERE NAME=@NAME AND NUM=@ACCOUNTID)    
       BEGIN
         DECLARE @P AS BIGINT
         INSERT INTO @TEB VALUES(@NAME,@ACCOUNTID)
       END   


FETCH NEXT
FROM @GETACCOUNTID INTO @NAME,@ACCOUNTID
END
CLOSE @GETACCOUNTID
DEALLOCATE @GETACCOUNTID
SELECT* FROM @TEB

 Use Of Charindex in sql  example

        Declare
        @value  varchar(100)
        ,@firatName  varchar(100)
        ,@lastName  varchar(100)
        ,@age  varchar(100)

                set  @value = 'mukesh trivedi Age 23'
               
                    SELECT @value
                   
                    Set @firatName = SUBSTRING(@value,0,CHARINDEX(' ',@value,0))
                   
                    Set @lastName = SUBSTRING(@value,CHARINDEX(' ',@value,0)+1,LEN(@value)- CHARINDEX(' ',@value,0)+ 1 -CHARINDEX(' ',@value, CHARINDEX(' ',@value,0)) )
                     
                    Set @age = SUBSTRING(@value,CHARINDEX(' ',@value,CHARINDEX(' ',@value,0)+ 1)+ 5,2)
                   
                    SELECT  @firatName ,@lastName,@age v

out put : mukesh trivedi Age 23
             mukesh    trivedi    23

Thursday, 7 June 2012

uploading file in specified folder using web.config file and downloading that file

                  creating simple application for uplaod file in folder download file from that folder
                   taking path from web config.

 for this create html like this:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <table class="style1">
            <tr>
                <td class="style2" colspan="2">
                    &nbsp;
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                </td>
            </tr>
            <tr>
                <td class="style2" colspan="2">
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
                        Text="Upload File in Folder using web config. file" />
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td class="style2">
                    <asp:Button ID="Button2" runat="server" OnClick="Button2_Click"
                        Text="see documents in link on click button and download" />
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
        </table>
    </p>
    <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <a href="NewUplage/85.[dbo].[stp_InsertTest1].sql">File</a>
    </form>
</body>
</html>


code for this on  .cs file like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string filepath = ConfigurationManager.AppSettings["MyFileLocation"].ToString();
            string filename = Path.GetFileName(FileUpload1.FileName);
            string str = filepath + "\\" + filename;
            FileUpload1.SaveAs(str);


        }

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            getfilename();
        }
    }

    private void getfilename()
    {


        LinkButton1.Text = null;
        string menuContent = null;
        FileInfo oFile = null;
        DirectoryInfo oDirectory = null;
        FileSystemInfo[] fileList = null;
        string folderPath = string.Empty;
        string filePath = string.Empty;
        int i = 0;
        string url = string.Empty;
        try
        {
            folderPath = ConfigurationManager.AppSettings["MyFileLocation"].ToString() + "\\";

            filePath = ConfigurationManager.AppSettings["filePath"].ToString();

            oFile = new FileInfo(folderPath);
            oDirectory = oFile.Directory;
            fileList = oDirectory.GetFiles();
            foreach (FileSystemInfo selectedFile in fileList)
            {
                i = i + 1;
                string name = selectedFile.Name;
                url = filePath + name;

                try
                {
                   

                    menuContent = "<li><a href =" + "\"" + ResolveUrl(url) + "\"" + "> " + i + "." + name + "</a></li>";

                    LinkButton1.Text = LinkButton1.Text + i + menuContent;

                   

                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
      
    }
}


and code in web.config also like this:

</configSections>
  <appSettings>
    <add key="MyFileLocation" value="E:\mukesh folder\TestDesignApplication\filedata\NewUplage"/>

    <add key="filePath" value="~\NewUplage\"/>
  </appSettings>
  <connectionStrings/>