-5

Hey Guys I have a problem and don't know how to resolve it I check the tble and enter the data and have data inside when I try with if (DTA.Rows.Count > 0) nothing is happening it is not login in. Weird thing is I have another application where is this working but there I am using Username and Password and there I am using if (DTA.Rows.Count == 1)

 SqlConnection CON = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\ECS_POS\ECS_POS\POS.mdf;Integrated Security=True;Connect Timeout=30");
        DataTable DTA = new DataTable();
        SqlDataAdapter SDA = new SqlDataAdapter("SELECT First,Role FROM users WHERE User='" + textBox1.Text + "'", CON);
        SDA.Fill(DTA);
        if (DTA.Rows.Count == 0)
        {

            if (DTA.Rows[0][0].ToString() == "something")
            {
                Home ss= new Home(DTA.Rows[0][0].ToString(), (DTA.Rows[0][1].ToString()));
                ss.Show();
                this.Hide();
            }
            else if (DTA.Rows[0][0].ToString() == "something")

            {
                Home ss = new Home(DTA.Rows[0][0].ToString(), (DTA.Rows[0][1].ToString()));
                ss.Show();
                this.Hide();
            }
            else if (DTA.Rows[0][0].ToString() == "something")
            {
                Home ss = new Home(DTA.Rows[0][0].ToString(), (DTA.Rows[0][1].ToString()));
                ss.Show();
                this.Hide();
            }

            else
            {
                MessageBox.Show("something something ");
Kleo
  • 5
  • 4
  • 1
    If the rows count is zero then why do you try to read from an inexistent row at index 0? – Steve Mar 14 '21 at 22:31
  • 1
    Also, remember that when you put your textbox input directly in the sql command you allow your users to type anything there even malicious sql code that could destroy or hack your database. See [Sql Injection](https://xkcd.com/327/) and [Parameterized queries](https://stackoverflow.com/questions/4892166/how-does-sqlparameter-prevent-sql-injection#:~:text=Basically%2C%20when%20you%20perform%20a%20SQLCommandusing%20SQLParameters%2C%20the,the%20SQL%20string%20and%20the%20array%20of%20parameters.) – Steve Mar 14 '21 at 22:33
  • when i try with 1 it is not login in nothing is happening and if try just if (DTA.Rows.Count == 0) without rest of it it is login in but I need that conditions and I know about the injection they have limited letters they can input – Kleo Mar 14 '21 at 22:34
  • Let me say this clearly. If the rows count is zero then you don't have any user with the name passed in the text. – Steve Mar 14 '21 at 22:37
  • Is row count zero or minus 1? If row count is -1 then no columns were added to the table. – jdweng Mar 14 '21 at 22:59
  • @Kleo What do you mean, "approximate"? When you look at the number of rows in the debugger, as I'm sure you have, what specific value does it give you? –  Mar 14 '21 at 23:06
  • sorry Data rows count is 0 – Kleo Mar 14 '21 at 23:27
  • What is the expected behavior when `DTA.Rows.Count == 0` ? What happened whed you used `DTA.Rows.Count > 0`? Can you share that code too? – Chetan Mar 15 '21 at 01:30
  • @ChetanRanpariya when i am using DTA.Rows.Count > 0 nothing like i didn't click the button – Kleo Mar 15 '21 at 10:34
  • Can you share the code which used `DTA.Rows.Count > 0`? Did you debug the code? Did it exeucted code inside the if block when you debugged? – Chetan Mar 15 '21 at 10:46
  • yes he did the command in debugger and after that nothing and if you see in the code I am trying to create login page with user role and to wright the user in the label on the ss just weirdest thing it is working on another app but with change DTA.Rows.Count ==1 here is DTA.Rows.Count == 0 – Kleo Mar 15 '21 at 12:11

1 Answers1

0

I figure it out

I change the name of the row that I am searching

SqlConnection CON = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\ECS_POS\ECS_POS\POS.mdf;Integrated Security=True;Connect Timeout=30");
            DataTable DTA = new DataTable();
            SqlDataAdapter SDA = new SqlDataAdapter("SELECT First,Role FROM Table1 WHERE Id='" + textBox1.Text + "'", CON);
            SDA.Fill(DTA);
            if (DTA.Rows.Count == 1)
            {
Kleo
  • 5
  • 4