2

I am grouping data in datatable and summing up one column using linq but while run time i am getting error stating that

Cannot cast DBNull.Value to type 'System.Int32. Please use a nullable type" Below is my code

Dim resultStyle = invData.Tables(0).AsEnumerable() _
      .GroupBy(Function(v) New With {Key .InvestorStyleID = v.Field(Of Integer)("InstitutionalInvestorStyleID"), Key .StyleName = Not IsNothing(v.Field(Of String)("InstitutionalInvestorStyleName"))}) _
      .Select(Function(v) New With {Key .InvestorStyleID = v.Key.InvestorStyleID, Key .StyleName = v.Key.StyleName, Key .Sum = v.Sum(Function(r) Double.Parse(r.Item("k001ICGeo").ToString()))})

Kindly suggest me how to avoid taking null value.I am getting error on second line

.GroupBy(Function(v) New With {Key .InvestorStyleID = v.Field(Of Integer)("InstitutionalInvestorStyleID"), Key .StyleName = Not IsNothing(v.Field(Of String)("InstitutionalInvestorStyleName"))}).

I don't want to take the null value.

1
  • I believe the problem is the InstitutionalInvestorStyleID. Check if this is nullable in database. Try v.Field(Of Integer?)("InstitutionalInvestorStyleID") Commented Feb 14, 2017 at 12:10

3 Answers 3

1

DataTables do not store null values, instead, they store DBNull.

Your IsNothing is therefore not helpful.

So, you need to compare with DBNull.Value (a singleton of DBNull).

Sign up to request clarification or add additional context in comments.

Comments

0

Try something like this:

Dim resultStyle = invData.Tables(0).AsEnumerable() _
                        .GroupBy(Function(v) New With {Key .InvestorStyleID = v.Field(Of Integer?)("InstitutionalInvestorStyleID").GetValueOrDefault, 
                                                        Key .StyleName = If( String.IsNullOrEmpty(v.Field(Of String)("InstitutionalInvestorStyleName")),"", v.Field(Of String)("InstitutionalInvestorStyleName"))}) _
                        .Select(Function(v) New With {Key .InvestorStyleID = v.Key.InvestorStyleID, Key .StyleName = v.Key.StyleName, Key .Sum = v.Sum(Function(r) Convert.ToDouble(r.Item("k001ICGeo"))))})

1 Comment

Thanks for your help. I am not getting any error now but i don't want to take that particular row if the stylename is null or "".How to avoid taking that row.
0

The error message is because you have null values in column which you are trying to cast while grouping, so simply change it to Nullable type. Also, to get only the records which don't have null values apply a filter using Where clause like this:-

Dim resultStyle = invData.Tables(0).AsEnumerable() _
    .GroupBy(Function(v) New With {Key .InvestorStyleID = v.Field(Of Integer?)("InstitutionalInvestorStyleID"), Key .StyleName = Not IsNothing(v.Field(Of String)("InstitutionalInvestorStyleName"))}) _
    .Where(Function(x) x.Key.InvestorStyleID  IsNot Nothing) _
    .Select(Function(v) New With {Key .InvestorStyleID = v.Key.InvestorStyleID, Key .StyleName = v.Key.StyleName, Key .Sum = v.Sum(Function(r) Double.Parse(r.Item("k001ICGeo").ToString()))})

I have also added a sample fiddle to show how it works with some sample data.

Fiddle.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.