private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
Ontrip _ontrip = new Ontrip(_FNAME);
string _query2 = "select CContactno from CustomerTbl where CUsername = @USERNAME";
string _query3 = "select Price from TransactionTypeTble T join PendingTransTbl P ON P.TransType = T.TransType ";
string _query4 = "select VehicleDescription from DriverTbl D join VehicleSpecTbl V ON D.VehicleType = V.VehicleType";
SqlConnection _sqlcnn = new SqlConnection("Data Source=MELIODAS;Initial Catalog=WeGo;Integrated Security=True");
_sqlcnn.Open();
try
{
SqlDataReader _reader = null;
SqlCommand _cmd = new SqlCommand("Select CFName+' '+CLName from CustomerTbl where CUsername=@USERNAME", _sqlcnn);
SqlParameter _param = new SqlParameter();
_param.ParameterName = "@USERNAME";
_param.Value = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
_cmd.Parameters.Add(_param);
_reader = _cmd.ExecuteReader(); //for displaying users name in the label
while (_reader.Read())
{
_ontrip._txtboxUsername.Text = _reader.GetString(0);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
using (SqlCommand _sqlcmd = new SqlCommand(_query2, _sqlcnn))
{
try
{
SqlDataReader _reader = null;
SqlParameter _param = new SqlParameter();
_param.ParameterName = "@USERNAME";
_param.Value = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
_sqlcmd.Parameters.Add(_param);
_reader = _sqlcmd.ExecuteReader(); //for displaying users name in the label
while (_reader.Read())
{
_ontrip._txtboxContact.Text = _reader.GetString(0);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
他们是一种方式让我阅读查询并显示输出,当我运行此代码时,他们的错误是他们已经是一个与命令关联的开放数据阅读器。我应该在文本框中显示多个数据
答案 0 :(得分:0)
完成阅读后尝试呼叫关闭。
_reader.Close();
答案 1 :(得分:0)
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
Ontrip _ontrip = new Ontrip(_FNAME);
string _query2 = "select CContactno from CustomerTbl where CUsername = @USERNAME";
string _query3 = "select Price from TransactionTypeTble T join PendingTransTbl P ON P.TransType = T.TransType ";
string _query4 = "select VehicleDescription from DriverTbl D join VehicleSpecTbl V ON D.VehicleType = V.VehicleType";
SqlConnection _sqlcnn = new SqlConnection("Data Source=MELIODAS;Initial Catalog=WeGo;Integrated Security=True;MultipleActiveResultSets=True ");
_sqlcnn.Open();
我添加了MultipleActiveResultSet或MARS