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
SamCashAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.