Scott Booth's Blog
My Links
Blog Stats
  • Posts - 5
  • Stories - 0
  • Comments - 10
  • Trackbacks - 0
Archives

Monday, January 15, 2007

If you ever have a situation in which you need to convert a DateTime data type into hex, here's an easy way to do so:

If you do this conversion often, use constant:
   const long ticks1970 = 621355968000000000; // .NET ticks for 1970

This constant was generated with:
  DateTime dt70 = new DateTime( 1970, 1, 1, 0, 0, 0, 0 );
  long ticks1970 = dt70.Ticks;

get current time: 
 int gmt = (int) ((DateTime.UtcNow.Ticks - ticks1970 ) / 10000000L);
String hexDate = gmt.ToString(“X2“);

or you can convert back:

   int gmt = 0x3e482b89; // sample GMT time in seconds since 1970
   DateTime yourDateTime = new DateTime( ticks1970 + gmt * 10000000L );

posted @ 5:44 PM | Feedback (0)

Tuesday, July 18, 2006

Needing to add values from your database to the page header or footer is pretty common in reports.  Everything I've found to date has said that in Asp.Net 2.0 local reporting services, you could not add any values from your data set to the page header or footer.  The data set values can only be used within the body.  This statement is true.  However, you can use the “value” from a control in your report in the page header or footer. 

So if you have a textbox in the body of the report that stores a value from your data set, you can add a textbox (or any control) to the header, and type =First(ReportItems!Customer.Value)“ in the expression for this textbox.  The “ReportItems“ part of the expression tells the report to find the control called “Customer“ and retrieve the first value in it. 

It's important to note that “Customer“ is the name of my textbox control in the body.  Not the name of the field from the data set. You could also use “Last“ in the expression to return the last value instead of the first.  With this expression, the customer name that is in the textbox will be printed in the header of the report. 

posted @ 5:49 PM | Feedback (3)

Thursday, March 02, 2006

Creating subreports in asp.net 2.0 proved to be very difficult since there is limited documentation on it.  If you don't have all the properties and necessary objects and values just right, you will receive the uninformative message “Error: Subreport can not be shown.“. 

This error can mean lots of things.  Check your stored procedure and data layer to insure that all the objects variables are correct.  Then check all your parameter names, data source names, and report names to ensure they match.

The MSDN is a good starting place to see how to create subreports.  Below is some useful tips that aren't explained in the MSDN.

In the report viewer of the main report, the subreport Name property must be the exact filename of the subreport minus the file extension.  So if your subreport is “MySubReport.rdlc“, you would specify the name as “MySubReport“. 

If you subreport takes any parameters, the parameter name in your subreport rdlc file must match the parameter name in the subreport in the report viewer.  To view/create these parameters. right click on the subreport in the report viewer, click the parameter tab.  Type the name of the param and specify the value for it.  If you are using a field from the report, depending on the data type and param data type, you might need to append “.ToString()“ in order to get an acceptable data type.

One of the keys to using subreports is dynamically adding the data source for the subreport to use.  To do this, add an event handler in the page load for the SubreportProcessing Event:

 ReportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(ItemsSubreportProcessingEventHandler);

In the “ItemsSubreportProcessingEventHandler“, you can create your data source.  The first argument in the ReportDataSource must be the data source name specified in your rdlc file. 

If you are passing parameters to you subreport, you must specify them in your data source as well.  The SubreportProcessEventArgs has a property that stores the parameters pased to the subreport.  You can capture these parameters and add them to your data source parameter list.

void ItemsSubreportProcessingEventHandler(object sender, SubreportProcessingEventArgs e)

 

{

 

if (odsItems == null)

 

odsItems = LoadItemsData();

 

if (e.Parameters.Count > 0)

 

{

 

if (odsItems.SelectParameters.Count > 0)

 

odsItems.SelectParameters.RemoveAt(0);

 

odsItems.SelectParameters.Add(new Parameter("JobId", TypeCode.String, e.Parameters[0].Values[0].ToString()));

 

}

 

e.DataSources.Add(new ReportDataSource("LandscapeItemsInstalledReport", odsItems));

 

 

}

 

private ObjectDataSource LoadItemsData()

 

{

 

ObjectDataSource ods = new ObjectDataSource();

 

ods.TypeName = "Gpe.Operations.Business.LandscapeItemsInstalledBC";

 

ods.SelectMethod = "GetSodTicketItems";

 

return ods;

 

}

 

posted @ 12:00 PM | Feedback (4)

Monday, January 23, 2006

To help myself remember the process to create a report in ASP 2.0, I've decided to blog it here...

Note:  This is only the basic steps to create a simple local report using data objects. 

1. Create Stored Procedure to capture the data for the report

2. If an object does not already exist to store this data, create the class for the properties, the data layer (DAO), and business container(BC). 

3. Right click in your project, select New Item, then Report.  This will create a new RDLC file that you will use to create the report layout.

4. Create your datasource for the RDLC.  In the RDLC file, click Report-> Data Sources.  If you don't see “Report“ in the menu bar, click the RDLC file.  The menu bar should refresh.  Select the BC that returns the list of your data.  Click Add to Report.

Note:  This seems to be kind of buggy.  There have been times when no available data sources displays.  If this happens, try recompiling your solution.  If no data sources still displays, close the solution and reopen it.  If still nothing, try rebooting your computer.  Not sure why this happens, but eventually you will see the list of data sources.

Another thing to note, all data sources show in this list.  So all your BC, DAO, and Entity classes will show in this list.  Be sure to pick the BC.  There doesn't seem to be any rhyme or reason to how this list is sorted.  You just have to scroll through and pay attention.

5.  If you want to display a list of data, you will need a “list“ in the body.  Drag the list from the toolbox onto the report.  Set the “DataSetName“ property to the data source you created.  ASP.net has other controls besides a “list“ to display data.  Check the MSDN for documentation on these other controls.

6. Select the Web Data Sources on the left hand toolbar.  It shows all available data sources.  See above if nothing shows in the list.  Expand the correct data source that has your data list.  Drag the items you want onto the list.  The default items are only the First item in that list:  “=First(Fields!Name.Value)“.  Change this to “=Fields!Name.Value“ to show all items in this field.  You can right click on the item, select properties to change how it looks and add a function to the item.

7. Continue adding fields, and other controls to the report as needed.

Note:  One limitation with 2.0 Reporting Services is that you can not add a field to the page header or footer.  Or if you can, no one in Google has figured it out and is telling.  So your report titles must be generic. 

8.  When the report layout is done, create a new ASP page.  Drag the report viewer control onto the page.

9. Create a new object data source that points to the BC for your data.

10.  Run the project and see how you did.

Note:  If there is an error in the RDLC, you may see many errors like fields can not overlap a line and other formatting errors.  Typically there is only one error, like an invalid data source, that once you correct, all the other errors will go away.

posted @ 10:59 AM | Feedback (0)

Wednesday, January 11, 2006

Today I ran into an issue trying to populate one dropdownlistbox from another within a formview.  I have a one dropdown called “ddlCity“ and another called “ddlSubdivision“.  The city dropdown store cities retrieved from the Subdivision table in my database.  To shorten the list of subdivisions in the “ddlSubdivision“ control, I want my users to select a city first.  The ddlSubidision control uses the selected city value as a parameter to a stored proc to retrieve it's values.

I found out you can not use 2 way data binding to populate a dropdownlist from another dropdownlist within a formview thanks to Phillip Williams at www.webswapp.com.  If you try, you will get an error: “"Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control".

In order to populate a dropdownlist from another within a formview, you must remove the 2 way data binding from the dropdownlist that will be populated and will manually bind the items to the control.  First, in the ddlSubdivision, I removed the “SelectedValue” piece of code. Notice there is not a “Bind“ or “Eval“ tag.

<asp:DropDownList ID="ddlSubdivision" runat="server" DataSourceID="odsSubdivisionList" DataTextField="Name" DataValueField="Id" OnDataBinding="ddlSubdivision_DataBound" Width="100%">

 

In the code behind file, there are 2 methods you need to add your customized code:

protected void frmvwPackage_ItemUpdating(object sender, FormViewUpdateEventArgs e)

{ e.NewValues["City"] = ((DropDownList)((FormView)sender).FindControl("ddlCity")).SelectedValue;

e.NewValues["SubdivisionId"] = ((DropDownList)((FormView)sender).FindControl("ddlSubdivision")).SelectedValue; }

protected void ddlSubdivision_DataBound(object sender, EventArgs e)

{

DropDownList ddl = (DropDownList)sender;

FormView frmV = (FormView)ddl.NamingContainer;

if (frmV.DataItem != null)

{

string strSubdivision = ddl.SelectedValue.ToString();

DropDownList ddlSubdivision = (DropDownList)frmV.FindControl("ddlSubdivision");

ddl.ClearSelection();

ListItem lm = ddl.Items.FindByValue(strSubdivision);

if (lm == null)

{ ddl.SelectedIndex = 0; }

else

{ lm.Selected = true; }

}

}

Be sure to set the page directive for AutoEventWireup to False and the selected dropdownlist AutoPostBack property to True.

posted @ 2:01 PM | Feedback (3)
Scott Booth