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.