Michelle Carthy

Michelle Carthy

  • NA
  • 52
  • 11.5k

When link is clicked in html table it shows data based on id

Jan 2 2018 12:04 PM
So I have a table created that shows the properties of the landlord that is logged in. When I click on the view link, it redirects me to a new page which then I would like to display a table of the tenants that are linked to that particular property. 
 
 
When view is for property number 1 clicked, this is what is shown below: 
  however, it is displaying the tenants of all properties, and i am unsure how to only show for that particular row. This is my code behind the second image: 
 
using System;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Tenant : System.Web.UI.Page
{
//creating an empty string
string checkTenant = String.Empty;
//constructing a string called table that will be used to create a dynamic table
StringBuilder table = new StringBuilder();
protected void Page_Load(object sender, EventArgs e)
{
checkTenant += Session["LandlordLogin"];
if (!Page.IsPostBack)
{
//Creating a connection to my database using the connection string
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["rent-dbConnectionString1"].ToString();
con.Open();
SqlCommand comm = new SqlCommand();
//preparing a query which will select all properties matching the landlord that is logged in at that moment
comm.CommandText = "select Tenant_FullName, Tenant_Email, Tenant_TelNum, Tenant_EmName, Tenant_EmNum, Tenant_WorkStatus from Tenants";
comm.Connection = con;
SqlDataReader rd = comm.ExecuteReader();
//creating a table depending on the data that is selected from the query
table.Append("<table border='2' class='table table-striped table-bordered'>");
table.Append("<tr><th>Full Name</th>");
table.Append("<th>Email</th>");
table.Append("<th>Phone Number</th>");
table.Append("<th>Work Status</th>");
table.Append("<th>Emergency Name</th>");
table.Append("<th>Emergency Contact Number</th>");
table.Append("</tr>");
if (rd.HasRows)
{
while (rd.Read())
{
table.Append("<tr>");
table.Append("<td>" + rd[0] + "</td>");
table.Append("<td>" + rd[1] + "</td>");
table.Append("<td>" + rd[2] + "</td>");
table.Append("<td>" + rd[3] + "</td>");
table.Append("<td>" + rd[4] + "</td>");
table.Append("<td>" + rd[5] + "</td>");
table.Append("</tr>");
}
}
table.Append("</table>");
PlaceHolder2.Controls.Add(new Literal { Text = table.ToString() });
rd.Close();
}
}
}
 

Answers (1)