Click here to Skip to main content
15,921,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi this is eswar , i want single user posted data from server so i wrote below code but is get more results so plz help me.

this is my query plz modify this query for best results..thank you
C#
"SELECT n.newsid, n.state, n.district, n.region, n.views, n.currentplace, n.newstitle, n.description, n.poston, n.postby, (SELECT TOP 1 imagepath FROM tbl_userpost_newsimages ni WHERE nipostby='"+lbl_upname.Text+"'  and n.newsid=ni.newsid) AS imagepath FROM  tbl_userpost_news  n Order By n.poston desc"
Posted
Updated 25-Nov-15 2:02am
v2

1 solution

1. You do not have a WHERE clause on your SQL statement. You are using one in the subquery but you need to also put one on the main query or else you get all records.
2. Never use string concatenation like this for parameters. I could steal your data with the code you have written using SQL injection techniques. Instead use parameterized statements, like this:

C#
string SQL = "SELECT * FROM table1 WHERE field1 = @userName";
...
cmd.CommandText = SQL;
...
cmd.Parameters.AddWithValue("@userName", lbl_upname.Text);
...
 
Share this answer
 
Comments
Member 10575434 25-Nov-15 8:38am    
sir i want image path from another table and data from another table so i write above code please once again check it ...thank you for your reply .
ZurdoDev 25-Nov-15 8:43am    
You need to use a JOIN instead of a subquery.

Something like

SELECT a.field1, b.field1
FROM table1 a
LEFT JOIN table2 b ON a.userid = b.userid
WHERE a.userid = @userid

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