WHAT'S NEW?
Loading...

Login system in asp.net with C Sharp


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.

The line of programmer in programming


Having fun writing codes do not mean you are knowledgeable or an expert to create a certain program in all aspects of programming language. Somebody says, “It is a fashion that you’re enjoying what you’re doing” to tell computer what we want them to do based on the instructions you’ve written. Programming is an interesting routine style if you give an instance of humor; because you’re allowing instructing computer what we wish for, like a child teaching them a series of command what we are doing. However, computer is not a child; it is a machine that follows a series of instruction. In order having fun in programming, foremost follow the rules and syntax of every programming language to come up with the output. If you know one language, then you’ll know all about programming languages aspects. Actually, the only difference in every language is their syntax, example the use of data type and the way of declaring a variable, etc. but the routine are similar, it depends how the programmer can manipulates command. The important part of programmer is knew what you are trying and the process where to go through. 

The task of programmer is not merely easy, every tasks has a capability to handles processes and the logic how they are going to manage the daily routine of a piece of code. It includes problem solving and analyzing a certain problem and does not simply simple as part of every programmer that instruct to the computer, however to make it easier for instance, the programming task can be made much more manageable by systematically breaking it up into a number of less complex sub-tasks in order the routine more convenient and the code well apparent to understand. A code symbolized the instruction to tell computer, a simple line of code computer know how to interact what you are trying to, though it has a limitation. A programmer can do extra ordinary routine interacting to the computer machine with the help of programming skills and programming application such Perl, Java, VB, C, and other languages. 
Seeking to the programming for the first time

Somebody’s get mad in programming for the first time because they don’t know how to go through in every command and most probably were getting an error while trying to run a program. However, getting an error does not mean you quite for the reason, but it is a challenge over and over again how to fix those errors. With that, the facts reaching an error is the best experience for the future success because the more getting an error the more confident what you are doing.

Unfortunately, programming is not a very difficult task if you would like to become a good programmer. It is an instance of determination and perseverance to accomplish a definite ambition.

Do and while loop in java


The logical expression is called a loop condition or simply a condition; it is a statement can be either a simple or compound statement in a body of the loop. To avoid the endless infinite loop where the loop executes continuously without ending the process, make sure that the loop’s body contains one or more statement that ensure that the loop condition eventually returns to be false until such time the condition meets the actual output.
In this sample program determine the difference between do and while loop in java given the following example code.
Do Loop
import java.util.*;
public class doloop
{
   static Scanner console = new Scanner(System.in);
  
   public static void main(String[]args)
   {
       int index=1;
       int num;
       int num1;
       int num2;
       int ans;
     
       System.out.println("Enter how many number do you want to process: ");
       num = console.nextInt();
       do
       {
        System.out.println("Enter the 1st number : ");
        num1 = console.nextInt();
        System.out.println("Enter the 2nd  number: ");
        num2 = console.nextInt();
       
        ans = num1 + num2;
        System.out.println("The sum of two numbers is: " + ans);
        index ++;  //increment by 1 until the number meets the number entered
             
        }while(index<=num);
         System.out.println("You've already done " + num + " steps");
         System.exit(0);      
    }

 }

While Loop
import java.util.*;
public class whileloop
{
   static Scanner console = new Scanner(System.in);
  
   public static void main(String[]args)
   {
       int index=1;
       int num;
       int num1;
       int num2;
       int ans;
     
       System.out.println("Enter how many number do you want to process: ");
       num = console.nextInt();
       while (index<=num)
       {
        System.out.println("Enter the 1st  number: ");
        num1 = console.nextInt();
        System.out.println("Enter the 2nd  number: ");
        num2 = console.nextInt();
       
        ans = num1 + num2;
        System.out.println("The sum of two numbers is: " + ans);
        index ++;  //increment by 1 until the number meets the number entered            
        }
         System.out.println("You've already done " + num + " steps");
         System.exit(0);
   }
  
}


The above code determines the number entered by the user that we are going to process. If the number meets the input of a user, the programs automatically stop.

Note: The final output still the same.


Align label and textbox in ASP.NET


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