Link to home
Start Free TrialLog in
Avatar of derekclee
derekclee

asked on

When populating the column in a datagrid I get an overflow error as described below

'Value was either too large or too small for an Int16. Couldn't store '41100' in 'Frequency' Column. Expected type is Int16'.

The variable pushing the value '41100' into the column is declared as an Integer so there should be no problem there. Could it be that my actual column 'Frequency' is not capable of storing the value?

Thanks
Dim dr As DataRow = dsDownloaded.Tables(0).NewRow
dr.Item("Last_Date")=dsDownloads.Tables(0).Rows(i).Item("date_attempt")
dr.Item("Username")=dsDownloads.Tables(0).Rows(i).Item("email_address")
dr.Item("Frequency") = iCount 'this is the offending line
dsDownloaded.Tables(0).Rows.Add(dr)

Open in new window

Avatar of adathelad
adathelad
Flag of United Kingdom of Great Britain and Northern Ireland image

Yes, the column 'Frequency' in your datatable cannot hold the value - it's because that column in the DataTable is defined as datatype Int16 which only allows values between -32768 and 32767 to be assigned to it.

Your iCount variable, declared as an integer allows larger numbers (i.e. as in your case) and so you get the error when you try to assign that value into the Int16 column.

Really, your iCount variable should be the same datatype as the Frequency column...therefore that would prevent that assignment ever failing as the variable could only contain values supported by the Frequency column it gets mapped into.

If you need to allow these larger numbers, then you need to change your Frequency column type to Integer (which defaults to a 32 bit integer = Int32)
Avatar of derekclee
derekclee

ASKER

Thanks, I suspected as much and already had a look at changing the column type default to Integer but it was not immediately apparent how to do it. To close this would be grateful if you could indicate how I can change this default.

Presumably, you are populating the DataTable from a database and that you're not manually defining the datatable columns/datatypes yourself....in which case it's in your database.

e.g. assuming you're using SQL Server, check the Frequency column datatype - it'll be SMALLINT (which translates to the .NET type Int16)....it will need changing in the database to INTEGER (which translates to the .NET type Int32)
Hi,

The 'Frequency' column is the one that is not populated from a database but from the 'iCount' integer variable which should never exceed a value of 50000.

Thanks.
Dim dcFrequency As New DataColumn
dcFrequency.ColumnName = "Frequency"
dsAll.Tables(0).Columns.Add(dcFrequency)
.
.
.
dr.Item("Frequency") = iCount

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of adathelad
adathelad
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial