Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am trying to search the database using Data List and Item Template.

I just want to loop the data's from database in a linkbutton and a lable row by row.I am new to this.
C#
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Description", typeof(string)));
dt.Columns.Add(new DataColumn("Auctionno", typeof(string)));
dt.Columns.Add(new DataColumn("Location", typeof(string)));
SqlCommand cmd = new SqlCommand("select * from Auction_Upload where Keyword = '" + TextBox1.Text + "'", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if(dr.Read())
{
DataRow dc = dt.NewRow();
dc["Description"] = dr["Description"].ToString();
dc["Auctionno"] = dr["Auctionno"].ToString();
dc["Location"] = dr["Location"].ToString();
dt.Rows.Add(dc);
}
DataList1.DataSource = dt;
DataList1.DataBind();



Please Help me in this
Thanks in advance
Posted
Updated 26-Aug-15 1:12am
v3
Comments
DamithSL 26-Aug-15 3:08am    
what is the issue with your code?
Prabhu92 26-Aug-15 3:10am    
I just want to loop the linkbutton and lable using foreach. Need to display all the data's from database row by row
[no name] 26-Aug-15 4:35am    
Do you want to create multiple linkbutton based on your result? Secondly you are looping over your results in code. Please describe when you display all data..
Prabhu92 26-Aug-15 4:55am    
I want to display all the data from database. I mean all the rows
Prabhu92 26-Aug-15 5:03am    
i have changed if condition to while and it worked. Thanks

1 solution

Please see the comment to the question by Richard Deeming.

Your approach is wrong from the very beginning. The query composed by concatenation with strings taken from UI. Not only repeated string concatenation is inefficient (because strings are immutable; do I have to explain why it makes repeated concatenation bad?), but there is way more important issue: it opens the doors to a well-known exploit called SQL injection.

This is how it works: http://xkcd.com/327.

Are you getting the idea? The string taken from a control can be anything, including… a fragment of SQL code.

What to do? Just read about this problem and the main remedy: parametrized statements: http://en.wikipedia.org/wiki/SQL_injection.

With ADO.NET, use this: http://msdn.microsoft.com/en-us/library/ff648339.aspx.

Please see my past answers for some more detail:
EROR IN UPATE in com.ExecuteNonQuery();,
hi name is not displaying in name?.

—SA
 
Share this answer
 

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