Sql Reader returning null value

Asked

Viewed 289 times

2

I’m running a datareader, however does not return any value, already checked in the table and conditions (where) of select exist in the table.

The code goes below:

public void consulta()
    {
        string sqltring = @"Data Source=tptspo01sql01.database.windows.net;Initial Catalog=Tptecnologia;Integrated Security=False;User ID=******;Password=******;Connect Timeout=60;Encrypt=False;TrustServerCertificate=False";
        string _sql = string.Empty;
        SqlConnection sqlcon = new SqlConnection(sqltring);

        string usu, pass, idemp1, idusu;
        usu = textBox1.Text;
        pass = textBox2.Text; 

        try
        {
            sqlcon.Open();

            _sql = "SELECT * FROM Login WHERE usuario = @usuario AND Passwd = @Passwd";

            SqlCommand cmd = new SqlCommand(_sql, sqlcon);

            cmd.Parameters.Add("@usuario", SqlDbType.VarChar).Value = usu;
            cmd.Parameters.Add("@Passwd", SqlDbType.VarChar).Value = pass;

            SqlDataReader reader2 = cmd.ExecuteReader();


            while (reader2.Read())
            {
                idemp1 = reader2["Id_empresa"].ToString();
                idusu = reader2["id_usu"].ToString();

                MessageBox.Show("id empresa " + usu + "\b" + "is usuario " + pass);
            }

        }

        catch(SqlException error)
        {
            MessageBox.Show(error + "Na segunda consulta de id empresa");
        }

        finally
        {
            sqlcon.Close();
        }



    }
  • It got hard to help, I may have missed something, but everything seems okay, except maybe the data is not as you imagine. For not being sure I didn’t vote for "can’t be reproduced," but it seems we can’t help, at least not just with this.

  • 2

    Young, even if your server does not accept connection of any IP do not leave your credentials thus open to the public.

  • wow, truth.. I didn’t even attack myself, sorry. and thank you for editing it. I’ll try to see something strange on my chart, I’ll come back something. thank you

1 answer

0

Thanks for your help, I decided, what happened was:

the two data I was looking for in the table, are of type INT, and I was wanting to run a datareader, which looks for values of type string. hence the error, I solved by applying a query of type executescalar.

Thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.