0

I am trying to authenticate a user through a login form by reading user details from my database UsersDB. On attempting to read, I get an error: Invalid object name: UsersDB

I got no errors when adding a new user to the database so I do not know why I am getting this error. Here is the stack trace I am getting:

[SqlException (0x80131904): Invalid object name 'UsersDB'.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +388
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +810
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +4403
System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +82
System.Data.SqlClient.SqlDataReader.get_MetaData() +135
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +6666037
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite) +6667856
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +577
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +107
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +288
System.Data.SqlClient.SqlCommand.ExecuteReader() +302
AuthWebRole.Account.UserLogin.Buttonlogin_Click(Object sender, EventArgs e) in c:\Users\Tamara\Documents\Visual Studio 2012\Projects\TCWalletAzure\AuthWebRole\Account\UserLogin.aspx.cs:32
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +155
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3804

Edit:

The query is as follows: (I replaced my username and password in my code)

string strcon = "Server=tcp:qemcclhlar.database.windows.net,1433;Database=UsersDB;User ID=[userid];Password=[mypassword];Trusted_Connection=False;Encrypt=True;Connection Timeout=30";
        SqlConnection con = new SqlConnection(strcon);

        SqlCommand com = new SqlCommand("CheckUser", con);
        com.CommandType = CommandType.StoredProcedure;
        SqlParameter user = new SqlParameter("username", UserName.Text);
        SqlParameter pword = new SqlParameter("password", Password.Text);
        com.Parameters.Add(user);
        com.Parameters.Add(pword);
        con.Open();
        SqlDataReader rd = com.ExecuteReader();
        if (rd.HasRows)
        {
            rd.Read();
            LabelInfo.Text = "Login successful.";
        }

        else
        {
            LabelInfo.Text = "Invalid username or password.";

        }

Database Schema:

Database: UsersDB with table UserTable

Tamara Caligari
  • 479
  • 3
  • 12
  • 28

2 Answers2

0

Well the important thing here is probably the connection string. Do the following: create an empty text file and rename it "myconnection.udl". Now double click on the file and it will launch an applet. You can configuer the connection to your database and test it. Now open the udl file in notepad, you will see the correct connection string. Copy connections string to your app connection settings. UDL files are generally misunderstood. They are simply a text file that holds the connection settings. They then call the connection dll. If the udl file works then you have a correct connection string 100%

Ian P
  • 1,724
  • 1
  • 10
  • 12
0

I solved my problem - I entered the wrong table in the procedure query. It had to be as follows:

create PROCEDURE CheckUser ( @username as varchar(50), @password as varchar(50) ) AS SELECT * FROM UserTable WHERE Username=@username AND Password=@password

I erroneously entered UsersDB instead of UserTable

Tamara Caligari
  • 479
  • 3
  • 12
  • 28