Domanda

This is my code:

protected void logujButton_Click(object sender, EventArgs e)
{
    string user = "data source=myHostServer; database = myDataBase; user id=myLogin; password=myPassword";
    SqlConnection con2 = new SqlConnection(user);
    con2.Open();

    string loguj = "select count(*) from uzytkownik where Login = '"+ logujTextBox.Text +"'";
    SqlCommand command = new SqlCommand(loguj, con2);
    int wartosc = Convert.ToInt32(command.ExecuteScalar().ToString());
    con2.Close();

    if (wartosc == 1)
    {
        con2.Open();
        SqlCommand pobierzHaslo = new SqlCommand("select Haslo from uzytkownik where Login = '" + logujTextBox.Text + "'", con2);
        SqlDataReader rdr = pobierzHaslo.ExecuteReader();
        string haslo = rdr["Haslo"].ToString();
        if (haslo == hasloTextBox.Text)
        {
            errorLabel.Text = "Prawidlowe Haslo !";
        }
        else
        {
            errorLabel.Text = "Zle haslo !";
        }
    }
    else 
    {
        errorLabel.Text = "Taki uzytkownik nie istnieje !";
    }
}

When I press button, this error is appearing: "Invalid attempt to read when no data is present". Could You tell me, where i made mistake ?. Thanks for advise !

È stato utile?

Soluzione

You haven't read anything from the reader yet. You have to call the Read() method:

 SqlDataReader rdr = pobierzHaslo.ExecuteReader();
 if (rdr.Read())
 {
        string haslo = rdr["Haslo"].ToString();
        ....
 }

Altri suggerimenti

If you have access to SSMS, run the query directly in a query window and make sure that you get data back. Your query may be bad. It is most likely an error from the ExecuteReader method of your SqlDataReader, based on the text of the error message.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top