WHAT'S NEW?
Loading...
Showing posts with label C sharp. Show all posts
Showing posts with label C sharp. Show all posts

Login System is one of the thrilling securities that the users prompt to input their username and password or even has a user level control to enable accessing their transaction locally or through online application. Most probably every account has their own access level, for instance to avoid accessing unnecessary operation, for example the admin user has an authority to access all transaction. There are different ways of coding style in logging the system, also the uses of database such as MS SQL Server, MySQL, Oracle, ODBC, or MS Access. Unfortunately, for the beginner’s we can use MS Access to login the system. However, you may also use all databases. 

Migrating Visual Basic to C Sharp (C#) unfortunately is not really hard because almost both of them has a similarities except for the advantages and disadvantages of them. If you had already a background in C, C++, or even in java is advantage. Actually, I didn’t expect knowing this language before but when I encountered it seems looks like the same in java, so I tried and I made it accidentally. Unintentionally, I made some fun making webpage and published it to my own site because I enjoy what I’m doing. So, every time I’m done making unintentional project and I think it is important, I write it for an article and post to my own site.

Making a login system is very important in small or a large system to avoid access unnecessarily transaction. This example shows a simple but it is very helpful for the beginner’s how to go through with the login system. Actually, this code written before in Visual Basic and it looks like almost same, I made some understanding how the proper syntax is being made.

This complete code written in C sharp and applied it in ASP.NET.
protected void Btnlogin_Click(object sender, EventArgs e)
    {
        //when you noticed..I split dbprovider and dbsource,
        //actually you can combined them without having
        //concatenation.
        string dbProvider = "PROVIDER = Microsoft.Jet.OLEDB.4.0;";
        string dbSource = "Data Source = C:\\account.mdb";


        OleDbConnection con = new OleDbConnection(dbProvider + dbSource);

        string sql = "SELECT * FROM tblaccount WHERE Username = '" + txtusername.Text +
            "' AND Password = '" + txtpassword.Text +
            "' AND UserLevel = '" + DropDownList1.Text + "'";

        OleDbCommand cmd = new OleDbCommand(sql, con);
        con.Open();
   
        OleDbDataReader sdr = cmd.ExecuteReader();

        string userlevel; //variable declaration for the user

        userlevel = DropDownList1.SelectedItem.Text.ToString();

      
        if (sdr.HasRows == true) //determine if the data has rows
        {
            switch (userlevel)//reads the username if it is equal to the given username
            {
                case "User"://if sprint username was input, Loginforsprint.asp web form
                    Response.Redirect("~/Loginforuser.aspx");
                    break;
                case "Admin":
                    Response.Redirect("~/Loginforadmin.aspx");
                    break;
            }
         
        }
        else
        {
                lblerror.Text = "Invalid data!!";
           
        }
     
      
    }

Hopefully this is very helpful. In fact, I’m not totally good but the above code is totally running!
I’ll show you the screenshots:
 
In order to come up with the output, you should provide 3 Web Forms for user login, login for user, and login for admin, 3 Labels for username with (Textbox), password with (Textbox), user level with (DropDownList), and 1 Button.

ASP.Net build in codes such as drag and drop from the toolbox to web form or even from the opening is complicated for the first time user. However, as we go along and focus what’s behind the code in every line it makes you think how you are going to align every label, text box, button, etc. that comes from the toolbox. There are different ways how you are going to work with, such as table. However, Cascading Style Sheet (CSS) is most interesting way to align, emphasize your text, label, background, and a lot more. Visit w3shcools.com for more information about CSS.
Now, let’s look how to align label and text box in ASP.Net using C Sharp (C#) 2010.

Open your Visual Studio 2010 – New Project >select Visual C# language > Web and choose ASP.NET Empty Web Application and name it My First Web Site.
 
Next, in your solution explore right click My First Web Site which is the name of your website and click Add new item, Select Web Form and name it Home.aspx.

Here is the code generated of your Home.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="My_First_Web_Site.Home" %>

<!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>
   
    </div>
    </form>
</body>
</html>
 
We are going to split. Your form looks like this.
Add the following CSS code after the </title> closing tag until closing tag </head> 

    <style type="text/css">
        .Main
        {
            background-color: white;
            margin-left: auto; /* Center mainTable for >= IE6  */
            margin-right: auto; /* Center mainTable for >= IE6  */
        }
        .LeftLabelColumn
        {
            display: block;
            margin-left: 10px;
            margin-right: 10px;
            height: 22px;
            padding-top: 5px;
        }
        .LeftValueColumn
        {
            display: block;
            margin-left: 10px;
            margin-right: 10px;
        }
    </style>

Your output looks like the following screenshots:

 
Next, we are going to delete open <div> and close </div>
 
 Delete the highlighted <div> </div> and paste this code.

<div class="Main">
    <!-- This label where my output firstname, lastname, middlename, and address are being displayed -->
        <asp:Label ID="lblshowmyname" runat="server" CssClass="LeftLabelColumn"
            Font-Size="Larger"></asp:Label>
        <div style="float: left; border-top: solid 10px white;">

            <asp:Label ID="Label1" CssClass="LeftLabelColumn" runat="server"
                Text="Firstname"></asp:Label>
            <asp:Label ID="Label2" CssClass="LeftLabelColumn" runat="server"
                Text="Lastname"></asp:Label>
            <asp:Label ID="Label3" CssClass="LeftLabelColumn" runat="server"
                Text="Middlename"></asp:Label>
            <asp:Label ID="Label4" CssClass="LeftLabelColumn" runat="server" Text="Address"></asp:Label>
        </div>
        <!-- Because we can't use border-top, margin-top or padding-top on a textbox, we surround the textboxes with div tags -->
        <div class ="LeftLabelColumn">
            <div style="padding-top: 5px;"><asp:TextBox ID="TextBox1"
                    CssClass="LeftValueColumn" runat="server"></asp:TextBox></div>
            <div style="padding-top: 5px;"><asp:TextBox ID="TextBox2"
                    CssClass="LeftValueColumn" runat="server"></asp:TextBox></div>
            <div style="padding-top: 5px;"><asp:TextBox ID="TextBox3"
                    CssClass="LeftValueColumn" runat="server"></asp:TextBox></div>
            <div style="padding-top: 5px;"><asp:TextBox ID="TextBox4"
                    CssClass="LeftValueColumn" runat="server" Width="459px"></asp:TextBox>
                <asp:Button ID="Button1" runat="server" Text="Show me"
                    onclick="Button1_Click" />
            </div>
          
        </div>
    </div>

Note: make sure paste the code above after this code <form id="form1" runat="server"> and closing </form>

After you paste the code, the final output looks like the following.
The label above displays your firstname, lastname, middlename, and address when your click Show me

Now, we are going to put code in order to display the result in the label. Double click the show me button. The code should looks like the following.
 
Copy this code:
 string fname;
        string lname;
        string mname;
        string address;

        fname = TextBox1.Text;
        lname = TextBox2.Text;
        mname = TextBox3.Text;
        address = TextBox4.Text;

        lblshowmyname.Text = "Name: " + fname + " " + mname +
            ". " + lname +  " From: " + address;

Finally, your final code looks like:
 So, now we are done! Press F5 to run your program. The output runs your browser.


That’s the final output!
Next: Creating Master Page in ASP.NET 2010