无法使用数据库中的现有用户登录! - ASP.NET

时间:2015-08-06 20:07:00

标签: c# html asp.net login

我正在使用ASP.NET Web应用程序并且我已经完成了Login.aspx页面,但每当我尝试登录时都没有发生任何事情!点击登录按钮后,没有任何错误,但没有任何好处!请帮忙吗?

Login.asp.cs代码:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyWebsite
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ButtonS_Click(object sender, EventArgs e)
        {

                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString);
                conn.Open(); 
                string checkuser = "Select count(*) from Sellers where Username='" + TextBoxSUsername.Text + "'";
                SqlCommand com = new SqlCommand(checkuser, conn);
                int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
                conn.Close();
            if (temp == 1)
            {
                conn.Open();
                string checkPasswordQuery= "Select Password from Sellers where Username='" + TextBoxSUsername.Text + "'";
                SqlCommand PassCon = new SqlCommand(checkPasswordQuery, conn);
                string password = PassCon.ExecuteScalar().ToString().Replace(" ","");
                if (password==TextBoxSPassword.Text)
                {
                    Session["New"] = TextBoxSUsername.Text;
                    Response.Write("DONE");

                }
                else { Response.Write("Wrong password "); }
            }
            conn.Close();

        }

Login.aspx代码:

<table>
    <tr>
        <td> Username</td>
        <td><asp:TextBox ID="TextBoxSUsername" runat="server" Height="25px" Width="157px"></asp:TextBox></td>
        <td><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBoxSUsername" ErrorMessage="must enter username  "></asp:RequiredFieldValidator></td>
    </tr>
    <tr>
        <td>Password</td>
        <td><asp:TextBox ID="TextBoxSPassword" runat="server" Height="25px" Width="157px"></asp:TextBox></td>
        <td><asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBoxSPassword" ErrorMessage="must enter password"></asp:RequiredFieldValidator></td>
    </tr>
        <tr><td><asp:Button ID="ButtonS" runat="server" OnClick="ButtonS_Click" Text="Button"/></td></tr>
</table>

1 个答案:

答案 0 :(得分:0)

为什么不调试代码?看起来像temp != 1,代码只是关闭连接。

顺便说一句,该代码存在一些问题。调用两次相同的查询效率非常低。也许您可以获取所有信息(select * from Sellers),然后从内存中的相同数据中询问用户和密码。

检查temp的值并首先尝试trim()用户名和密码。