Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
My Project contain 2 tables.
DetailsTB and ImageTB

Now DetailsTB contain Primary key as a Composite Key of two column of Receipt,Passenger_Name
e.g. PRIMARY KEY(Receipt, Passenger_Name)

and this FOREIGN KEY(Receipt, Passenger_Name) is Primary key for ImageTB

e.g. FOREIGN KEY (Receipt, Passenger_Name) REFERENCES DetailsTB(Receipt, Passenger_Name), PRIMARY KEY (Receipt, Passenger_Name) for table ImageTB

I want to select record in both table but my query cant work properly

My query:

cmd = new SqlCommand("SELECT Picture FROM ImageTB WHERE DetailsTB.Passenger_Name = '" + datagridview_details.Rows[e.RowIndex].Cells[6].Value.ToString() + "' AND DetailsTB.Passenger_Name = ImageTB.Passenger_Name", con);

it gives me various sqlexception error.

So plz help me for the same query.

thanks in advance
Posted
Comments
thatraja 12-Dec-13 2:59am    
What's the error message? Always include that in your question
er.shoaib 12-Dec-13 5:10am    
It gives me error everytime
The multi-part identifier "DetailsTB.Passenger_Name" could not be bound.
creepz03 12-Dec-13 22:46pm    
Base from what you posted, I don't think you even know what PRIMARY KEY and FOREIGN KEY means.. So I'd suggest you start by learning the basic of the SQLs,..

use JOIN in your query
C#
cmd = new SqlCommand(string.Format("SELECT Picture FROM ImageTB itb join DetailsTB dtb on dtb.Passenger_Name = itb.Passenger_Name WHERE dtb.Passenger_Name = '{0}'", datagridview_details.Rows[e.RowIndex].Cells[6].Value.ToString()), connection);


also you need check that atagridview_details.Rows[e.RowIndex].Cells[6].Value is not null
(or you will get NullReferenceException)

and also be aware of Sql injection
 
Share this answer
 
Comments
BobJanova 12-Dec-13 12:24pm    
Right. It should be a parameterised query though to avoid SQL injection as you say.

I would use a left join for this, as I would say it's more useful to get a row with a null for ImageTB.Picture rather than no results (which is what happens with an inner join).
I suggest you look at the use of JOINs.

Basic example: http://www.w3schools.com/sql/sql_join.asp[^]
 
Share this answer
 
you have to understand about SQL Join to use multiple tables condition.

visit here to understand ...

SQL Retrieve data from multiple tables[^]

or

Use Join With Multiple Tables[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900