弹出登录asp.net webform项目与数据库代码



我试图在asp.net web-form项目中建立一个弹出式登录表单。我将页面命名为popuploin .aspx。我在这个链接中找到了一个好方法"http://jqueryasp.net/popup-login-formpage-using-jquery-asp-net/"在这个链接中,你可以看到他们只是写下用户名="ajay"和密码="admin"。但我想从数据库中检索数据。我使用sqlserver 2008。我写ado.net代码在我的代码后面的PopupLogin.aspx.cs。但是我不能成功登录。显示错误警报。我把我的代码给你。

请给我正确的ado.net代码对于这种情况,或找出我的错误。谢谢你!

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;
            }
        }
    }
}

您应该检查一次用户和密码对。

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.
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新