variables and sql query c#

Greetings Experts,

After logging in a user I want to display records from a second database. I tried to inner join the databases but I don't think its possible in WebMatrix.

If the email field within the database used for logging user in is the same as the email field in the second database, what code do I need to use this variable to insert it into new query against the second database so I am able to show user records.

I am adding my attempts so far.. any direction greatly appreciated. Regards. SJ

// Initialize general page variables
var authenticatedUser = "";
var authenticatedUserFirstName = "";
var authenticatedUserLastName = "";
var authenticatedUseremail = "";
if (WebSecurity.IsAuthenticated) {
    authenticatedUser = WebSecurity.CurrentUserName;
    var db = Database.Open("StarterSiteTest");
   var UserData = db.QuerySingle("SELECT FirstName, LastName, email FROM UserProfile WHERE LOWER(Email) = LOWER(@0)", authenticatedUser);
    authenticatedUserFirstName = UserData.FirstName;
    authenticatedUserLastName = UserData.LastName;
      authenticatedUserLastName =;


<!DOCTYPE html>

<html lang="en">
        <meta charset="utf-8" />
        @if (WebSecurity.IsAuthenticated) {
                                Hello, <a class="username" href="~/Account/PasswordReset" title="Change password">@authenticatedUserFirstName @authenticatedUserLastName,  @authenticatedUseremail</a>!
                                <a href="~/Account/Logout">Logout</a>



Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
It should be possible providing the login you are using for the SQL Server has permissions on both databases.  Cross-database queries are a feature of the database server itself - the version of Visual Studio is irrelevant.

Syntax for a cross-database query would be something like:
select a.[Name], a.[Email], b.[Age]
	from [LoginInfo] a
		inner join [AdditionalUserData].[dbo].[UserInfo] b on a.[Email] = b.[Email]

Open in new window

Bear in mind that i'm just assuming SQL Server here - there is nothing in your code that indicates what database platform you are connecting to - and you also look to be using a custom data access library which may also restrict the queries you can perform.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sunny-jAuthor Commented:
Dear Carl, thank you for your reply. Just going back a step, assuming that the login information is on the same database, what are the steps for capturing the username and displaying associated records. I understand the queries involved but I am struggling to understand how to link retrieving the login data and then matching with records and would welcome some basic outline of what is involved and some basic code to try out, just showing how the results hang together with the initial query.  I seem to learn quicker when I can see a demonstration. thanks again. SJ
sunny-jAuthor Commented:
thanks for your answer - it prompted me to look at the issue in another way, thanks. SJ
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.