-1

i am building small login form and this issue showed up. I SQL server connected (i tried couple different connections aswell), table should be right, i don't see any issue in that.

I have tried already couple different SQL connections and new databases and tables.

private void BtnLogin_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(@"Data Source=MYDESKTOP\MSSQLSERVER01;Integrated Security=True;Connect Timeout=30;Encrypt=False; TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");

    SqlDataAdapter sqa = new SqlDataAdapter ("SELECT COUNT (*) from LOGINFORM where USERNAME ='" + txtUsername.Text +"' and PASSWORD = '" + txtPassword.Text + "'", con);
    DataTable dt = new DataTable();
    sqa.Fill(dt);

    if (dt.Rows[0][0].ToString() == "1")
    {
        this.Hide();
        Form2 main = new Form2();
        main.Show();
    }
    else
    {
        MessageBox.Show("Username/Password is incorrect. Please try again");
    }
}

Expection unhandled invalid object name "LOGINFORM"

R.J. Dunnill
  • 2,049
  • 3
  • 10
  • 21
  • Always use parameterized sql and avoid string concatenation to add values to sql statements. See [How can I add user-supplied input to an SQL statement?](https://stackoverflow.com/q/35163361/1260204), and [Exploits of a Mom](https://xkcd.com/327/). – Igor Jun 05 '19 at 21:20
  • 2
    The main problem causing the error is you never specify a database to use in your connection string so the catalog/database used is the default one associated with the account you are connecting which is most likely `master`. This database does (and should not) have a table named `loginform` – Igor Jun 05 '19 at 21:21

1 Answers1

0

You need to set your "Initial Catalog" in your connection string. If you don't specify it, then the default database will be "master", which likely does not contain your table "LOGINFORM". Another option is to fully qualify the table name in your query like databasename.owner.tablename.

reasonet
  • 145
  • 12