1

I'm trying to make my first steps in databases used with WPF. The problem is that once I start the application, I get an error saying

Invalid Object - "tblUser"

where tblUser is the name of a table.

I made sure that table name is correct, tried creating another table to see whether it changes anything. All the permissions are granted to manipulate the table.

private void Submit_OnClick(object sender, RoutedEventArgs e)
{
    SqlConnection sqlCon = new SqlConnection(@"Server=localhost\SQLEXPRESS;Database=master;Trusted_Connection=True;");

    try
    {
        if (sqlCon.State == ConnectionState.Closed)
            sqlCon.Open();

        String query = "SELECT COUNT(1) FROM tblUser WHERE Username = @Username AND Password = @Password";

        SqlCommand sqlCmd = new SqlCommand(query, sqlCon);
        sqlCmd.Parameters.AddWithValue("@Username",txtUsername.Text);
        sqlCmd.Parameters.AddWithValue("@Password", txtPassword.Text);

        int count = Convert.ToInt32(sqlCmd.ExecuteScalar());

        if (count == 1)
        {
            MainWindow dashboard = new MainWindow();
            dashboard.Show();
            this.Close();
        }
        else
        {
            MessageBox.Show("Username or password does not exist");
        }
    }
    catch (Exception exception)
    {
        MessageBox.Show(exception.Message);
    }
    finally
    {
        sqlCon.Close();
    }
}

First I create a connection to the database. Next step is I would like to check whether connection is closed, if it is I'm opening it. Then it looks like something went wrong with the SQL query because it seems not to recognise tblUser and sees it as an invalid one.

  • 4
    Obligatory "never store passwords in plaintext" notice. – itsme86 Aug 13 at 14:31
  • 2
    Whats the meaning of the String query = "***SELECT COUNT(1) FROM tblUser WHERE [email protected] AND [email protected]"***; , you have *** inside the string and then *** out of the string – Catarina Ferreira Aug 13 at 14:31
  • 2
    What's with all the asterisks around your query? – itsme86 Aug 13 at 14:32
  • 2
    I wouldn't bother checking the connection state before opening it. You can see that you just created the connection object just above. There's no chance that it's open. That's really only useful when passing the connection object around. – itsme86 Aug 13 at 14:35
  • 2
    @Fueledbypizza "just skip the asterisks" - better: just edit the question and remove them. – Clemens Aug 13 at 14:38
4

tblUser is certainly not in the master database. The asterisks are not part of SQL. I am surprised that the error message doesn't complain about that at first

  • 1
    The error message doesn't mention it because for some reason OP included those here even though they aren't in the actual code. From their comment: "Asterisks are not included in the code" – Broots Waymb Aug 13 at 14:37
  • Asterisks were my mistake while editing code. Now it's corrected. – Fueled by pizza Aug 13 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.