[英]popup login asp.net webform project with database code
I am trying to build a popup log-in form in asp.net web-form project.我正在尝试在asp.net web 表单项目中构建一个弹出式登录表单。 i named the page PopupLogin.aspx.
我将页面命名为 PopupLogin.aspx。 i found a good way in this link "http://jqueryasp.net/popup-login-formpage-using-jquery-asp-net/ " in this link you can see that they just write down the username = "ajay" and password = "admin".
我在这个链接“http://jqueryasp.net/popup-login-formpage-using-jquery-asp-net/ ”中找到了一个好方法,在这个链接中你可以看到他们只是写下用户名 =“ajay”和密码 =“管理员”。 but i want to retrive the data from the database.
但我想从数据库中检索数据。 i use sqlserver 2008. i write ado.net code in my code behind PopupLogin.aspx.cs.
我使用 sqlserver 2008。我在 PopupLogin.aspx.cs 后面的代码中编写了 ado.net 代码。 but i cant successfully login.
但我无法成功登录。 an error alert shown.
显示错误警报。 i give my code to you.
我给你我的代码。
please give me the right ado.net code for this situation, or find out my mistakes.请为这种情况提供正确的 ado.net 代码,或者找出我的错误。 thank you.
谢谢你。
.cs code. .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.Services;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.Configuration;
using System.Collections;
namespace OnlineDhaka
{
public partial class PopupLogin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public bool CheckUser(string username, string password)
{
int temp =0;
SqlConnection conn = new SqlConnection("Data Source=AKASH-PC\\SQLEXPRESS;Initial Catalog=Registration;Integrated Security=True");
conn.Open();
string checkuser = "select count(*) from RegData where Username='" + username + "'";
SqlCommand com = new SqlCommand(checkuser, conn);
temp = Convert.ToInt32(com.ExecuteScalar().ToString());
conn.Close();
if (temp == 1)
{
conn.Open();
string checkPasswordQuery = "select Password from RegData where Username='" + username + "'";
SqlCommand passcom = new SqlCommand(checkPasswordQuery, conn);
string Password = passcom.ExecuteScalar().ToString().Replace(" ", "");
if (Password == password)
{
Session["New"] = username;
using (SqlCommand cmdid = new SqlCommand("select Id from RegData where Username = '" + username+ "'", conn))
{
int id = (int)cmdid.ExecuteScalar();
Session["ID"] = id;
}
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
}
You should check a user and password pair once.您应该检查一次用户和密码对。
var selectText = "SELECT Id FROM RegData WHERE Username=@UserName AND Password = @Password"
using (var command = connection.CreateCommand())
{
command.CommandText = selectText;
command.Parameters.AddWithValue("@Username", username);
command.Parameters.AddWithValue("@Password", password);
using(var reader = command.ExecuteReader())
{
//If table has row with username and password
if(reader.read())
{
//Username and password is valid
var id = reader["Id"];
//Your logic here
}
else
{
//Username and password is invalid.
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.