I have issue with how record is being displayed in my gridview, and the issue is that gridview displays all records, ignoring parameters. Gridview is supposed to display record based on the Id of a user, but instead it displays all record in the table.
Id | userName | email | Name | Recipient | DateCreated | |
1 |
timJohn02 |
tim0001@mail.com |
Tim John |
Victor Jones |
June 8, 2023 |
|
2 |
MrkH247 |
henrymark@mail.com |
Mark Henry |
Benson Gabby |
Nov 15, 2023 |
|
3 |
MrkH247 |
henrymark@mail.com |
Mark Henry |
King Wills |
Nov 23, 2023 |
|
4 |
RichPete |
peter007@mail.com |
Peter Richard |
Noni Eric |
Nov 30, 2023 |
|
Id - int IDENTITY (1, 1)
email - nvarchar(MAX)
userName - nvarchar(MAX)
Name - nvarchar(MAX)
Recipient - nvarchar(MAX)
DateCreated - datetime
I tried to display record in gridview based on the Name, but it is all the records that display.
For example, If Name (Mark Henry) login, the gridview is suppose to show only records associated with (Mark Henry), which in my case Mark Henry has only 2 record in the table. However, in the gridview pager it shows that that Mark Henry has 2 records, but the gridview displays all record even the ones that Mark Henry did not create. please how do I correct this?
HTML
<asp:GridView ID="GridView1" runat="server" GridLines="None" DataKeyNames="Name"
AllowPaging="true" AutoGenerateColumns="false" OnPageIndexChanging="OnPageIndexChanging"
CssClass="table" Width="100%">
<Columns>
<asp:BoundField DataField="email" HeaderText="Email" HeaderStyle-Font-Bold="false" />
<asp:BoundField DataField="Recipient" HeaderText="Receiver" HeaderStyle-Font-Bold="false" />
<asp:BoundField DataField="DateCreated" HeaderText="Date created" HeaderStyle-Font-Bold="false" />
</Columns>
</asp:GridView>
<div style="float: right; font-size: 10pt; margin-right: 1%;">
Page
<asp:Label ID="lblPageIndex" runat="server" Text="Label" />
of
<asp:Label ID="lblTotalPage" runat="server" />
(<asp:Label ID="lblTotal" runat="server" />
Records)
<div class="dvPager">
<asp:Repeater ID="rptPager" runat="server">
<ItemTemplate>
<asp:LinkButton ID="lnkPage" runat="server" Text='<%#Eval("Text") %>' CommandArgument='<%# Eval("Value") %>'
CssClass='<%# Convert.ToBoolean(Eval("Enabled")) ? "page_enabled" : "page_disabled" %>'
OnClick="Change_Page" OnClientClick='<%# !Convert.ToBoolean(Eval("Enabled")) ? "return false;" : "" %>'></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
</div>
</div>
STORED PROCEDURE
CREATE PROCEDURE [dbo].[MyTableData]
@Name NVARCHAR(MAX)
,@PageIndex INT
,@PageSize INT
,@RecordCount INT OUT
AS
BEGIN
SELECT ROW_NUMBER() OVER(ORDER BY Name) RowNumber
,Id
,email
,Recipient
,DateCreated
INTO #Temp
FROM myTable
WHERE Name = @Name
SELECT @RecordCount = COUNT(*) FROM #Temp
SELECT * FROM #Temp
WHERE (RowNumber BETWEEN ((@PageIndex-1) * @PageSize) + 1 AND (@PageIndex * @PageSize)) OR @PageIndex = - 1
DROP TABLE #Temp
END
RETURN 0
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
ShowMyData();
this.GetTableData(1);
}
}
public void ShowMyData()
{
try
{
if (Session["userName"] != null)
{
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlCommand cmd = new SqlCommand("SELECT * FROM myTable WHERE userName = @userName", con))
{
cmd.Parameters.AddWithValue("@userName", Session["userName"]);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
labelName.Text = dr["Name"].ToString();
labelMail.Text = dr["email].ToString();
}
con.Close();
}
}
}
else
{
Response.Redirect("Login.aspx");
}
}
catch (SqlException ex)
{
string msg = "Error:";
msg += ex.Message;
throw new Exception(msg);
}
}
private void GetTableData(int pageIndex)
{
try
{
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlCommand cmd = new SqlCommand("MyTableData", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name", labelName.Text.Trim());
cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
cmd.Parameters.AddWithValue("@PageSize", PageSize);
cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4);
cmd.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
con.Open();
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
using (SqlDataReader dr = cmd.ExecuteReader())
if (dr.HasRows)
{
Template.Visible = true;
GridView1.DataSource = dt;
GridView1.DataBind();
}
else
{
Template.Visible = false;
}
con.Close();
int recordCount = Convert.ToInt32(cmd.Parameters["@RecordCount"].Value);
this.PopulatePager(recordCount, pageIndex);
}
}
con.Close();
}
}
}
catch (SqlException ex)
{
string msg = "Error:";
msg += ex.Message;
throw new Exception(msg);
}
}
protected void Change_Page(object sender, EventArgs e)
{
int pageIndex = int.Parse((sender as LinkButton).CommandArgument);
this.GetIDCardDesign(pageIndex);
}
private void PopulatePager(int recordCount, int currentPage)
{
double dblPageCount = (double)((decimal)recordCount / (decimal)PageSize);
int pageCount = (int)Math.Ceiling(dblPageCount);
List<ListItem> pages = new List<ListItem>();
if (pageCount > 0)
{
if (currentPage != 1)
{
pages.Add(new ListItem("Prev", (currentPage - 1).ToString()));
}
if (pageCount < 4)
{
for (int i = 1; i <= pageCount; i++)
{
pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
}
}
else if (currentPage < 4)
{
for (int i = 1; i <= 4; i++)
{
pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
}
pages.Add(new ListItem("...", (currentPage).ToString(), false));
}
else if (currentPage > pageCount - 4)
{
pages.Add(new ListItem("...", (currentPage).ToString(), false));
for (int i = currentPage - 1; i <= pageCount; i++)
{
pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
}
}
else
{
pages.Add(new ListItem("...", (currentPage).ToString(), false));
for (int i = currentPage - 2; i <= currentPage + 2; i++)
{
pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
}
pages.Add(new ListItem("...", (currentPage).ToString(), false));
}
if (currentPage != pageCount)
{
pages.Add(new ListItem("Next", (currentPage + 1).ToString()));
}
}
rptPager.DataSource = pages;
rptPager.DataBind();
lblPageIndex.Text = currentPage.ToString();
lblTotalPage.Text = ((recordCount / PageSize) + ((recordCount % PageSize) > 0 ? 1 : 0)).ToString();
lblTotal.Text = recordCount.ToString();
}
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
this.GetTableData(1);
}