In Dynamic-Data how do I get the results count of a DynamicFilter? - dynamic-data

I have a Dynamic-Data application with a List.aspx main page listing data which can be filtered by country. The country table is used as a DynamicFilter as it is a foreignkey to the main table.
When I choose a state the DynamicFilter_FilterChanged method fires and it filters the main table properly but I'm trying to figure out how I can get the total number of rows that the filtering has produced.
I've looked at previous posts here with no luck.
Any ideas?

You can count when the Gridview on the list.aspx page is selected
protected void GridDataSource_Selected(object sender, LinqDataSourceStatusEventArgs e)
{
if (e.TotalRowCount > 0)
{
lblCount.Text = e.TotalRowCount.ToString() + ((e.TotalRowCount == 1) ? " item found." : " items found.");
}
}

Related

How do I create a nice message when there are no rows in a Microsoft rdlc sub report?

I am using the ReportViewer control in a website, my reports contain 3 or more sub reports. Often the data in the sub-report does not exist due to it being optional data. I need to output a nicely formatted and aligned message to let the reader know. I have the following code:
protected void ReportViewer1_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
{
DataTable dtReport = GetReportSub(reportId);
foreach (DataRow drRow in dtReport.Rows)
{
if (Replace(drRow["subName"].ToString(), " ", "") == e.ReportPath)
{
string sSQLMain = SQLReplacer(drRow["subSQL"].ToString());
DataTable dtData = BuildCollectionArray(sSQLMain, new OdbcParameter());
if (dtData.Rows.Count == 0)
{
//TODO: Hide the report and show a friendly message
}
else
{
ReportDataSource dataSource = new ReportDataSource("DS" + Replace(drRow["subName"].ToString(), " ", ""), dtData);
e.DataSources.Add(dataSource);
}
break;
}
}
}
Which will fetch the data and add it to the report that requires it. However, if there is no data the only option is the NoRowsMessage property. However the NoRowsMessage property in the main report on each of the sub reports is too limiting.
Ideally I'd like to have each sub-report take care of the no rows message itself using an exporession or code. However the flaw in the Microsoft way of working appears to be that the report is conditional on there being data to display.
So I have found a workable solution to my problem. I am ignoring the default no rows functions and instead doing the work in the sub report.
I have a Tablix with my details in, I will then create an extra Textbox with my no rows message. The no rows message is hidden depending on the DataSet such as:
=iif(Sum(Fields!ActID.Value, "DSSub") > 0, true, false)

asp.net dynamic data web site : passing default value while inserting/updating records

I am working in ASP.NET Dynamic Data web application and have an issue of passing default value while inserting/updating records. In my application all table has following common column:
CreatedBy (Default Value : Logged in user)
CreatedDate (Default Value : DateTime.Now)
modifiedBy (Default Value : Logged in user)
ModifiedDate (Default Value : DateTime.Now)
I want to keep these column hide in Insert and Edit page and want that default value will be inserted automatically in the respective column.
Please suggest me.
Thanks
paul
I see you are doing simple Auditing have a look at my blog post here Basic Auditing for Dynamic Data with Entity Framework 4.x hope that helps.
It's never good to do this sort of thing in the page, it's always best to do it in your data model/layer. You can use my A New Way To Do Column Generation in Dynamic Data ... to hide the columns in all pages.
Ref:
Maintaining a Log of Database Changes - Part 1
In order to maintain a history of changes to the database's data we
need to record every insert, update, and delete to some sort of
"history" table. In addition to capturing the data that was inserted,
updated, or deleted, we also need to note what user made the
modification, as well as the date and time it was made.
More Reference:
Best design for a changelog / auditing database table?
Log changes to database table with trigger
To customize ORM Entity Framework check following links:
How to use Default column value from DataBase in Entity Framework?
How to: Execute Business Logic When Saving Changes
solution i have implemented finally:
protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
{
e.Values.Add("CreatedBy", HttpContext.Current.User.Identity.Name);
e.Values.Add("CreatedDate", DateTime.Now);
}
protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
e.OldValues.Add("ModifiedBy", null);
e.OldValues.Add("ModifiedDate", null);
e.NewValues.Add("ModifiedBy", HttpContext.Current.User.Identity.Name);
e.NewValues.Add("ModifiedDate", DateTime.Now);
}
My solution is a bit different from Paul's requirements.
In the file DynamicData\PageTemplates\Insert.aspx.cs I made edits to show my defaults for new records in any table having the shared fields. The user can still put in something else on insert.
public partial class Insert : System.Web.UI.Page
{
protected MetaTable table;
protected void Page_Init(object sender, EventArgs e)
{
table = DynamicDataRouteHandler.GetRequestMetaTable(Context);
var values = table.GetColumnValuesFromRoute(Context);
// set default values for meta data of new records across all tables
// unknown values will be skipped
values.Add("creationDate", DateTime.Now);
values.Add("modificationDate", DateTime.Now);
values.Add("modificationUser", HttpContext.Current.User.Identity.Name.Substring(
HttpContext.Current.User.Identity.Name.IndexOf("\\") + 1));
FormView1.SetMetaTable(table, values);
DetailsDataSource.EntityTypeFilter = table.EntityType.Name;
}
[...]
}
For editing records with existing values, I made changes to some DynamicData\FieldTemplates files.
public partial class Text_EditField : System.Web.DynamicData.FieldTemplateUserControl
{
protected void Page_Load(object sender, EventArgs e)
{
// ...
// show current user as value for the modification user upon editing records
if (Column.Name == "modificationUser")
{
FieldValue = Page.User.Identity.Name.Substring(Page.User.Identity.Name.IndexOf("\\") + 1);
}
}
[...]
}
It will show the updated value on the page for editing, but after updating the changes won't persist! An additional change to the Edit page template is required:
protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
// make sure a meta data update is always triggered by setting a different old value
// required for the edit components
if (e.OldValues.Contains("modificationUser"))
{
e.OldValues["modificationUser"] = string.Empty;
e.OldValues["modificationDate"] = DateTime.MinValue;
}
}

Two way binding with custom expression?

I am using a gridview in my asp.net project to view and modify some records from the database. The database has two columns: start_date and end_date. When a new record is created these columns contains null, but they can be modified later using the gridview update command.
In gridview I have two template fields (having names start_date and end_date) in which I have placed two calendar controls. Upon clicking an update link of gridview it always returns an error because of the null value binding to the calendar. I have used this helper function to solve it:
protected DateTime ReplaceNull(Object param)
{
if (param.Equals(DBNull.Value))
{
return DateTime.Now;
}
else
{
return Convert.ToDateTime(param);
}
}
and used these two custom expressions in calendar control's SelectedDate:
ReplaceNull(Eval("start_date"))
ReplaceNull(Eval("end_date"))
The problem is that two-way data binding the calendars upon selecting a date does not update the database table. Are there any workarounds? Or alternatively, a better solution would be appreciated.
I don't know why you let them null when insert a new record , but many ways you can solve this problem i think .
one of them : in the RowDataBound event of the Gridview
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[1] == null) //the index of the start_date
{
e.Row.Cells[1].Text = DateTime.Now.ToString(); // in your case you will make the selected date of the calender(through casting to calender) with the value you need.
}
}
}
Or: you can catch the exception , you meet in your Update button through
try and catch block.

Gridview cell value and array list

When I am tryng to pass the gridview cell values to an array everything is perfect but the array is empty after executing the code.
When I see through debug mode the index of selected row also is fine, i.e. when I select two rows using checkbox among 1000 the count shows exactly 2 in the array but the data is empty.
I'm not able to get the cell value from the gridview.
Any advice would be greatly appreciated.
protected void Button2008_Click(object sender, EventArgs e)
{
ArrayList checkedItems = new ArrayList();
foreach (GridViewRow row in this.GridView1.Rows)
{
if (((CheckBox)row.FindControl("cbRows")).Checked == true)
{
checkedItems.Add(row.Cells[12].Text);
}
}
Session["CheckedItems"] = checkedItems;
Response.Redirect("About.aspx", true);
}
you can use a breakpoint and check where the problem is occuring, for instance, does row.Cells[12].Text show any value ?
and you can check how your aspx page is acting after the postback.

How can I retrieve a subset of data from entity object data source and pass to another page

I am playing about just now trying to teach myself a little bit about the entity framework.
I have a Gridview data bound to a Entity Date Source using the Entity Framework. If I select certain items in that list I then wish to redirect another page and populate another gridview with just the items selected (but with more detail, different includes/navigation properties)
This is probably the most simple thing but I have spent 2 hours banging my head on the wall trying to get this to work.
Essentially I have a continue button which when clicked should identify all the UIDs (a column in the gridview) of the rows and allow me to subset to just these rows and pass them to another page to be rebound to another datagrid
Any ideas???
Well, the big picture is that you should get those IDs, pass them to the other page, and then use a query with Contains; see this question for an idea of how to use it:
How search LINQ with many parametrs in one column?
Assuming you haven't used DataKeys in your GridView, this would be my approach.
Page 1
protected void Button1_Click(object sender, EventArgs e)
{
var checkedItems = new List<int>();
foreach (GridViewRow row in GridView1.Rows)
{
var checkbox = (CheckBox)row.FindControl("CheckBox1");
if (checkbox.Checked)
{
checkedItems.Add(int.Parse(row.Cells[1].Text));
}
}
Session["checkedItems"] = checkedItems;
Response.Redirect("Page2.aspx");
}
Page 2
protected void Page_Load(object sender, EventArgs e)
{
var checkedItems = (List<int>)Session["checkedItems"];
Session["checkedItems"] = null;
foreach (var checkedItem in checkedItems)
{
Response.Write(checkedItem);
}
}
Using the IDs in the checkedItems List you can now query those from you DB and finally assign the Result to your GridView on the second page.
Instead of using Session you could pass the IDs via QueryString.

Resources