[英]C# login form Error “There is no row at Position 0”
I am new to C# coding.我是 C# 编码的新手。 When I enter the Username and Password in a Login Form application with values already stored in
SignUp
database, An Error There is no row at position 0" comes up.当我在登录表单应用程序中输入用户名和密码且值已存储在
SignUp
数据库中时,出现错误position 0" 处没有行。
Below is the link to error screenshot.以下是错误截图的链接。
Screenshot: Error Message屏幕截图:错误消息
And below is the code which is giving trouble.下面是给麻烦的代码。
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("!!Please fill in both Username and Password!! ");
}
else
{
SqlConnection sqlcon = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""C:\Users\Lenovo\Desktop\dev\C# .net\Aadi Paw Plethysmometer\Aadi Paw Plethysmometer\Database1.mdf"";Integrated Security=True");
string query = "Select * from Signup where Username = '" + Username.Text.Trim() + "' and Password = '" + Password.Text.Trim() + "'";
SqlDataAdapter sda = new SqlDataAdapter(query, sqlcon);
DataTable dtbl = new DataTable();
sda.Fill(dtbl);
if (dtbl.Rows[0][0].ToString() == "1")
{
this.Hide();
new Form2().Show();
}
else
{
MessageBox.Show("Invalid username or password");
textBox1.Text = textBox2.Text = "";
}
}
}
And below is the link to screenshot of the SignUp Data Table.以下是注册数据表屏幕截图的链接。
When no results are found in the DB, you are still trying to access Row 0 of the DataTable, which does not exist as your error states.当在 DB 中未找到结果时,您仍在尝试访问 DataTable 的第 0 行,因为您的错误状态不存在该行。 Replace your condition as follows.:
替换您的条件如下:
if (dtbl.Rows.Count > 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.