Getting DataTable column names using linq, error when column is null - linq

I have a datatable with n columns & I am converting it to Pivot.
I already have a piece of code for converting it to Pivot, reference :
Adding Sum per row in PIVOT using LINQ
Now problem comes when there is a column with null values in it.
following line fails with error
dtPivot.PrimaryKey = dtPivot.Columns.Cast<DataColumn>().ToArray();
Error : Column 'x' has null values in it.
Can any one help me, how to handle this...

I'd start by looking at what exactly is causing the issue.
Add a breakpoint before the line, and see what items are contained in dtPivot.Columns (you can try to use ToArray()).
Are there items which are null or are there items with properties which are null that's causing the problem? You can check this by using the watch in the debugging window to try and cast an item from dtPivot.Columns manually to DataColumn, and see which ones throw errors (and possibly, WHY they throw errors).
If the error is because some of the items in dtPivot.Columns are null, then you could try and filter them out first using Cast<object>().Where(o => o != null). If the error is because a property is null, then see if there isn't anything else you could cast the columns to first, and then try to filter the columns (or fill out the missing values, if need be).

Related

How to use previous in crystal report and display not null.

I want to suppress the duplicate and display only one value but the previous is only displaying the first record i have first record blank or "null" and the second is not null the previous is displaying the first value.
I Solved my problem using this codes.
PreviousIsNull({#Q3}) or {#Q3} <> Previous({#Q3}) and PreviousIsNull({#E3}) or {#E3} <> Previous({#E3});
Select the field you want and select on the properties suppress on duplicate

How do you show all rows with missing fields in Crystal Reports?

I'm struggling with Crystal Reports suppressing rows whenever I add a field that some rows may not have data in.
I've been able to fix some of the rows and make them show by using the formula:
if not isnull({field}) then {field} else "Arbitrary string to make row display"
This at least fills in the absent field with something and displays the row.
Do I really have to try and identify every field that may have incomplete data for some rows? Or is there some global method to make all rows show no matter what?
Something like: If isnull(ANYTHING) then " "?
you can do right click on the field, then under suppress, click the formula icon beside it and you can input there the conditions.
You can try the following in report options:
Convert Database NULL values to Default
Convert Other NULL values to Default.
This is hidden in File > Report Options. I have used Convert Database NULL Values to default to show 0's instead of blanks for null valued summaries

getUserBy() phpfox

During working I faced a very weird thing in phpfox script
I put in the table user a new field .. and this field is tinyint with default value 0 and started to work on giving the user the ability to insert the value through links and finally it's succeeded but when I tried to get this value by getUserBy('name_of_the_field') it gave me a null value although I checked it in the database table and found that field has a value ... so could you help me please ?!
The getUserBy() does not get every field in the user table, there is a predefined list of columns that it will fetch.
You will need to get this field in a different way or write a plug-in to the hook "user.service_auth___construct_query" so it loads your new field, I have not tried this but I believe it should work as a plug-in to that hook:
$this->database()->select('u.my_new_field,');

C# LINQ - Recognize unique index violation

I am using Linq-to-SQL for accessing SQL Server 2008.
In database I have UNIQUE index on some column.
When I enter duplicated data in that column, LINQ will throw SqlException with an error message. Is there a way to figure out that exception is related to UNIQUE index and not some other SQL error?
Solution I am aware of is to user RegEx to parse exception message, but I would like to know is there more elegant approach?
You could use the SQLException.Number property.
For more details you could iterate the Errors collection each of which have thier own number.
As an aside, in the past I have written my own custom serialiser for SqlException but it appears that in 4.5 ToString() has been overridden so this is not now necessary.
You could check the table having the unique constraint on whether it already has a row with some value in the constrained column:
if (linqDataContext.ConstrainedTable.Any(row => row.ConstrainedColumn == somevalue))
{
//show a message, saying you've already got this value
//and it is not applicable
}
else
{
//accept your changes with smth like this:
linqDataContext.SubmitChanges();
}
Another question, whether this approach is applicable for you, i.e. whether the cost of this query overwhelms the cost of catching an exception and processing it. If the constrained column is indexed, than a simple request on that column must not be that costy.

Linq Sort a Datatable by DateTime Field

How would I sort a DataTable using Linq? I've tried the following but received the error:
InvalidCastException was unahndled by user code. Specified cast is not allowed.
var query = from c in allFiles.AsEnumerable() orderby c.Field<DateTime>(1)
descending select c;
That would suggest that for at least some row, field 1 isn't a DateTime. If it may be null, you might want to try DateTime? instead. Or check that it really is that field in the first place... maybe use a name instead of a number?
The table was dynamically generated and had no actual specified column dataType. When I created the column and specified the dataType the issue was resolved.
I'm not sure why I didn't realize the dataType wasn't defined until after I posted this question.

Resources