Hello Forum,
I have a table structure like this
Id
|
Email
|
Name
|
Admin_email
|
CreatedDate
|
1
|
simons@yahoo.com
|
Simons Simons
|
simons@yahoo.com
|
|
2
|
Wills@gmail.com
|
Wiliams Terrence
|
Preston@devon.com
|
|
3
|
queensland@hotmail.com
|
Queenland Limited
|
queensland@hotmail.com
|
|
4
|
Preston@devon.com
|
Preston Associates
|
Preston@devon.com
|
|
5
|
Larry2020@gmail.com
|
Larry Howard
|
queensland@hotmail.com
|
|
6
|
Peter02@gmail.com
|
Peter Collins
|
queensland@hotmail.com
|
|
7
|
Trecords@yahoo.com
|
Terra Records Inc.
|
Trecords@yahoo.com
|
|
The above table shows that Williams Terrence and Preston Associates are linked together; Queenland Limited, Larry Howard and Peter Collins are linked together. While Simons Simons and Terra Records are two separate data.
I am trying to display data in gridview from data table “Users” using SQL in stored procedure based on user that is logged in. If Simons Simons logs in and navigates to the gridview, then all data related to him should display in the gridview. If Preston Associates logs in, then all related data to Preston should be displayed and other data that is not related to Preston will not show. I want to call the column name “Admin_email” because there is a relation between users in the column “Admin_email.
I tried this SQL but I am finding difficult to make it work. Please is this the correct statement?
Here is my SQL (stored procedure)
CREATE PROCEDURE [dbo].[Dataregister]
@PageIndex INT
,@PageSize INT
,@RecordCount INT OUT
AS
BEGIN
DECLARE @Admin_email VARCHAR(50)
SELECT ROW_NUMBER() OVER(ORDER BY email) RowNumber
,email
,Name
,Admin_email
INTO #Temp
FROM Users
WHERE Admin_email = @Admin_email
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
C#
public partial class InvoiceRecords : System.Web.UI.Page
{
SqlCommand cmd = new SqlCommand();
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True");
private int PageSize = 5;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.GetCustomersPageWise(1);
}
}
private void GetCustomersPageWise(int pageIndex)
{
using (SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand("Dataregister", con))
{
cmd.CommandType = CommandType.StoredProcedure;
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);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
int recordCount = Convert.ToInt32(cmd.Parameters["@RecordCount"].Value);
this.PopulatePager(recordCount, pageIndex);
}
}
}
}
}
}