MVC Razor Web Grid- Error - asp.net-mvc-3

I am using MVC3 webgrid. I am displaying the data in a webgrid and before displaying the data I have to calculate the number of days between start date and end date. I am using the following code.
int noOfAbsenceDays = item.AbsEnd?(item.AbsEnd.Subtract(item.AbsStart)).Days: (item.DateTime.Now.Subtract(item.AbsStart)).Days;
It complains about this error
Cannot implicitly convert type 'System.DateTime' to 'bool'
I don't know where it is coming from?
Thanks

You can try the following code:
int noOfAbsenceDays = item.AbsEnd == null ? (item.AbsEnd.Subtract(item.AbsStart)).Days : (item.DateTime.Now.Subtract(item.AbsStart)).Days;

Related

ASP.NET MVC 3 Razor DateTime.Parse don't work

I'm trying to do a cast with dates but it's throws me an exception
.
The code is:
This Works ->
var FechaInicio = Model != null ? DateTime.Parse(Model.FechaInicio).Date.ToString("dd/MM/yyyy") : DateTime.Parse("").Date.ToString("dd/MM/yyyy");
This Doesn't Work ->
var FechaFin = Model != null ? DateTime.Parse(Model.FechaFin).Date.ToString("dd/MM/yyyy") : DateTime.Parse("").Date.ToString("dd/MM/yyyy");
The model is Ok and values are dates in string format
The error is:
Server Error in '/' Application.
String was not recognized as a valid DateTime.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: String was not recognized as a valid DateTime.
Source Error:
Line 13: var Ubicacion = Model != null ? Model.Ubicacion : null;
Line 14: var FechaInicio = Model != null ? DateTime.Parse(Model.FechaInicio).Date.ToString("dd/MM/yyyy") : DateTime.Parse("").Date.ToString("dd/MM/yyyy");
Line 15: var FechaFin = Model != null ? DateTime.Parse(Model.FechaFin).Date.ToString("dd/MM/yyyy") : DateTime.Parse("").Date.ToString("dd/MM/yyyy");
The error is straight-forward. The string being passed to Parse could not be interpreted as a valid DateTime. By default, the format of the current culture is employed, when using Parse. Not sure what that is exactly in your case, but it would likely be the same as you would see by outputting DateTime.Now.ToString().
The reason DateTime.Today.ToString() failed, is because the output will only have a date component. The default format utilized by Parse will require date and time components.
If you need to parse a string into a DateTime that doesn't match the format of the current culture, then you need to use ParseExact instead of Parse and pass a format string that shows how the string-based datetime is formatted.

Getting strange exception in OpenXml SDK 2.5

I have a simple file *.xlsx created in Office 2010 with 3 spreadsheets. Only first is used and have easy formula like Sum and Multiplication in cells. It also contains 17 columns and 4k rows filled. Data types of the cells are: Numeric, Short Date, Long Date and Text. When I'm trying to read that file using OpenXml SDK 2.5 and validate it to make sure this file is correct I'm receving this strange exception:
ErrorType: Schema.
Error Description: The attribute 'verticalDpi' has invalid value '0'. The MinInclusive constraint failed. The value must be greater than or equal to 1.
This is method how I'm reading and validating:
VB.NET:
Using spreadsheet As SpreadsheetDocument = SpreadsheetDocument.Open(m_FileName, False)
Dim validator As New OpenXmlValidator
Dim errorsValidation As IEnumerable(Of DocumentFormat.OpenXml.Validation.ValidationErrorInfo) = validator.Validate(spreadsheet)
If Not errorsValidation.Any() Then
'Do something here if it's correct
Else
Console.WriteLine(errorsValidation.First().Description)
End If
End Using
C#:
using (SpreadsheetDocument spreadsheet = SpreadsheetDocument.Open(m_FileName, false)) {
OpenXmlValidator validator = new OpenXmlValidator();
IEnumerable<DocumentFormat.OpenXml.Validation.ValidationErrorInfo> errorsValidation = validator.Validate(spreadsheet);
if (!errorsValidation.Any()) {
//Do something here if it's correct
} else {
Console.WriteLine(errorsValidation.First().Description);
}
}
I don't know what I'm doing wrong and what is wrong. Can't find any issues to how to fix the file. Any idea, suggestions?

Getting value of database field in Crystal Reports

Currently I'm working on legacy application, that uses crystal report engine. I have to get value of database fields programmatically. As I've assumed, I need proper event for getting next code to work:
Report.Database.Tables(1).Fields(1).Value
But the value is always empty in DownloadStarted/Finished event handlers. What I'm doing wrong and is it at least possible?
I think that if you want to get value of your table fields in program the best way is that you get the field name from report and then connect to your table directly and use report field names as the table columns name
i do it in c# i hope it can help you in vb6 too:
string name = report2.Database.Tables[1].Fields[1].Name;
string[] names = name.Split('.');
and then add your database to your program and use names like this:
DataTable dt = new DataTable();
string[] value = dt.Columns[names[1]];
if you just need your tables values, you can use my last answer, but if you need value of database fields in crystal report, i mean something like formula field ,this code can help you:
CRAXDRT.FormulaFieldDefinitions definitions = report2.FormulaFields;
string formulaText = "IF " + report2.Database.Tables[1].Fields[3].Name
+ " > 10 THEN" + report2.Database.Tables[1].Fields[2].Name;
definitions.Add("Test", formulaText);
report2.Sections[1].AddFieldObject(definitions[1], 0, 0);

formatting in razor nested webgrid

I'm nesting a webgrid inside another webgrid as shown in Razor Nested WebGrid
But when I try to format the columns inside the nested webgrid it's throwing an error stating that the column in the mastergrid has invalid arguments.
Has anyone faced this problem before?
Any suggestions?
Thanks
Arnab
I guess your problem is that you tried to use the same parameter name item in the inner format parameter. You cannot use the same parameter name in nested lambda expressions. You can find here more about lambda expressions.
So you need to use a different parameter name (e.g. subItem) for the inner format:
...
#topGrid.GetHtml(columns:
topGrid.Columns(
topGrid.Column("Index"),
topGrid.Column("SubItems", format: (item) =>
{
WebGrid subGrid = subGrid = new WebGrid(item.SubItems);
return subGrid.GetHtml(
columns: subGrid.Columns(
subGrid.Column("A", format: (subItem) => string.Format("Formatted: {0}", subItem.A)),
subGrid.Column("B")
)
);
})
)
)
...

Showing a default value in a WebGrid when a null is present?

I've got Webgrid sourced to a EF4 entity with navigation properties (basically relationships)
If the webgrid encounters a null for that foreign key it errors out because it's looking for that object, which in this case doesn't exist.
Is it possible to catch when a column item is null and default to a value within the Webgrid helper?
I guess the follwoing code should serve your purpose if I am correctly relating to your problem. Below Trigger is the navigated entity which we will get by include in linq and we can put a check like below when this entity is null.
grid.Column("Job", format: #<text> #if (#item.Trigger !=null) { <span> Write your default code here .</span> } </text> close text tag here ),
Hope that solves your problem.
I noticed after posting 'text' is getting truncated in this forum so put text in < and > between 2 # as you can see and also close that text tag before the final ).
All the best.

Resources