User Login Problem in Asp.net with Sql

Posted by Nuwan under ASP.NET on 11/6/2011 | Points: 10 | Views : 1356 | Status : [Member] | Replies : 3
I created C#.net Windows Application for Control User Login, But I cant Implement that Asp.net Application. This is my code.

//In Form 1........

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace UserPermission
{
public partial class Form1 : Form
{
private readonly string conn_str = "Data Source=nuwan-pc;Initial Catalog=UserPermission;Integrated Security=True";

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void btnLogin_Click(object sender, EventArgs e)
{



string query = "SELECT Id, Username, Password, PriviledgeLvl FROM Access WHERE Username = @Username";

using (SqlConnection conn = new SqlConnection(conn_str))
{
using (SqlCommand cmd = new SqlCommand(query, conn))
{
//add a parameter to sql query
cmd.Parameters.AddWithValue("Username", txtUsername.Text.Trim());

conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{

reader.Read();
string password = reader["Password"].ToString();
string priviledgeLevel = reader["PriviledgeLvl"].ToString();
string id = reader["Id"].ToString();

if (reader.Read())
{
return;
}
else
{
if (txtPassword.Text.Trim() == password)
{


//Create new form2 and pass it the 'priviledgeLevel' parameter
Form2 frm2 = new Form2(priviledgeLevel,id,m);

frm2.ShowDialog();
}
else
{
MessageBox.Show("Password or Username Not Valid"); //Wrong password!
}
}
}
}
}
}
}
}
}


//In Form 2.........

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace UserPermission
{
public partial class Form2 : Form
{
public Form2(string priviledgeLevel)
{
InitializeComponent();
InitControls(priviledgeLevel);

}



private void InitControls(string priviledgeLevel)
{
if (priviledgeLevel == "ADMIN")
{
//buttons visible to admin only
btnFirst.Visible = true;
btnSecond.Visible = true;
btnThird.Visible = true;

}
else
{
//buttons not visible to common user
btnFirst.Visible = false;
btnSecond.Visible = false;
btnThird.Visible = false;


}

//These two buttons are visible to both admin and common user
btnFourth.Visible = true;
btnFifth.Visible = true;
}
}
}



Please Help me that matter.........

Rathnayake


Responses

Posted by: SheoNarayan on: 11/6/2011 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Hi Nuwan,

Have a look at this article http://www.dotnetfunda.com/articles/article114.aspx that should help you know how to implement Login mechanism in ASP.NET application.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Nuwan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nuwan on: 11/7/2011 [Member] Starter | Points: 25

Up
0
Down
Hi SheoNarayan,
But i want to control it user & Administrator Permission.



Rathnayake

Nuwan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vinay13mar on: 11/14/2012 [Member] Starter | Points: 25

Up
0
Down
Hi this is really nice article. please check the below link also.


http://www.dotnetpools.com/Article/ArticleDetiail/?articleId=79&title=How To Write a Simple Login Page In Asp.net



Nuwan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response