2 Replies - 1525 Views - Last Post: 02 April 2012 - 07:52 PM Rate Topic: -----

#1 ankit9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 18-February 11

How to retrive user's GUID on the basis of email id from two diffe

Posted 01 April 2012 - 05:48 AM

hi!! i need to retrive a user's GUID on the basis of email id which is stored in one table and the GUlD is stroed in different table..

i am using inner join for this below is the code can someone please tell whether it's correct or not....

   SqlDataReader reader = new SqlDataReader();

                DataColumn dc = ds.Tables["users_detail"].Columns["u_email_id"];


                while (reader.Read())
                {

                    if (reader["u_email_id"].ToString() == passrec_txt.Text)
                    {

                        string e = "select u_id from users_detail where u_email_id='" + passrec_txt.Text + "'";


                        string key = "SELECT u_security.user_guid FROM u_security INNER JOIN users_detail on u_security.u_id=e ";


Thank's in advance

MOD EDIT: Added code tags. When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 01 April 2012 - 05:54 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to retrive user's GUID on the basis of email id from two diffe

#2 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,377
  • Joined: 02-June 10

Re: How to retrive user's GUID on the basis of email id from two diffe

Posted 01 April 2012 - 08:30 AM

Quote

can someone please tell whether it's correct or not....


I hate to sound like a smart ass (okay, no I don't) but if you get the results you expected then its correct. If you didn't, then its not.

Are you getting errors... unexpected results...?
"It doesn't work" isn't good enough - How you can help us, to help you



Database tutorials: FAQ # 22. (Click the SHOW button below)


TOP most asked:


What does this error message mean?

FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Mar 2012
Spoiler



Was This Post Helpful? 0
  • +
  • -

#3 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,504
  • Joined: 28-April 09

Re: How to retrive user's GUID on the basis of email id from two diffe

Posted 02 April 2012 - 07:52 PM

Your join is not done correctly. When joining two tables you need to join them using columns from each table. You can't join two columns based off of a column in one table matching an outside value because then it doesn't know what column it should be matching in the second table. You should specify a value to match in the WHERE clause of the query.

SELECT u_security.user_guid 
FROM u_security as usec 
INNER JOIN users_detail as udet
on usec.u_id = udet.u_id
WHERE udet.u_email_id='" + passrec_txt.Text + "'"



That should be closer to what you need. This way you only have to open one connection to the database instead of two. However, this is really a good way to have someone do a SQL injection attack on your site. If you're not sure what a SQL injection attack is you should google it.

Instead of directly passing a textbox value into your query you should be using parameters. In which case the query would look like this.

SELECT u_security.user_guid 
FROM u_security as usec 
INNER JOIN users_detail as udet
on usec.u_id = udet.u_id
WHERE udet.u_email_id = @email



If you're using a basic SqlCommand object to execute your query then you would create a parameter for it in the following way.

command.Parameters.AddWithValue("@email", passrec_txt.Text);



A few things you might want to research:
ADO.Net
SqlCommand Parameters
SQL Injection
SQL Joins

This post has been edited by Nakor: 02 April 2012 - 07:53 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1