Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Friday, 3 January 2014

C# Using Enum: GTK style

using System;
using System.Collections;
using Gtk;

namespace EmpIEnum
{
class MainClass
{
public static void Main (string[] args)
{
EmployeeCollection E=new EmployeeCollection();
foreach(Employee e in E)
Console.WriteLine(e.Name+"\t\t"+e.Id+"\t\t"+e.Salary);
Console.WriteLine();
}
}
}



using System;
using System.Collections;

namespace EmpIEnum
{
public class Employee
{
public string name;
public int id;
public long salary;

public Employee (string Name,int Id,long salary)
{
name=Name;
id=Id;
salary=Salary;
}
public string Name{
get {return name;}
set {name=value;}
}

public int Id{
get{return id;}
set {id=value;}
}
public long Salary{
get{return salary;}
set {salary=value;}
}

}

}


using System;
using System.Collections;
namespace EmpIEnum
{
public class EmployeeCollection:IEnumerable,IEnumerator
{
Employee[] empcollection;
int pos=-1;

public EmployeeCollection ()
{
empcollection=new Employee[5]
{
new Employee("Ravi",123,2000),
new Employee("Kiran",234,3000),
new Employee("Ranju",345,4000),
new Employee("Ram",456,5000),
new Employee("Maansi",567,6000),
};
}
public IEnumerator GetEnumerator()
{
return(IEnumerator)this;
}
public bool MoveNext(){
pos++;
return(pos<empcollection.Length);
}
public void Reset(){
pos=0;
}
public object Current{
get{return empcollection[pos];}
}

}
}



C# Using indexers : GTK style

using System;
using Gtk;

namespace Lab2
{
class MainClass
{
public static void Main (string[] args)
{
int size=10;
HDFC emp=new HDFC(size);

emp[4]="Manish";
emp[7]="Divya";
emp[9]="Aarushi";

Console.WriteLine("HDFC Emplooyee details");
for(int i=0;i<size;i++)
{
Console.WriteLine("emp[{0}]: {1}", i, emp[i]);
}

Console.ReadKey();
}
}
}


using System;
namespace Lab2
{
public class HDFC
{
private string[] Name;


public HDFC(int n){
Name=new string[n];
for(int i=0;i<n;i++)
{
Name[i]="null";
}
}

public string this [long index]
{
get{
return Name[index];
}
set{
Name[index]=value;
}

}
}
}


Monday, 23 December 2013

C# Gettter/Setter : GTK style

C# properties: Properties in C# are used to provide protection to the attributes of a class against direct reading or writing to it . By using properties we fix the rule that a particular attribute of the class can be read or written by means of property associated to that attribute.
These can be classified into 3 types:
Read only properties : We implement only the getter in the property implementation.
Syntax:
public String Name
{
get
{
return name;
}
}
Write only properties :We implement only the Setter in the property implementation.
Syntax:
public String Name
{
set{name=value;}
}
Auto implemented properties: This is the most commonly used type. we define the both getter and setter without any actual implementation.
Syntax:
public String Name{get; set;}
Example: Accessing the Employee class with properties
Employee.cs
using System;
namespace lab1
{
public class Employee
{
private string name;
private long empid;
public Employee (String Name,long id){ name=Name;empid=id;}
public String Name
{
get
{
return name;
}
set{name=value;}
}
public long Empid
{
get
{
return empid;
}
set{empid=value;}
}
public override string ToString ()
{
return (“Name:”+name+”\nEmployee ID:”+empid);
}
}
}
Main.cs
using System;
using Gtk;
namespace lab1
{
class MainClass
{
public static void Main (string[] args)
{
Employee E1=new Employee();
E1.Name=”kiran”;
E1.Empid=1234;
Console.WriteLine(“Employee details\n {0}”, E1);
Console.WriteLine(“End of Employee details”,E1);
Console.ReadKey();
}
}
}

Sunday, 24 June 2012

C# Properties : using the getter/setter


C# properties: Properties in C# are used to provide protection to the attributes of a class against direct reading or writing to it . By using properties we fix the rule that a particular attribute of the class can be read or written by means of property associated to that attribute.


These can be classified into 3 types:

  • Read only properties : We implement only the getter in the property implementation.
Syntax:
public String Name
{
get
{
return name;
}
                }
  • Write only properties :We implement only the Setter in the property implementation.
Syntax:
public String Name
{
set{name=value;}
}
  • Auto implemented properties: This is the most commonly used type. we define the both getter and setter without any actual implementation.
Syntax:

public String Name{get; set;}

Example: Accessing the Employee class with properties


Employee.cs
using System;
namespace lab1
{
public class Employee
{
private string name;
private long empid;

public Employee (String Name,long id){ name=Name;empid=id;}
public String Name
{
get
{
return name;
}
set{name=value;}

}
public long Empid
{
get

return empid;
}
set{empid=value;}

}
public override string ToString ()
{
return ("Name:"+name+"\nEmployee ID:"+empid);
}
}
}


Main.cs



using System;
using Gtk;


namespace lab1
{
class MainClass
{
public static void Main (string[] args)
{
Employee E1=new Employee();

E1.Name="kiran";
E1.Empid=1234;

Console.WriteLine("Employee details\n {0}", E1);
Console.WriteLine("End of Employee details",E1);

Console.ReadKey();
}
}
}




Wednesday, 13 June 2012

Cross-page post ASP.NET & C#



ASP.Net 1.1 provided the feature of web forms to post back the form content only to themselves. But in practice in many situations, the solution requires posting to a different web page. 
The traditional workaround alternatives were to use :
  • Response.Redirect and/or
  • Server.Transfer 

to move to a different page and simulate cross page post-back behavior.

ASP.Net version 2.0 and further versions provided a feature known as Cross Page PostBack for a web form to post-back to a different web form (other than itself)

Post to a different page:

To set a web form to post back to a different web form, in the source web form, set the PostBackURL property of a control that implements IButtonControl (eg. Button, ImageButton, LinkButton) to the target web form. When the user clicks on this button control, the web form is cross-posted to the target web form. No other settings or code is required in the source web form.

Access source page info within the posted page: FindControl Method

The target web form resulting from the cross-page postback provides a non-null PreviousPage property. This property represents the source page and provides reference to the source web form and its controls.

The controls on the source page can be accessed via the FindControl method on the object returned by the PreviousPage property of the target page.

protected void Page_Load(object sender, EventArgs e)
    {
        if (PreviousPage != null)
        {
            TextBox TextBox1 = (TextBox)PreviousPage.FindControl("TextBox1");
            label1.Text = String.Format("Text was:{0}", TextBox1.Text);
        }
    }




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

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        Enter Some value:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /><br />
        <asp:Button ID="Button1" Text="Send my text" PostBackUrl="~/Page2.aspx" runat="server"/>
   
    </div>
    </form>
</body>
</html>

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

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label id="label1" runat="server"></asp:Label>
   
    </div>
    </form>
</body>
</html>


Page2.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 Page2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (PreviousPage != null)
        {
            TextBox TextBox1 = (TextBox)PreviousPage.FindControl("TextBox1");
            label1.Text = String.Format("Text was:{0}", TextBox1.Text);
        }
    }
  
}



I hope this will help :)

Securing a web page from unauthorized access ASP.NET & C#

ASP.NET provides with the  feature of Login Control which helps you to easily build a user registration system for your website. This feature , Login control is used to display user registration forms, login forms, change password forms, and password reminder forms.
The Login controls use ASP.NET Membership(ASP.NET version 2.0 introduces a membership feature, which provides a consistent API for user credential storage and management) to authenticate users, create new users, and change user properties. When you use the Login controls, you are not required to write any code when performing these tasks.
We shall now  learn how to password-protect a section of our website and enable users to register and log in to our website.
The Login control supports the following properties:
  • Login Enables you to display a user login form.
  • CreateUserWizard Enables you to display a user registration form.
  • LoginStatus Enables you to display either a log in or log out link, depending on a user’s authentication status.
  • LoginName Enables you to display the current user’s registered username.
  • ChangePassword Enables you to display a form that allows users to change their passwords.
  • PasswordRecovery Enables you to display a form that allows users to receive an email containing their password.
  • LoginView Enables you to display different content to different users depending on the their authentication status or role.

    Steps:

1.To password-protect a page which is stored inside our secured folder , we need to make two configuration settings to our application and is to configure both authentication and authorization for the page's access.

2. Authentication:

This is the step 1 of  mission :)
By default, Windows authentication is enabled. To use the Login controls, you need to enable Forms authentication by adding the web configuration file of the root folder of our application.

3.Authorization:

By default, all users have access to all pages in an application. If you want to restrict access to the pages in a folder, you need to configure authorization for the folder.
we use two symbols to set the restrict the access to our secured page in the folder . they are as follows:
  1. <deny users="?"/>
  2. <deny users="*"/>
?-anonymous users are prevented from accessing any pages in the folder.Now if anyone attempts to request the secured page after adding the web configuration file (for authorization of the user, located inside the folder in which the secured page is stored), he will be redirected to a page for Login automatically. Therefore, the next page that we need to create is the page containing the login control we call it Login.aspx  page. (By default, this page must be located in the root of your application.)

4.Creating Login.aspx


<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Login</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <asp:Login
        id="Login1"
        CreateUserText="Register"
        CreateUserUrl="~/Register.aspx"
        Runat="server" />

    </div>
    </form>
</body>
</html>

5.Creating Register.aspx

The Login control includes a CreateUserText and CreateUserUrl property. Adding these properties to the Logincontrol causes the control to display a link to a page that enables a new user to register for your application. TheLogin control in links to a page named Register.aspx.

The Register.aspx page contains a CreateUserWizard control. This control automatically generates a user registration form. After you submit the form, a new user is created, and you are redirected back to the secured page.

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Register</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <asp:CreateUserWizard
        id="CreateUserWizard1"
        ContinueDestinationPageUrl="~/SecretFiles/Secret.aspx"
        Runat="server" />

    </div>
    </form>
</body>
</html>

Now lets do an Exercise:


Create a page with some content .secure this page so that authorized user is able to access your page to check the authorization of the user. Use login control for unregistered user to create a registration form using registration control and send confirmation mail to the user


The structure of  your project should look somewhat similar to image given below:

Note: I named my project EmailConfirmation(to avoid confusion) you can name it some thing else .


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

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>shhhhhh ! i am a secured form</h1>
    </div>
    </form>
</body>
</html>

//This web.config file come under your projects root folder.
Web.config
<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>

<system.net>
    <mailSettings>
      <smtp deliveryMethod="PickupDirectoryFromIis"/>
      </mailSettings>
  </system.net>

       <system.web>
              <compilation debug="true" targetFramework="4.0"/>
              <authentication mode="Forms">
                     <forms>
                           <credentials passwordFormat="Clear">
                                  <user name="ravi" password="kiran"/>
                           </credentials>
                     </forms>
              </authentication>
       </system.web>
</configuration>


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

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:CreateUserWizard ID="createRegistration" ContinueDestinationPageUrl="~/secured/secured.aspx" runat="server">
    <MailDefinition BodyFileName="Register.txt" Subject="Registration Confirmation" From="Admin@YourSite.com"></MailDefinition>
    </asp:CreateUserWizard>
    </div>
    </form>
</body>
</html>

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

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Login ID="login1" CreateUserText="Register Me" CreateUserUrl="~/Register.aspx" onAuthenticate="login_authenticate" runat="server"></asp:Login>
    </div>
    </form>
</body>
</html>

Login.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.Web.Security;

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

    }
    protected void login_authenticate(object sender, AuthenticateEventArgs e)
    {
        string username = login1.UserName;
        string password = login1.Password;
        e.Authenticated = FormsAuthentication.Authenticate(username, password);
    }
}



//This web.config file is a part of the Secured folder.
Web.config

<?xml version="1.0"?>
<configuration>
    <system.web>
      <authorization>
        <deny users="?"/>
      </authorization>
    </system.web>
</configuration>



Register.txt
Thank you for registering !

Hope you have good experience with us !





I hope you find it usefull.:)