Click here to Skip to main content
15,915,050 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
string str = string.Format(@"select m.autoid,m.pcode, m.fyyear, m.date, m.salary, m.ta, m.contigency, m.nrc, m.institcharges, m.others,y.yearlyalloc,y.salary,y.ta,y.contigency,y.nrc,y.institcharges,y.others FROM monthly AS m inner join yearly y pcode where m.pcode=('" + DropDownList1.SelectedItem.ToString() + "' ) AND y.fyyear=('" + DropDownList2.SelectedItem.ToString() + "')", con);
       SqlDataAdapter da = new SqlDataAdapter(str, con);
       DataTable dt = new DataTable();
       da.Fill(dt);
       GridView1.DataSource = dt;
       GridView1.DataBind();


i am able to filter the data but i what i am not able to do is how to join two tables together and use them with dropdownlist condition
Posted
Updated 13-Mar-13 19:57pm
v3
Comments
Prasanna Devarajan 14-Mar-13 2:09am    
Can you be more specific, do you have problem in query formation ?
where m.pcode=('" + DropDownList1.SelectedItem.ToString() + "' )

Why there are brackets ?
a2ulthakur 14-Mar-13 2:12am    
yes the problem is that when i am trying to execute it my sql data adapter is not filling up neither is the the data table so i think there must be something wrong with my query.
a2ulthakur 14-Mar-13 2:14am    
@tadit i reused this query from another page of mine but in that page i was just populating data from one table only i wasn't using any join.
As you can see there are two answers saying that you have missed the join condition, which is specified by ON keyword.
So, try and let us know.

see the syntax of INNER JOIN

SQL
SELECT ft.col1, ft.col2, st.col1, st.col2
FROM FristTable ft
INNER JOIN SecondTable st
ON ft.colname = st.colname


in your query there is no ON keyword...
 
Share this answer
 
v2
correct the "inner join" query use "on" keyword to connect both tables
example:
SQL
select * FROM monthly AS m inner join yearly y on  m.pcode=y.pcode where m.pcode=('" + DropDownList1.SelectedItem.ToString() + "' ) AND y.fyyear=('" + DropDownList2.SelectedItem.ToString() + "'
 
Share this answer
 
v2
Comments
a2ulthakur 14-Mar-13 2:22am    
string str = string.Format(@"select m.autoid,m.pcode, m.fyyear, m.date, m.salary, m.ta, m.contigency, m.nrc, m.institcharges, m.others,y.yearlyalloc,y.salary,y.ta,y.contigency,y.nrc,y.institcharges,y.others FROM monthly AS m inner join yearly y on pcode where m.pcode=('" + DropDownList1.SelectedItem.ToString() + "' ) AND y.fyyear=('" + DropDownList2.SelectedItem.ToString() + "')", con);

this is my corrected query its giving me following errors :
An expression of non-boolean type specified in a context where a condition is expected, near 'where'.
Again the ON condition is not correct.
It should contain something like below as suggested by @lakshmilkk.
on m.pcode=y.pcode
vishal.shimpi 14-Mar-13 2:33am    
u cant write only 'ON pcode' becouse it requires boolean condition
a2ulthakur 14-Mar-13 2:39am    
@tadit i have more problem i want my gridview this way
http://postimage.org/image/5hpr4k521/
Can you tell me what is the problem, which you are facing, while designing this Grid ?

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