Click to See Complete Forum and Search --> : Question about C# syntax in a .cs page


Nostromo77
12-22-2008, 01:16 AM
I would like to make a change to an existing ASP.NET 2.0 "Product Details" page so that certain products are flagged with a warning label. The website's product details page already uses such boolean-triggered flags for other product information, so I have copied the existing format to add a path for this additional boolean field from the .cs page back through the DAL, the stored procedure, and the database's products table.

I have the new flag's asp:Image tag in place on the .aspx page with Visible="False", but I am not sure where to place the code on the .cs page so as not to create a compile or runtime error.

Here is a cut-down version of the Product Detail page's .cs file showing what I hope are the relevant parts; a copy of the full .cs page is here (http://www.mediafire.com/?sharekey=bbf1fb9579f300a5d2db6fb9a8902bda):
namespace MySite
{
public partial class productdetail : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Image Image1;
//[a series of WebControl declarations here]
protected System.Web.UI.WebControls.Image imgNew;
//["imgNew" is one of the existing boolean flags]

public void BindTheData()
{
.... code ....

if (!errorFlg)
{
if (dataset.Tables[0].Rows.Count > 0)
{
DataRow tr = dataset.Tables[0].Rows[0];
imgProduct.ImageUrl = "images/products/" +tr["IMAGEFILE_NM"].ToString();
litName.Text = "<h3 id='detName'>" + tr["NAME"].ToString() +"</h3>";
//Create the grades label
string from_grade = tr["FROM_GRADE"].ToString();
string to_grade = tr["TO_GRADE"].ToString();
if (from_grade.Length > 0 && to_grade.Length > 0)
{
lbGrades.Text = "Grades "+ from_grade + " - " + to_grade;
}
if (from_grade == to_grade)
{
lbGrades.Text = "Grade "+ from_grade;
}
if (from_grade.Length > 0 && to_grade.Length == 0)
{
lbGrades.Text = "Grades "+ from_grade + "+";
}
if (from_grade.Length == 0 && to_grade.Length == 0)
{
lbGrades.Text = "";
}

lbContent.Text = tr["NSES_NM"].ToString();
lbDescription.Text = tr["DESCRIPTION"].ToString();


//Remove these icons from the ProductLine1/ProductLine11 products

string topic_id = tr["TOPIC_CD"].ToString();
if (topic_id != "1" || topic_id != "11")
{
//Format the image icons representing award, resource

string awardFlg = tr["award_flg"].ToString();
string ResourceFlg = tr["resource_flg"].ToString();
if (awardFlg == "1")
{
imgAward.Visible = true;
}
if (ResourceFlg == "1")
{
imgResource.Visible = true;
}

}
.....code.....
}
I want the flags to apply to all products, not just the "if (topic_id != "1" || topic_id != "11"" ones mentioned above. I have tried to insert a copy of the code for one of the preexisting flags:
string MyNewFlg = tr["my_new_flg"].ToString();
if (MyNewFlg == "1")
{
imgMyNewFlag.Visible = true;
}
If I place the new flag code either inside or outside of the topic_id exclusion the solution builds OK, I get the following runtime error:
Column 'my_new_flg' does not belong to table Table...
Exception Details: System.ArgumentException: Column 'my_new_flg' does not belong to table Table.
Is the code I am imitating creating some sort of dynamic table, and that is causing the screwup? Should I be using something like

if (productTable.MyNewFlagColumn > 0)
{
MyNewFlag.Visible = true;
}

Thanks for any advice.