Displaying records

F

Fronky

Hope someone can help.
I am still learning, so no laughing please.

I am displaying records from a database using Response.Write(""); instead of
the usual datagrid method.
I am doing it this way to achieve the result I require.

However. Since I have added a button to the screen, and have had to include
the "object sender, System.EventArgs e", the Response.Write code has been
appearing within the Documents_Table(null,null); tag on the aspx screen, and
also at the top of the screen.

Why is the code appearing twice on the screen!!!!!?????

Can I put the html code into an asp:tag?? I've tried an asp:label, but this
will only display 1 record at a time.

HOPE YOU CAN HELP!!


MY CODE:

ASPX FRONTEND TAGS

<% Documents_Table(null,null); %>

<% Document_Cat(); %>

<asp:button Text="Search" ID="BTNDownloadSearch" OnClick="Documents_Table"
runat="server"/>


C# BACKEND CODE

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Security;
using System.Data.Odbc;
using System.Net;

namespace NCCIS_WEB
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class Documents : System.Web.UI.Page
{
//Variables
string DocImg;
string ImgAlt;
string todayDate;
string CatCode = "NoRecord";
string CatDesc;
public Label CatDownload_Nav;
string SQLQuery;
string FileCatRef;
public Label CitrixAccess;
string UserOrg;
string UserPerm;
string UserAccessLevel;
string TempAccessLevel;
string SQLKeyword;
string SQLPartnership;
string SQLContent;
//string DocumentDownload;

//public DataList temp;

public TextBox Keyword;
public TextBox Partnership;
public TextBox Content;
public Button BTNDocumentsSearch;

GlobalClass GlobalFuncs = new GlobalClass();

private void Page_Load(object sender, System.EventArgs e)
{
//Initial Page Check - Security
GlobalFuncs.InitPageCheck();
CitrixAccess.Text = GlobalFuncs.CitrixLabel;

if (!Page.IsPostBack)
{
Documents_Table(null,null);
}

}

public void Document_Cat() {

OdbcConnection Odbccon1 = new
OdbcConnection(GlobalFuncs.GlobalODBCConnString);
Odbccon1.Open();

OdbcDataAdapter adpt1 = new OdbcDataAdapter("select code,description from
documentcat", Odbccon1);

DataSet dset1 = new DataSet();
DataTable dt1 = dset1.Tables.Add("DocumentCat");

adpt1.Fill(dset1, "DocumentCat");

foreach (DataRow dr in dt1.Rows) {

Response.Write("<tr>");
Response.Write("<td><li class='body_text_white'><a
href='documents.aspx?FileCatRef="+dr["code"]+"'
class='body_text_white'>"+dr["description"]+"</a></li></td>");
Response.Write("</tr>");

}

Response.Write("<tr>");
Response.Write("<td><li class='body_text_white'><a
href='documents.aspx?FileCatRef=0' class='body_text_white'>Display All
Categories</a></li></td>");
Response.Write("</tr>");

}

public void Documents_Table(object sender, System.EventArgs e) {

string StrKeyword = Keyword.Text;
string StrPartnership = Partnership.Text;
string StrContent = Content.Text;

FileCatRef = Request.Params["FileCatRef"];
HttpCookie cookie = Context.Request.Cookies["NCCIS_Cookie"];

string cookieCXUser = cookie["CXUser"].ToString().Trim();
string cookieUserName = cookie["UserName"].ToString().Trim();
string cookieAccessLevel = cookie["AccessLevel"].ToString().Trim();

OdbcConnection Odbccon = new
OdbcConnection(GlobalFuncs.GlobalODBCConnString);
Odbccon.Open();

OdbcCommand cmd3 = new OdbcCommand("select UserCode,CXPartnership from
CX_USERS where UserCode = '"+cookieCXUser+"'", Odbccon);
OdbcDataReader rdr3 = cmd3.ExecuteReader();

while (rdr3.Read()) {
UserPerm = rdr3.GetString(0);
UserOrg = rdr3.GetString(1);
}

rdr3.Close();

OdbcCommand cmd5 = new OdbcCommand("select CanSeeLevel from SECURITY
where Level = '"+cookieAccessLevel+"'", Odbccon);
OdbcDataReader rdr5 = cmd5.ExecuteReader();

while (rdr5.Read()) {
TempAccessLevel = rdr5.GetString(0);
}

rdr5.Close();

//LEVEL FORMATING
UserAccessLevel = TempAccessLevel.Replace(" ","").Replace(",","','");

todayDate =
DateTime.Now.ToLocalTime().ToString("yyyy/MM/dd").Trim();

if (FileCatRef == "0"){
SQLQuery = "select
refno,doctype,added,addedby,title,description,orgfilename,\"file
cat\",securityorg,securityuser,securitylevel from documents WHERE
contenttype = 'DOCUMENT' "+SQLKeyword+" "+SQLPartnership+" "+SQLContent+"
AND (ExpiryDate >= '"+todayDate+"' OR ExpiryDate is NULL) AND (SecurityLevel
IN ('"+UserAccessLevel+"')) AND (SecurityOrg like '%"+UserOrg+"%' OR
SecurityOrg IS NULL) AND (SecurityUser like '%"+UserPerm+"%' OR SecurityUser
IS NULL) order by \"file cat\" asc, added desc";
} else {
SQLQuery = "select
refno,doctype,added,addedby,title,description,orgfilename,\"file
cat\",securityorg,securityuser,securitylevel from documents WHERE
contenttype = 'DOCUMENT' "+SQLKeyword+" "+SQLPartnership+" "+SQLContent+"
AND (ExpiryDate >= '"+todayDate+"' OR ExpiryDate is NULL) AND (SecurityLevel
IN ('"+UserAccessLevel+"')) AND (SecurityOrg like '%"+UserOrg+"%' OR
SecurityOrg IS NULL) AND (SecurityUser like '%"+UserPerm+"%' OR SecurityUser
IS NULL) AND \"file cat\" = "+FileCatRef+" order by added desc";
}

if (StrKeyword == "") {
SQLKeyword = "";
} else {
SQLKeyword = "AND title LIKE '%"+StrKeyword+"%'";
}

if (StrContent == "") {
SQLContent = "";
} else {
SQLContent = "AND description LIKE '%"+StrContent+"%'";
}

if (StrPartnership == "") {
SQLPartnership = "";
} else {
SQLPartnership = "AND description LIKE '%"+StrPartnership+"%'";
}

OdbcDataAdapter adpt = new OdbcDataAdapter(SQLQuery, Odbccon);

DataSet dset = new DataSet();
DataTable dt = dset.Tables.Add("Documents");

adpt.Fill(dset, "Documents");

foreach (DataRow dr in dt.Rows) {

OdbcCommand cmd = new OdbcCommand("select * from DOCUMENTCAT where Code =
'"+dr["file cat"]+"'", Odbccon);
OdbcDataReader rdr = cmd.ExecuteReader();

while (rdr.Read()) {
CatDesc = rdr.GetString(1).Trim();
}

rdr.Close();

OdbcCommand cmd1 = new OdbcCommand("select * from DOCTYPE where
Code = '"+dr["doctype"]+"'", Odbccon);
OdbcDataReader rdr4 = cmd1.ExecuteReader();

while (rdr4.Read()) {
DocImg = rdr4.GetString(2).Trim();
ImgAlt = rdr4.GetString(1).Trim();
}

if (CatCode == dr["file cat"].ToString()){

Response.Write("<tr class='body_text'>");
Response.Write("<td align='right' valign='top' width='100%'
colspan='3'>");
Response.Write("<table width='98%' border='0' cellpadding='0'
cellspacing='0'>");
Response.Write("<tr class='body_text'>");
Response.Write("<td valign='top'><b>"+ dr["Title"] +"</b></td>");
Response.Write("<td valign='top' align='right'><b>Posted:</b>&nbsp;"+
dr["Added"].ToString().Remove(10, 9) +"</td>");
Response.Write("<td align='right' valign='top'><a
href='docdownload.aspx?docref="+dr["refno"]+"' target='doc'
onclick='tracker.location.href=\"tracker.aspx?docref="+dr["refno"]+"\"'><img
src='../images/"+DocImg+"' border='0' alt='"+ImgAlt+" -
"+dr["orgfilename"]+"'></a></td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td colspan='3' class='body_text' valign='top'>"+
dr["Description"] +"</td>");
Response.Write("</tr>");
Response.Write("</table>");
Response.Write("</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td colspan='3'><hr></td>");
Response.Write("</tr>");

} else {

Response.Write("<tr class='body_text'>");
Response.Write("<td valign='top' colspan='3' class='CatTitle'><b>::
"+CatDesc+" ::</b></td>");
Response.Write("</tr>");
Response.Write("<tr class='body_text'>");
Response.Write("<td align='right' valign='top' width='100%'
colspan='3'>");
Response.Write("<table width='98%' border='0' cellpadding='0'
cellspacing='0'>");
Response.Write("<tr class='body_text'>");
Response.Write("<td valign='top'><b>"+ dr["Title"] +"</b></td>");
Response.Write("<td valign='top' align='right'><b>Posted:</b>&nbsp;"+
dr["Added"].ToString().Remove(10, 9) +"</td>");
Response.Write("<td align='right' valign='top'><a
href='docdownload.aspx?docref="+dr["refno"]+"' target='doc'
onclick='tracker.location.href=\"tracker.aspx?docref="+dr["refno"]+"\"'><img
src='../images/"+DocImg+"' border='0' alt='"+ImgAlt+" -
"+dr["orgfilename"]+"'></a></td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td colspan='3' class='body_text' valign='top'>"+
dr["Description"] +"</td>");
Response.Write("</tr>");
Response.Write("</table>");
Response.Write("</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td colspan='3'><hr></td>");
Response.Write("</tr>");

CatCode = dr["file cat"].ToString();

}

rdr4.Close();
}
Odbccon.Close();
}
}
 
R

Red Forks

Fronky said:
Hope someone can help.
I am still learning, so no laughing please.

I am displaying records from a database using Response.Write(""); instead
of the usual datagrid method.
I am doing it this way to achieve the result I require.

However. Since I have added a button to the screen, and have had to
include the "object sender, System.EventArgs e", the Response.Write code
has been appearing within the Documents_Table(null,null); tag on the aspx
screen, and also at the top of the screen.

Why is the code appearing twice on the screen!!!!!?????

Can I put the html code into an asp:tag?? I've tried an asp:label, but
this will only display 1 record at a time.

HOPE YOU CAN HELP!!


MY CODE:

ASPX FRONTEND TAGS

<% Documents_Table(null,null); %>

<% Document_Cat(); %>

<asp:button Text="Search" ID="BTNDownloadSearch" OnClick="Documents_Table"
runat="server"/>


C# BACKEND CODE

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Security;
using System.Data.Odbc;
using System.Net;

namespace NCCIS_WEB
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class Documents : System.Web.UI.Page
{
//Variables
string DocImg;
string ImgAlt;
string todayDate;
string CatCode = "NoRecord";
string CatDesc;
public Label CatDownload_Nav;
string SQLQuery;
string FileCatRef;
public Label CitrixAccess;
string UserOrg;
string UserPerm;
string UserAccessLevel;
string TempAccessLevel;
string SQLKeyword;
string SQLPartnership;
string SQLContent;
//string DocumentDownload;

//public DataList temp;

public TextBox Keyword;
public TextBox Partnership;
public TextBox Content;
public Button BTNDocumentsSearch;

GlobalClass GlobalFuncs = new GlobalClass();

private void Page_Load(object sender, System.EventArgs e)
{
//Initial Page Check - Security
GlobalFuncs.InitPageCheck();
CitrixAccess.Text = GlobalFuncs.CitrixLabel;

if (!Page.IsPostBack)
{
Documents_Table(null,null);
}

}

public void Document_Cat() {

OdbcConnection Odbccon1 = new
OdbcConnection(GlobalFuncs.GlobalODBCConnString);
Odbccon1.Open();

OdbcDataAdapter adpt1 = new OdbcDataAdapter("select code,description
from
documentcat", Odbccon1);

DataSet dset1 = new DataSet();
DataTable dt1 = dset1.Tables.Add("DocumentCat");

adpt1.Fill(dset1, "DocumentCat");

foreach (DataRow dr in dt1.Rows) {

Response.Write("<tr>");
Response.Write("<td><li class='body_text_white'><a
href='documents.aspx?FileCatRef="+dr["code"]+"'
class='body_text_white'>"+dr["description"]+"</a></li></td>");
Response.Write("</tr>");

}

Response.Write("<tr>");
Response.Write("<td><li class='body_text_white'><a
href='documents.aspx?FileCatRef=0' class='body_text_white'>Display All
Categories</a></li></td>");
Response.Write("</tr>");

}

public void Documents_Table(object sender, System.EventArgs e) {

string StrKeyword = Keyword.Text;
string StrPartnership = Partnership.Text;
string StrContent = Content.Text;

FileCatRef = Request.Params["FileCatRef"];
HttpCookie cookie = Context.Request.Cookies["NCCIS_Cookie"];

string cookieCXUser = cookie["CXUser"].ToString().Trim();
string cookieUserName = cookie["UserName"].ToString().Trim();
string cookieAccessLevel = cookie["AccessLevel"].ToString().Trim();

OdbcConnection Odbccon = new
OdbcConnection(GlobalFuncs.GlobalODBCConnString);
Odbccon.Open();

OdbcCommand cmd3 = new OdbcCommand("select UserCode,CXPartnership from
CX_USERS where UserCode = '"+cookieCXUser+"'", Odbccon);
OdbcDataReader rdr3 = cmd3.ExecuteReader();

while (rdr3.Read()) {
UserPerm = rdr3.GetString(0);
UserOrg = rdr3.GetString(1);
}

rdr3.Close();

OdbcCommand cmd5 = new OdbcCommand("select CanSeeLevel from SECURITY
where Level = '"+cookieAccessLevel+"'", Odbccon);
OdbcDataReader rdr5 = cmd5.ExecuteReader();

while (rdr5.Read()) {
TempAccessLevel = rdr5.GetString(0);
}

rdr5.Close();

//LEVEL FORMATING
UserAccessLevel = TempAccessLevel.Replace(" ","").Replace(",","','");

todayDate =
DateTime.Now.ToLocalTime().ToString("yyyy/MM/dd").Trim();

if (FileCatRef == "0"){
SQLQuery = "select
refno,doctype,added,addedby,title,description,orgfilename,\"file
cat\",securityorg,securityuser,securitylevel from documents WHERE
contenttype = 'DOCUMENT' "+SQLKeyword+" "+SQLPartnership+" "+SQLContent+"
AND (ExpiryDate >= '"+todayDate+"' OR ExpiryDate is NULL) AND
(SecurityLevel IN ('"+UserAccessLevel+"')) AND (SecurityOrg like
'%"+UserOrg+"%' OR SecurityOrg IS NULL) AND (SecurityUser like
'%"+UserPerm+"%' OR SecurityUser IS NULL) order by \"file cat\" asc, added
desc";
} else {
SQLQuery = "select
refno,doctype,added,addedby,title,description,orgfilename,\"file
cat\",securityorg,securityuser,securitylevel from documents WHERE
contenttype = 'DOCUMENT' "+SQLKeyword+" "+SQLPartnership+" "+SQLContent+"
AND (ExpiryDate >= '"+todayDate+"' OR ExpiryDate is NULL) AND
(SecurityLevel IN ('"+UserAccessLevel+"')) AND (SecurityOrg like
'%"+UserOrg+"%' OR SecurityOrg IS NULL) AND (SecurityUser like
'%"+UserPerm+"%' OR SecurityUser IS NULL) AND \"file cat\" =
"+FileCatRef+" order by added desc";
}

if (StrKeyword == "") {
SQLKeyword = "";
} else {
SQLKeyword = "AND title LIKE '%"+StrKeyword+"%'";
}

if (StrContent == "") {
SQLContent = "";
} else {
SQLContent = "AND description LIKE '%"+StrContent+"%'";
}

if (StrPartnership == "") {
SQLPartnership = "";
} else {
SQLPartnership = "AND description LIKE '%"+StrPartnership+"%'";
}

OdbcDataAdapter adpt = new OdbcDataAdapter(SQLQuery, Odbccon);

DataSet dset = new DataSet();
DataTable dt = dset.Tables.Add("Documents");

adpt.Fill(dset, "Documents");

foreach (DataRow dr in dt.Rows) {

OdbcCommand cmd = new OdbcCommand("select * from DOCUMENTCAT where Code
=
'"+dr["file cat"]+"'", Odbccon);
OdbcDataReader rdr = cmd.ExecuteReader();

while (rdr.Read()) {
CatDesc = rdr.GetString(1).Trim();
}

rdr.Close();

OdbcCommand cmd1 = new OdbcCommand("select * from DOCTYPE
where
Code = '"+dr["doctype"]+"'", Odbccon);
OdbcDataReader rdr4 = cmd1.ExecuteReader();

while (rdr4.Read()) {
DocImg = rdr4.GetString(2).Trim();
ImgAlt = rdr4.GetString(1).Trim();
}

if (CatCode == dr["file cat"].ToString()){

Response.Write("<tr class='body_text'>");
Response.Write("<td align='right' valign='top' width='100%'
colspan='3'>");
Response.Write("<table width='98%' border='0' cellpadding='0'
cellspacing='0'>");
Response.Write("<tr class='body_text'>");
Response.Write("<td valign='top'><b>"+ dr["Title"] +"</b></td>");
Response.Write("<td valign='top' align='right'><b>Posted:</b>&nbsp;"+
dr["Added"].ToString().Remove(10, 9) +"</td>");
Response.Write("<td align='right' valign='top'><a
href='docdownload.aspx?docref="+dr["refno"]+"' target='doc'
onclick='tracker.location.href=\"tracker.aspx?docref="+dr["refno"]+"\"'><img
src='../images/"+DocImg+"' border='0' alt='"+ImgAlt+" -
"+dr["orgfilename"]+"'></a></td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td colspan='3' class='body_text' valign='top'>"+
dr["Description"] +"</td>");
Response.Write("</tr>");
Response.Write("</table>");
Response.Write("</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td colspan='3'><hr></td>");
Response.Write("</tr>");

} else {

Response.Write("<tr class='body_text'>");
Response.Write("<td valign='top' colspan='3' class='CatTitle'><b>::
"+CatDesc+" ::</b></td>");
Response.Write("</tr>");
Response.Write("<tr class='body_text'>");
Response.Write("<td align='right' valign='top' width='100%'
colspan='3'>");
Response.Write("<table width='98%' border='0' cellpadding='0'
cellspacing='0'>");
Response.Write("<tr class='body_text'>");
Response.Write("<td valign='top'><b>"+ dr["Title"] +"</b></td>");
Response.Write("<td valign='top' align='right'><b>Posted:</b>&nbsp;"+
dr["Added"].ToString().Remove(10, 9) +"</td>");
Response.Write("<td align='right' valign='top'><a
href='docdownload.aspx?docref="+dr["refno"]+"' target='doc'
onclick='tracker.location.href=\"tracker.aspx?docref="+dr["refno"]+"\"'><img
src='../images/"+DocImg+"' border='0' alt='"+ImgAlt+" -
"+dr["orgfilename"]+"'></a></td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td colspan='3' class='body_text' valign='top'>"+
dr["Description"] +"</td>");
Response.Write("</tr>");
Response.Write("</table>");
Response.Write("</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td colspan='3'><hr></td>");
Response.Write("</tr>");

CatCode = dr["file cat"].ToString();

}

rdr4.Close();
}
Odbccon.Close();
}
}

Don't use Response.Write().
Add a LiteralControl, Write your html code in a StringBuilder(), then change
the LiteralControl.Text property, like this:
StringBuilder sb = new StringBuilder();
sb.Append("<table>");
sb.Append("<tr>");
....

When the Response.Write() was called, the Page is not starting Render the
Page, so always the content you write is on the top of the Page.
If you want your output is where you want, wrap it as an asp.net control,
and insert your control at the position you want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top