简体   繁体   English

我正在验证 C# 中的表格,但我不知道我做错了什么。 请帮我解决这个问题

[英]I'm validating a form in C# but I don't know what I am doing wrong. Please help me solve this

I'm creating a validation for form data coming from database and then comparing it with data entered in textboxes.我正在为来自数据库的表单数据创建验证,然后将其与在文本框中输入的数据进行比较。 It always executes else part whether I enter correct or incorrect data in textboxes, please help with this.无论我在文本框中输入正确还是不正确的数据,它总是执行其他部分,请帮助解决这个问题。

c.Uname = Text1.Value.ToString();
c.Cnic  = Text2.Value.ToString();
c.pass = Text3.Value.ToString();

SqlConnection sqlConn = new SqlConnection(@"Data Source=DESKTOP-Q4AAHCG;Initial Catalog=practise;User ID=;Password=;Trusted_Connection=True");

SqlCommand sqlComm = new SqlCommand("select Uname , Cnic, password from carregister", sqlConn);
sqlConn.Open();

SqlDataReader dr = sqlComm.ExecuteReader();

while (dr.Read())
{
    name = dr["Uname"].ToString();
    cnic = dr["Cnic"].ToString();
    passs = dr["password"].ToString();

    if (name.Equals(c.Uname) && cnic.Equals(c.Cnic) && passs.Equals(c.pass))
    {
        Session["Uname"] = Text1.Value.ToString();
        Session["cnic"] = Text2.Value.ToString();

        Response.Redirect("Carloby.aspx");
    }
    else 
    {
        Response.Redirect("wrongidpass.aspx");
    }
}

You are reading ALL rows of your usertable and start comparing with the first received row.您正在读取用户表的所有行并开始与收到的第一行进行比较。 If this doesn't match, you are already redirecting...如果这不匹配,您已经在重定向...

You could count only the matching rows from your database, and if that returns anything other than 1 , there is an error with username or password (or your database).您可以只计算数据库中匹配的行,如果返回1以外的任何内容,则用户名或密码(或您的数据库)存在错误。

c.Uname = Text1.Value.ToString();
c.Cnic  = Text2.Value.ToString();
//you don't store plaintext passwords in your db, do you?
c.pass = hash_the_password(Text3.Value.ToString());  

SqlConnection sqlConn = new SqlConnection(@"Data Source=DESKTOP-Q4AAHCG;Initial Catalog=practise;User ID=;Password=;Trusted_Connection=True");

SqlCommand sqlComm = new SqlCommand("SELECT COUNT(*) FROM carregister WHERE uname = @uname and cnic = @cnic and password = @hashedpassword", sqlConn);
sqlComm.Parameters.Add("@uname", SqlDbType.NVarchar).Value = c.Uname;
sqlComm.Parameters.Add("@cnic", SqlDbType.NVarchar).Value = c.Cnic;
sqlComm.Parameters.Add("@hashedpassword", SqlDbType.NVarchar).Value = c.pass;
sqlConn.Open();

if (Convert.ToInt32(sqlComm.ExecuteScalar()) == 1) {
  //you have exactly one row where uname, cnic and password match the entered values
    Session["Uname"] = Text1.Value.ToString();
    Session["cnic"] = Text2.Value.ToString();

    Response.Redirect("Carloby.aspx");
}
else 
{
    //no row matched 
    //(or more than one which is an error in the database, because uname should probably be unique)
    Response.Redirect("wrongidpass.aspx");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我知道我做错了验证。请说服我停下来:) - I know I'm doing validation wrong. Please persuade me to stop :) 我不知道我的 C# 代码做错了什么 - I don't know what I'm doing wrong in my C# code 我正在创建员工图表,我真的不知道出什么问题了。 我对此完全陌生。 - I am creating an employee chart, I really don't know what is wrong. I am totally new to this. 我收到一个错误:使用未分配的局部变量“艺术家”,不知道我在做什么错? - I get an error: use of unassigned local variable 'artist', don't know what i'm doing wrong? 有时我不知道某些方法属于哪些类。 我究竟做错了什么? - Sometimes I don't know which classes certain methods belong to. What am I doing wrong? 不知道我在单元测试中做错了什么 - Don't know what I'm doing wrong in my Unit Test 无法在其他课程中引用公共对象,请查看我的资料并让我知道我在做错什么 - Cannot reference public object in another class, please look at my source and let me know what I'm doing wrong 我正在统一使用Raycast,当我单击它时我想获取对象的位置,但是我不工作并且我不知道我在做什么错 - I am using Raycast in unity and i want to get the position of an object when i click on it but i does not work and i don't know what i am doing wrong 收到错误,Collection 被修改; 枚举操作可能无法执行,但我不知道我做错了什么,在哪里? - Receiving an error, Collection was modified; enumeration operation may not execute, but I don't know what I am doing wrong and where? Python和C#密码学:我做错了什么? - Python and C# Cryptography: What i'm doing wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM