I am new to C# and ASP.net. I am a ColdFusion Programmer but I am catching on to ASP.net.
I will give details then ask my question….
I have been able to get it where I call from the Code Behind a Service Layer Class File (or Bussiness Logic Layer) then in that one call a Data Access Layer Class file. I have been sending back a DataSet from the Data Access Layer then in the Code Behind moving the code to a table so I can read it row by row.
I was looking for a way to get the Boolean values when I am on the Edit screen to set the check boxes to checked when I found this post.
C# Assigning a value to DataRow["haswhatnots"] = hasWhatnots is awful slow
I see a mention that they should…
Alternatively – use a class model instead of a DataTable that sounds great if it is what I think it is.
What is the Best way to do a CRUD screens where you use the Class file to pull back the data and what format should the Data be in?
I am used to in ColdFusion data coming back in a QuerySet and then we can loop through it. We can convert he query to Array of Structures or and Array of Obects, XML or JSon but I am still running into issues understanding HOW to get the data out of the DataSet when I get it back. I get String Data fine right now but maybe there is a very simple example with maybe one page…
I want to figure out how to get the Boolean values right now is my current issue.
Thanks for any help,
Nathan
P.S. if there is a better more professional way to do this let me know. I want to keep using a (Presentation/CodeBehind/Service/Data Access) type of layers.
Protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
DataTable dt = new DataTable();
DataRow dr;
PageService myPage = new PageService();
DataSet ds = myPage.getPages();
int rowCount = ds.Tables[0].Rows.Count;
ds.Columns.Add(new DataColumn("PageID", typeof(Int32)));
ds.Columns.Add(new DataColumn("Name", typeof(String)));
ds.Columns.Add(new DataColumn("PageHTMLContent", typeof(String)));
ds.Columns.Add(new DataColumn("NavigationText", typeof(String)));
ds.Columns.Add(new DataColumn("TopMenu", typeof(Boolean)));
ds.Columns.Add(new DataColumn("SubMenu", typeof(Boolean)));
ds.Columns.Add(new DataColumn("DisplayOrder", typeof(Int32)));
ds.Columns.Add(new DataColumn("Active", typeof(Boolean)));
for (int i = 0; i < rowCount; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = i.ToString;
dr[2] = i.ToString;
dr[3] = i.ToString;
dr[4] = i; // Not sure what to do here for the Boolean Value
dr[5] = i; // Not sure what to do here for the Boolean Value
dr[6] = i;
dr[7] = i; // Not sure what to do here for the Boolean Value
}
ds.Tables.Add(dt);
foreach (DataRow dataRow in ds.Tables[0].Rows)
{
PageIDHiddenField.Value = dataRow["PageID"];
NameTextBox.Text = dataRow["Name"].ToString();
PageHTMLContentTextBox.Text = dataRow["PageHTMLContent"];
NavigationTextTextBox.Text = dataRow["NavigationText"];
TopMenuCheckBox.Checked = dataRow["TopMenu"]; // Not sure what to do here for the Boolean Value
SubMenuCheckBox.Checked = dataRow["SubMenu"]; // Not sure what to do here for the Boolean Value
DisplayOrder.Text = dataRow["DisplayOrder"].ToString();
ActiveCheckBox.Checked = dataRow["Active"]; // Not sure what to do here for the Boolean Value
}
}
}
You need to cast the value to the correct type. Because
dataRow‘s indexer property will return objects of typeobject, you need to cast it to the right type. Therefore, you need to cast it toBooleanlike so:To do one loop to iterate over the rows in the
DataSetyou’d do something like:You can use
DataRow‘s indexer property to get the specific column.