Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Web App/Database

Status
Not open for further replies.

exodus300

Programmer
Mar 22, 2002
262
AU
Hey peoples,

I am experimenting creating a web application which connects to a database to check a username/password.
But when I try the app I get HTTP 500 (Internal Server Error). The project compiles successfully in BCB5.

I have never used database components in BCB before, but a few days ago I managed to connect to a database using a TDBGrid thing, now I want to get it to work with a web application.

Anyway, here's the essential code (i think):

Code:
void __fastcall TWebModule1::WebModule1waLoginAction(TObject *Sender,
      TWebRequest *Request, TWebResponse *Response, bool &Handled)
{
        if (Request->ContentFields->Values["UserName"] == "" ||
                Request->ContentFields->Values["Password"] == "")
        {
                Response->Content = ppBlankField->Content();
        }

        else if (AuthenticateUser(Request->ContentFields->Values["UserName"],
                Request->ContentFields->Values["Password"]))
        {
                Response->Location = Request->ContentFields->Values["URL"];
        }
        else
        {
                Response->Content = ppLoginFailed->Content();
        }
        Handled = true;
}
//---------------------------------------------------------------------------

bool TWebModule1::AuthenticateUser(AnsiString UN, AnsiString PW)
{
        // Construct SQL query
        Query->SQL->Add("SELECT DISTINCT UserName, Password "
                "FROM Users "
                "WHERE UserName = " + UN);

        // Execute the query
        Query->Active = true;

        if (Query->RecordCount == 0)
        {
                // Close the Query
                Query->Active = false;
                return false;
        }

        // Validate password
        if (PW == Query->FieldByName("Password")->AsString)
        {
                // Close the Query
                Query->Active = false;
                return true;
        }
        else
        {
                // Close the Query
                Query->Active = false;
                return false;
        }
}
//---------------------------------------------------------------------------
And here's the two database components I have:
Code:
  object Database: TDatabase
    Connected = True
    DatabaseName = 'DBUsers'
    LoginPrompt = False
    SessionName = 'Default'
    Left = 128
    Top = 8
  end
  object Query: TQuery
    DatabaseName = 'DBUsers'
    Left = 128
    Top = 56
  end
Is this the correct way to connect to a database in BCB? If not, could you please tell me the best way to do this?

Thanks in advance! [pc3]
 
Hey, I just worked it out, it was a problem with my SQL query. The new line to set the query is:
Code:
        Query->SQL->Add("SELECT DISTINCT UserName, Password "
                "FROM Users "
                "WHERE UserName = '" + UN + "'");

Well, tips for using DB's in BCB are still welcome! [pc3]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top