Answer the question
In order to leave comments, you need to log in
How to fix the error "System.InvalidOperationException: "Invalid read attempt with no data.""?
How to fix the error "System.InvalidOperationException: "Invalid read attempt with no data.""?
using (SqlConnection connection = new SqlConnection(@"Data Source=...; Initial Catalog=Airlines; Integrated Security=True"))
{
connection.Open();
string sqlExpression = "SELECT ID FROM Users WHERE ID = 1";
SqlCommand LoginData = new SqlCommand(sqlExpression, connection);
SqlDataReader LoginReaderData = LoginData.ExecuteReader();
if (LoginReaderData.HasRows)
{
object ID = LoginReaderData["ID"];
Mail.Text = (string)ID;
}
else
{
MessageBox.Show("Пользователь не найден");
}
}
Answer the question
In order to leave comments, you need to log in
You are using the wrong reader.
SqlDataReader loginReaderData = LoginData.ExecuteReader();
while (loginReaderData.Read())
{
var id = loginReaderData["ID"];
// ...
}
string sqlExpression = "SELECT ID FROM Users WHERE ID = 1";
SqlCommand loginData = new SqlCommand(sqlExpression, connection);
var id = (string)loginData.ExecuteScalar();
//...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question