Hey,

I have a this readblog.cs file in projects App_Code folder, and at main folder I have Blog.aspx -> Blog.aspx.cs file.

And I have a Table control in Blog.aspx file, called Bloglist, and I am trying to modify it from App_Code/readblog.cs class file and I dont know how to use Using statements or something like that to get reference so I could use Bloglist at App_Code folder? . here's a snippet from both of files->


----------------------------
Down under App_Code/readblog.cs
----------------------------

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;


/// <summary>
/// Summary description for readblog
/// </summary>
/// 

public class readblog

and somewhere down here is that this.Bloglist item, and it doesnt find it (of course, cause theres not a reference to the Blog.aspx.cs file, and I dont know how to do it? I have tryed namespaces and etc, maeby im just confused cause its in the App_Code folder
----------------------------
Down under blog.aspx.cs
----------------------------

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;


public partial class Blog : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{

}
else
{
readblog rb = new readblog("blogs.xml");
}
}
}

THX! <- Noob!

When using a website project, you don't need to worry about namespaces. Anything in App_Code is automatically available in the pages in the rest of the site (for better or worse).

The code you posted should work (assuming readblog has a constructor that accepts a string parameter). Are you getting a compilation error?

When using a website project, you don't need to worry about namespaces. Anything in App_Code is automatically available in the pages in the rest of the site (for better or worse).

The code you posted should work (assuming readblog has a constructor that accepts a string parameter). Are you getting a compilation error?

Yeah it works that way, no problem in there, but other way around.

I have this code in App_Code/readblog.cs file

void RebuildBlogPage(DataSet ds)
    {
        //string prevUser = ""; //Etsitään edellinen user

        foreach (DataRow dr in ds.Tables[0].Rows)
        {            
            
            //Lisää topic
            TableRow tr = new TableRow();
            tr.Cells.Add(new TableCell());
            tr.Cells[0].CssClass = "h3";
            tr.Cells[0].Text = dr[0].ToString();
            
            this.BlogTable.Rows.Add(tr);            

            //Lisää date
            tr = new TableRow();
            tr.Cells.Add(new TableCell());
            tr.Cells[0].CssClass = "h5";
            tr.Cells[0].Text = Convert.ToString(dr[1].ToString()) + " by " +
                dr[3].ToString();
            this.BlogTable.Rows.Add(tr);          

            //Lisää BLog
            tr = new TableRow();
            tr.Cells.Add(new TableCell());
            tr.Cells[0].Width = 200;
            tr.Cells[0].CssClass = "textblog";
            tr.Cells[0].Text = dr[2].ToString();
            this.BlogTable.Rows.Add(tr);

            //lisätään separator
            tr = new TableRow();
            tr.Cells.Add(new TableCell());
            tr.Cells[0].ColumnSpan = 2;

            this.BlogTable.Rows.Add(tr);             
        }

Error ->
'readblog' does not contain a definition for 'BlogTable' and no extension method 'BlogTable' accepting a first argument of type 'readblog' could be found (are you missing a using directive or an assembly reference?)

And that BlogTable is at ~/Blog.aspx file?

I'm assuming that's some code that's been moved from an ASPX page's code behind into its own class? It sounds like BlogTable was a control on that page.

If so, what you probably need to do is have RebuildBlogPage return a Table. Something like:

Table RebuildBlogPage(DataSet ds)
    {
        //string prevUser = ""; //Etsitään edellinen user
        Table BlogTable = new Table();

        foreach (DataRow dr in ds.Tables[0].Rows)
        {            
            
            //Lisää topic
            TableRow tr = new TableRow();
            tr.Cells.Add(new TableCell());
            tr.Cells[0].CssClass = "h3";
            tr.Cells[0].Text = dr[0].ToString();
            
            BlogTable.Rows.Add(tr);            

            //Lisää date
            tr = new TableRow();
            tr.Cells.Add(new TableCell());
            tr.Cells[0].CssClass = "h5";
            tr.Cells[0].Text = Convert.ToString(dr[1].ToString()) + " by " +
                dr[3].ToString();
            BlogTable.Rows.Add(tr);          

            //Lisää BLog
            tr = new TableRow();
            tr.Cells.Add(new TableCell());
            tr.Cells[0].Width = 200;
            tr.Cells[0].CssClass = "textblog";
            tr.Cells[0].Text = dr[2].ToString();
            BlogTable.Rows.Add(tr);

            //lisätään separator
            tr = new TableRow();
            tr.Cells.Add(new TableCell());
            tr.Cells[0].ColumnSpan = 2;

            BlogTable.Rows.Add(tr);             
        }

        return BlogTable;
    }

Then, in your ASPX page, you would add the return value of that method to the controls collection of your page, placeholder, panel, or whatever you're using for layout.

Now we are getting to point. But my knowledge is not enough yet, ill past both codes here

Blog.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

    public partial class Blog : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
            }
            else
            {
                readblog rb = new readblog("blogs.xml");                
            }
        }
        protected void WriteBlog1_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("WriteBlog.aspx");
        }
    }

and App_Code/readblog.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public class readblog
{
	public readblog()
	{
		//
		// TODO: Add constructor logic here
		//                
	}
    public readblog(string xmlfilename)
    {        
            DataSet ds = ReadBlogIntoTable(xmlfilename);

            if ((bool)HttpContext.Current.Session["Changed"])
            {
                AppendBlogs(ds, xmlfilename);
                WriteXmlBlog(ds, xmlfilename);
                HttpContext.Current.Session["Changed"] = false; 
            }
            RebuildBlogPage(ds);
    }

    private DataSet ReadBlogIntoTable(string xmlfilename)
    {
        DataSet ds = new DataSet();

        string filename = HttpContext.Current.Server.MapPath(".\\App_Data\\") + xmlfilename;

        if (File.Exists(filename))
        {
            ds.ReadXml(filename); //Jos on niin luetaan tiedosto datasettiin ja palautetaan se
        }
        else
        {
                DataTable dt = new DataTable("Contents");
                ds.Tables.Add(dt);

                dt.Columns.Add("Topic", Type.GetType("System.String"));
                dt.Columns.Add("Time", Type.GetType("System.DateTime"));
                dt.Columns.Add("Content", Type.GetType("System.String"));
                dt.Columns.Add("Writer", Type.GetType("System.String"));
        }
        return ds;
    }

    void AppendBlogs(DataSet ds, string xmlfilename)
    {
        DataRow dr = ds.Tables["Contents"].NewRow();

        dr[0] = HttpContext.Current.Session["Topic"];
        dr[1] = DateTime.Now;
        dr[2] = HttpContext.Current.Session["Content"];
        dr[3] = HttpContext.Current.Session["Writer"];

        ds.Tables["Contents"].Rows.Add(dr);

        WriteXmlBlog(ds, xmlfilename);
    }

    private void WriteXmlBlog(DataSet ds, string xmlfilename)
    {
        string filename = HttpContext.Current.Server.MapPath(".\\App_Data\\") + xmlfilename;
        ds.WriteXml(filename);
    }

    Table RebuildBlogPage(DataSet ds)
    {        
        Table BTable = new Table();

        foreach (DataRow dr in ds.Tables[0].Rows)
        {                       
            TableRow tr = new TableRow();
            tr.Cells.Add(new TableCell());
            tr.Cells[0].CssClass = "h3";
            tr.Cells[0].Text = dr[0].ToString();

            BTable.Rows.Add(tr);            
            
            tr = new TableRow();
            tr.Cells.Add(new TableCell());
            tr.Cells[0].CssClass = "h5";
            tr.Cells[0].Text = Convert.ToString(dr[1].ToString()) + " by " +
                dr[3].ToString();
            BTable.Rows.Add(tr);          

            tr = new TableRow();
            tr.Cells.Add(new TableCell());
            tr.Cells[0].Width = 200;
            tr.Cells[0].CssClass = "textblog";
            tr.Cells[0].Text = dr[2].ToString();
            BTable.Rows.Add(tr);

            tr = new TableRow();
            tr.Cells.Add(new TableCell());
            tr.Cells[0].ColumnSpan = 2;

            BTable.Rows.Add(tr);
        }
        return BTable;
    }
}

Now it's mixed with that return table. Everything else is original, i tried somethings. Now I have to return that table over to Blog.cs somehow....? Thx allready, it would be nice if i could figure it out on my own, but just cant :(

Okay, with that in mind, things are going to be a little different.

Doing something like this for your readblog class would be a step in the right direction:

using System;
using System.Data;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;

public class readblog
{
  private DataSet _blog;

  public readblog(string xmlfilename)
  {
    _blog = ReadBlogIntoTable(xmlfilename);

    if ((bool)HttpContext.Current.Session["Changed"])
    {
      AppendBlogs(xmlfilename);
      WriteXmlBlog(xmlfilename);
      HttpContext.Current.Session["Changed"] = false;
    }
  }

  private DataSet ReadBlogIntoTable(string xmlfilename)
  {
    DataSet ds = new DataSet();

    string filename = HttpContext.Current.Server.MapPath(".\\App_Data\\") + xmlfilename;

    if (File.Exists(filename))
    {
      ds.ReadXml(filename); //Jos on niin luetaan tiedosto datasettiin ja palautetaan se
    }
    else
    {
      DataTable dt = new DataTable("Contents");

      dt.Columns.Add("Topic", Type.GetType("System.String"));
      dt.Columns.Add("Time", Type.GetType("System.DateTime"));
      dt.Columns.Add("Content", Type.GetType("System.String"));
      dt.Columns.Add("Writer", Type.GetType("System.String"));

      ds.Tables.Add(dt);
    }

    return ds;
  }

  void AppendBlogs(string xmlfilename)
  {
    DataRow dr = _blog.Tables["Contents"].NewRow();

    dr[0] = HttpContext.Current.Session["Topic"];
    dr[1] = DateTime.Now;
    dr[2] = HttpContext.Current.Session["Content"];
    dr[3] = HttpContext.Current.Session["Writer"];

    _blog.Tables["Contents"].Rows.Add(dr);

    WriteXmlBlog(xmlfilename);
  }

  private void WriteXmlBlog(string xmlfilename)
  {
    string filename = HttpContext.Current.Server.MapPath(".\\App_Data\\") + xmlfilename;
    _blog.WriteXml(filename);
  }

  public Table RebuildBlogPage()
  {
    Table BTable = new Table();

    foreach (DataRow dr in _blog.Tables[0].Rows)
    {
      TableRow tr = new TableRow();
      tr.Cells.Add(new TableCell());
      tr.Cells[0].CssClass = "h3";
      tr.Cells[0].Text = dr[0].ToString();

      BTable.Rows.Add(tr);

      tr = new TableRow();
      tr.Cells.Add(new TableCell());
      tr.Cells[0].CssClass = "h5";
      tr.Cells[0].Text = Convert.ToString(dr[1].ToString()) + " by " +
          dr[3].ToString();
      BTable.Rows.Add(tr);

      tr = new TableRow();
      tr.Cells.Add(new TableCell());
      tr.Cells[0].Width = 200;
      tr.Cells[0].CssClass = "textblog";
      tr.Cells[0].Text = dr[2].ToString();
      BTable.Rows.Add(tr);

      tr = new TableRow();
      tr.Cells.Add(new TableCell());
      tr.Cells[0].ColumnSpan = 2;

      BTable.Rows.Add(tr);
    }

    return BTable;
  }
}

Then, in Blog.aspx.cs, you could add that table to the page like this:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

    public partial class Blog : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
            }
            else
            {
                readblog rb = new readblog("blogs.xml");    
                Page.Controls.Add(rb.RebuildBlogTable());            
            }
        }
        protected void WriteBlog1_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("WriteBlog.aspx");
        }
    }

Before going too far, it might not hurt for you to take a look at some tutorials on object oriented design and programming. That's what's at the heart of this. Getting off to the right start with it will make things go much more smoothly.

Hey buddy, Blogtable was a table runat the server. It could not find the table, so therefore it is not referenced. Create a server controlled table on the page with the ID of "BlogTable".

That wouldn't work for what he's doing, because the readblog.cs class trying to reference BlogTable isn't in the scope of the Page (though, it looks like it probably was at one point).

Thx mates. I know and I am reading couple of books now from wrox. Thx, have to try that at home at learn whats happening in there :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.