• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 105
  • Last Modified:

Resource usage and referencing DataTable

This is my first attempt at Object Oriented Programming.  I like the potential benefits.  Although I have a couple of questions, any pointers on proper coding would be appreciated.  I do not think this is OOP? but my lack of fully understanding "scope" I have not been able to use my _OutTable, I have been running around for a couple of days from one won't build or not referenced to another.  This works, (I get my result) but I think it is hacked together.  Do I have an issue with all these extra Tables?

Thanks Much
Sam
MyFirstOOP.docx
0
SamCash
Asked:
SamCash
  • 4
  • 4
1 Solution
 
it_saigeDeveloperCommented:
This would be one way that it can be refactored:
public class MultiColDll
{
	public DataTable InTable { get; set; }

	public MultiColDll(DataTable table)
	{
		InTable = table;
	}

	public DataTable Process()
	{
		var result = new DataTable();
		result.Columns.Add("Value", typeof(string));
		result.Columns.Add("Text", typeof(string));

		int i = 0;
		foreach (DataRow dr in InTable.Rows)
		{
			var dra = result.NewRow(); //dra DataRowAdd
			((DataRow)dra)["Value"] = InTable.Rows[i][0].ToString();
			((DataRow)dra)["Text"] = InTable.Rows[i][0].ToString();
			((DataRow)dra)["Text"] = InTable.Rows[i][0].ToString().PadLeft(6, (' ')).Replace(" ", " "); //.Replace(" ", "&nbsp");
			((DataRow)dra)["Text"] += " " + InTable.Rows[i][1].ToString();
			((DataRow)dra)["Text"] += " " + InTable.Rows[i][2].ToString();
			((DataRow)dra)["Text"] += " " + InTable.Rows[i][3].ToString();
			((DataRow)dra)["Text"] += " " + InTable.Rows[i][4].ToString();               

			result.Rows.Add(((DataRow)dra));
			i += 1;
		}
		//this._OutTable should be used 	
		return result;
	}
}

public partial class Concantonate : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		string RequestingPage = "Lucid: " + Request.PhysicalApplicationPath + Request.CurrentExecutionFilePath;
		if (!IsPostBack)
		{
			//Populate input table
			gvInput.DataSource = BLogic.GetRosterByTeam("14", RequestingPage);
			gvInput.DataBind();
		}
	}

	DataSet In = new DataSet();
	In = BLogic.GetRosterByTeam("14", RequestingPage);
	DataTable InTableCB = In.Tables[0]; //get table for conversion

	MultiColDll Dev = new MultiColDll(InTableCB);
	gvResult.DataSource = Dev.Process();
	gvResult.DataBind();
}

Open in new window

-saige-
0
 
SamCashAuthor Commented:
it_saige,

Thanks much.  I think I am getting it!

Regards
Sam
0
 
it_saigeDeveloperCommented:
Not a problem.  If you have any other questions please do not be shy about asking.  ;)

-saige-
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
SamCashAuthor Commented:
it_saige,

Thanks, OK.

I got frustrated trying to use the "dr".  So instead I finally set up my own "i" counter? (same code sample).

foreach(DataRow dr in table.Rows)
{
    dra["Text"] = table.Rows[dr][1].ToString;
}

Why isnt "dr" an int counter I can use.  Do I have to [dr.value] or something?

Thanks again
Sam
0
 
it_saigeDeveloperCommented:
Because dr *is* the DataRow and not the index based on the count/length - 1.  Since dr is it's own object, it knows nothing about the structure (Collection, DataTable, Array, etc) that it is a part of.  As such DataRow does not contain an index property or field.

Knowing that you could have simply used:
foreach (DataRow dr in InTable.Rows)
{
	var dra = result.NewRow();
	dra["Value"] = dr[0].ToString();
	dra["Text"] = dr[0].ToString().PadLeft(6, (' ')).Replace(" ", " "); //.Replace(" ", "&nbsp");
	dra["Text"] += " " + dr[1].ToString();
	dra["Text"] += " " + dr[2].ToString();
	dra["Text"] += " " + dr[3].ToString();
	dra["Text"] += " " + dr[4].ToString();

	result.Rows.Add(((DataRow)dra));
}

Open in new window

-saige-
0
 
SamCashAuthor Commented:
Saige,

How simple.  Most important *is* the DataRow, ie Object Orientated Programming, that simple concept will help me in a lot of places I have struggled.

Thanks Again
Sam
0
 
SamCashAuthor Commented:
Saige,

I have attached the result from your coaching.  Works great.  A DropDownLists with formatted columns.  I can see how more formatting options could be added for future expansion.  For now I am just happy to get this done.  

I should be happy (I am) but now my boss wants a one line call for the web developers, without instantiating the object? They are used to this?

ddlDropDownList.DataSource = ddlStd.Process("GetddlDivisionsProc", RequestingPage);
ddlDropDownList.DataSource = ddlStd.Process("The Desired Stored Procedure", RequestingPage);

I can see how to do this with a function but not with objects.   Can this be done with object?

Thanks for your mentoring
Sam
0
 
it_saigeDeveloperCommented:
It is possible, then, you just need to make this into an extension method.  That would be written something like this:
public static class Extensions
{
	public static DataTable Process(this DataTable table)
	{
		var result = new DataTable();
		result.Columns.Add("Value", typeof(string));
		result.Columns.Add("Text", typeof(string));

		foreach (DataRow dr in table.Rows)
		{
			var dra = result.NewRow(); //dra DataRowAdd
			dra["Value"] = dr[0].ToString();
			dra["Text"] = dr[0].ToString().PadLeft(6, (' ')).Replace(" ", " "); //.Replace(" ", "&nbsp");
			dra["Text"] += " " + dr[1].ToString();
			dra["Text"] += " " + dr[2].ToString();
			dra["Text"] += " " + dr[3].ToString();
			dra["Text"] += " " + dr[4].ToString();               

			result.Rows.Add(((DataRow)dra));
		}
		return result;
	}
}

public partial class Concantonate : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		string RequestingPage = "Lucid: " + Request.PhysicalApplicationPath + Request.CurrentExecutionFilePath;
		if (!IsPostBack)
		{
			//Populate input table
			gvInput.DataSource = BLogic.GetRosterByTeam("14", RequestingPage);
			gvInput.DataBind();
		}
	}

	DataSet In = new DataSet();
	In = BLogic.GetRosterByTeam("14", RequestingPage);
	DataTable InTableCB = In.Tables[0]; //get table for conversion

	gvResult.DataSource = InTableCB.Process();
	gvResult.DataBind();
}

Open in new window

-saige-
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now