Asked by:
select 10 rows from sql database at a time, when button is clicked

Question
-
User1043493656 posted
Hello
So I am working on a project, and when the button is clicked I want to select the first 10 rows from a database, and when clicked again it selects the next 10 rows, and the next and so on. What I am working on is something of along the lines as a user blog, where that the user can click on the comments, and the first 10 comments are visible, and then they have the ability to click on view more comments, which will then display the next 10 comments. it allows 10 comments at a time.
Here is a look at my code, and if more code is needed just let me know, but this is where a lot of the action is.
Thanks and I appreciate any help
protected void ViewMoreComments_Click(object sender, EventArgs e)
{
LinkButton MoreComments = sender as LinkButton;
string UserName = Session["UserName"].ToString();
string Id = MoreComments.ID.Remove(0, 16);
//ViewMoreComments[Convert.ToInt32(Id)].Visible = false;
//CommentBox[Convert.ToInt32(Id)].Visible = false;
//PostComment[Convert.ToInt32(Id)].Visible = false;
BlogPost.RetrieveComments(UserName, UserPosts, Id, Text, UserComments,
CommentImage, CommentName, CommentDate, NumOfComments, ViewMoreComments);
}
public void RetrieveComments(string UserName, Panel UserPosts, string Comment, Label[] Text,
Label[,] UserComments, ImageButton[,] CommentImage,
Label[,] CommentName, Label[,] CommentDate,
LinkButton[] NumOfComments, LinkButton[] ViewMoreComments)
{
SqlCommand UpdateComments = new SqlCommand(@"Select Comments, UserName, Date FROM BlogComments WHERE UserPost=@UserPost", connection);
UpdateComments.Parameters.AddWithValue("@UserPost", Text[Convert.ToInt32(Comment)].Text);
UpdateComments.Parameters.AddWithValue("@UserName", UserName);
//int PostNumber = Convert.ToInt32(Comment[c].ID.Remove(0, 7));
//UpdateComments.Parameters["@PostNumber"].Value = Comment;
//int Post = Convert.ToInt32(UpdateComments.ExecuteScalar());
SqlDataAdapter adapter = new SqlDataAdapter(UpdateComments);
DataTable dt = new DataTable();
adapter.Fill(dt);
try
{
for (int c = 0; c < 10; c++)
{
CommentImage[Convert.ToInt32(Comment), c].AlternateText = dt.Rows[c]["UserName"].ToString();
CommentImage[Convert.ToInt32(Comment), c].ToolTip = dt.Rows[c]["UserName"].ToString();
CommentName[Convert.ToInt32(Comment), c].Text = dt.Rows[c]["UserName"].ToString();
CommentDate[Convert.ToInt32(Comment), c].Text = dt.Rows[c]["Date"].ToString();
UserComments[Convert.ToInt32(Comment), c].Text = dt.Rows[c]["Comments"].ToString();
if (UserComments[Convert.ToInt32(Comment), c].Visible == false)
{
UserComments[Convert.ToInt32(Comment), c].Visible = true;
CommentImage[Convert.ToInt32(Comment),c].Visible = true;
CommentName[Convert.ToInt32(Comment), c].Visible = true;
CommentDate[Convert.ToInt32(Comment), c].Visible = true;
if (c == 0)
{
c = 1;
NumOfComments[Convert.ToInt32(Comment)].Text = c + " Comment";
c = 0;
}
else if (c == 1)
{
c = 2;
NumOfComments[Convert.ToInt32(Comment)].Text = c + " Comments";
c = 1;
}
else
{
c = c + 1;
NumOfComments[Convert.ToInt32(Comment)].Text = c + " Comments";
c = c - 1;
}
if(c == 9)
{
ViewMoreComments[Convert.ToInt32(Comment)].Visible = true;
}
else
{
ViewMoreComments[Convert.ToInt32(Comment)].Visible = false;
}
}
else
{
UserComments[Convert.ToInt32(Comment), c].Visible = false;
CommentImage[Convert.ToInt32(Comment),c].Visible = false;
CommentName[Convert.ToInt32(Comment), c].Visible = false;
CommentDate[Convert.ToInt32(Comment), c].Visible = false;
}
}
}
catch
{
connection.Close();
return;
}
connection.Close();
}
Tuesday, February 27, 2018 10:26 PM
All replies
-
User1120430333 posted
So I am working on a project, and when the button is clicked I want to select the first 10 rows from a database, and when clicked again it selects the next 10 rows, and the next and so on. What I am working on is something of along the lines as a user blog, where that the user can click on the comments, and the first 10 comments are visible, and then they have the ability to click on view more comments, which will then display the next 10 comments. it allows 10 comments at a time.
It's called paging. Why are you trying to reinvent the wheel? Why are you trying to use a datatable in which I will assume is a Web application? .This can easily be done with the ADO.NET Entity Framework implementing paging.
https://www.codeproject.com/Tips/1080844/Pagination-with-Entity-Framework
https://dzone.com/articles/reasons-move-datatables
http://lauteikkehn.blogspot.com/2012/03/datatable-vs-list.html
Wednesday, February 28, 2018 1:03 AM -
User-707554951 posted
Hi crossfire19
The SQL SELECT TOP Clause
The SELECT TOP clause is used to specify the number of records to return.
The SELECT TOP clause is useful on large tables with thousands of records. Returning a large number of records can impact on performence.
https://www.w3schools.com/sql/sql_top.asp
Best regards
Cathy
Wednesday, February 28, 2018 5:51 AM -
User1043493656 posted
Hello Cathy
Thanks so much for the reply.
If I were to use the select top clause in the query wont that just return the same top 10 records in the database each time button is clicked, because it was something that did cross my mind. If I wanted to return 10 records from the database on button click, and then when clicked again return the next 10 records, and then the next and so on wouldn't there be a little more to the logic.
Thanks a lot.
Wednesday, February 28, 2018 4:58 PM -
User-707554951 posted
Hi crossfire19
You could have a static variable, after button click. i=i+10
Related link:
https://forums.asp.net/t/1387219.aspx?Moving+to+next+record+with+C+for+a+SQL+Server
Best regards
Cathy
Thursday, March 1, 2018 6:23 AM