【发布时间】:2023-03-21 11:15:01
【问题描述】:
我正在尝试根据我的数据库中的数据集的结果来验证来自表单的用户输入。我不知道我是在实现逻辑还是在尝试 foreach 循环(我只是根据逻辑编造的)。我正在尝试将数据表的索引(从数据集创建)分配给变量,然后检查它们是否与用户输入匹配。这是一个体面的方法吗?可能吗?我怎样才能使这项工作?
我从代码中得到一个错误,我认为我正确地实现了你可以在代码的 cmets 中找到的代码。
protected void btnSubmit_Click(object sender, EventArgs e) { //为数据集创建连接以填充电子邮件和密码以进行验证 字符串 ConnectionString = ConfigurationManager.ConnectionStrings["ContourCoffeeRoastersConnectionString"].ConnectionString; ; SqlConnection 连接 = 新的 SqlConnection(ConnectionString); Conn.Open();
//creats a data set with database information and puts the dataset in a datatable
SqlDataAdapter daCustomer = new SqlDataAdapter("Select (CustEmail, CustPW) From Customer", Conn);
DataSet dsEmailsandPW = new DataSet("Emails");
daCustomer.Fill(dsEmailsandPW, "Customers");//I get and error here in my stack trace
DataTable tblCustomers;
tblCustomers = dsEmailsandPW.Tables["Customers"];
//sets the variable to user inputed data from the login form so it can be compared and validated to the dataset
string custEmail = exampleInputEmail1.Text;
string custPW = exampleInputPassword1.Text;
//looks through each row on the data set to see if a matching email can be found
foreach (DataRow drCurrent in tblCustomers.Rows)
{
string txtEmail = drCurrent[0].ToString();//sets a variable to the first index of the current row of the dataset
if (txtEmail == custEmail)//if a match is found with the user input and a record in the database through the data set the password is then checked for validation
{
string txtPW = drCurrent[1].ToString();//assigns a vaiable to the second index of the row that should contain customer password
if (txtPW == custPW)//if the password is a match
{
lblLogin.Text = "You are logged in!";
//TODO: query for cartID and set it to the cookie!!!!!
}
else
{
lblLogin.Text = "Email/username combination is not correct";
}
}
else
{
lblLogin.Text = "Email/username combination is not correct";
}
}
【问题讨论】:
标签: c# asp.net validation datatable dataset