[英]Encountering a System.IndexOutOfRangeException: when trying to display data from two tables in a database
I am writing a form application.我正在编写一个表单应用程序。 User inputs his name, email, address etc into text boxes as if he was ordering a package.
用户在文本框中输入他的姓名、email、地址等,就好像他在订购 package。 If the user has already made an order once I want to make it possible for the user to enter his email into the text box and based on his email fill out all the other personal information needed for the package.
如果用户已经下过订单,我想让用户可以在文本框中输入他的 email 并根据他的 email 填写 ZEFE90A8E604A7C6470E88D03A78 所需的所有其他个人信息
The trouble I am having is that his data is in two different tables.我遇到的麻烦是他的数据在两个不同的表中。 The data which is in customer table (his first and last name) I have successfully retrieved, but the data in the table address I don't know how to get.
客户表中的数据(他的名字和姓氏)我已成功检索,但表地址中的数据我不知道如何获取。
Here is the code:这是代码:
{
try
{
var connection = getConnection();
var command = new SqlCommand
{
Connection = connection,
CommandText = "SELECT * FROM Customer WHERE Email = @Email"
};
command.Parameters.Clear();
command.Parameters.AddWithValue("@Email", mailBox.Text);
connection.Open();
reader = command.ExecuteReader(CommandBehavior.SingleRow);
if (reader.Read())
{
fnameBox.Text = reader["fname"].ToString();
lnameBox.Text = reader["lname"].ToString();
command.CommandText = "SELECT * FROM address WHERE customerID= "+ reader["customerID"].ToString();
stateBox.Text = reader["state"].ToString(); //part where the error happens
cityBox.Text = reader["city"].ToString();
addressBox.Text = reader["address"].ToString();
zipBox.Text = reader["zip"].ToString();
int result = command.ExecuteNonQuery();
connection.Close();
if (result > 0)
{
MessageBox.Show("Success");
}
else
{
MessageBox.Show("Error");
}
}
else
{
MessageBox.Show("E-mail entered doesn't exist");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
} ```
state
field, which in turn is caused by your not actually executing your SELECT * FROM address
query - you are setting the command text but doing nothing further.state
字段的结果集引起的,而这又是由于您没有实际执行SELECT * FROM address
查询引起的 - 您正在设置命令文本但没有做任何进一步的事情。 You need to create another DataReader
for the second query and read those results.DataReader
并读取这些结果。 Overall there's a lot of stuff to improve, but you're clearly at an early state in learning this so that's fine for now....总的来说,有很多东西需要改进,但你显然是在早期的 state 学习这个,所以现在很好......
you didn't finish to read the first result and after this trying to get the second one你没有读完第一个结果,然后试图得到第二个
command.Parameters.Clear();
command.Parameters.AddWithValue("@Email", mailBox.Text);
var customerID=0;
var success=false;
connection.Open();
var reader1 = command.ExecuteReader();
if (reader1.Read())
{
sucess=true;
fnameBox.Text = reader1["fname"].ToString();
lnameBox.Text = reader1["lname"].ToString();
customerID= Convert.ToInt32( reader1["customerID"].ToString());
reader1.Close();
}
if( sucess)
{
command.CommandText = "SELECT * FROM address WHERE customerID = @CustomerID";
command.Parameters.Clear();
command.Parameters.AddWithValue("@CustomerID", customerID);
var reader2 = command.ExecuteReader();
sucess=false;
if (reader2.Read())
{
sucess=true;
stateBox.Text = reader2["state"].ToString();
cityBox.Text = reader2["city"].ToString();
addressBox.Text = reader2["address"].ToString();
zipBox.Text = reader2["zip"].ToString();
reader2.Close();
}
if (success)
{
MessageBox.Show("Success");
}
else
{
MessageBox.Show(" address select Error");
}
}
else
{
MessageBox.Show("E-mail entered doesn't exist");
}
connection.Close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.