Click here to Skip to main content
16,004,406 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected string gridValueChopping(object dataItem, string dataField, int iSize)
    {
        string gridValue = (string)DataBinder.Eval(dataItem, dataField);
        string output = string.Empty;
        if (gridValue.Length > iSize)
        {
            output = ((string)DataBinder.Eval(dataItem, dataField)).Substring(0, iSize - 3);
            output = output + "...";
        }
        else
        {
            output = (string)DataBinder.Eval(dataItem, dataField).ToString();
        }
        return output;
    }




I am getting the "system.invalidcastexception unable to cast object of type 'system.int64' to type 'system.string'"error in the following line of the above mentioned code -

string gridValue = (string)DataBinder.Eval(dataItem, dataField);


can I get a solution for this?

What I have tried:

C#
string gridValue = (string)DataBinder.Eval(dataItem, dataField);


I changed this line as,

C#
string gridValue = (string)DataBinder.Eval(dataItem, dataField).ToString;


but even then the error repeats..
Posted
Updated 8-Mar-16 0:55am
v2
Comments
Jochen Arndt 8-Mar-16 6:27am    
Are the missing parentheses for ToString in the changed line a typo?
It should be:
string gridValue = DataBinder.Eval(dataItem, dataField).ToString();
Member 12377290 9-Mar-16 6:36am    
I tried this, but it didnt workout.. solution given by Richard Deeming helped me.

Another alternative:
C#
string gridValue = DataBinder.Eval(dataItem, dataField, "{0}");

Also, don't Eval the same value multiple times; use the value retrieved from the first call to Eval instead:
C#
protected string gridValueChopping(object dataItem, string dataField, int iSize)
{
    string gridValue = DataBinder.Eval(dataItem, dataField, "{0}");
    string output;
    
    if (gridValue.Length > iSize)
    {
        output = gridValue.Substring(0, iSize - 3);
        output = output + "...";
    }
    else
    {
        output = gridValue;
    }
    
    return output;
}
 
Share this answer
 
Comments
Member 12377290 9-Mar-16 6:31am    
thnak you so much for your help.. it helped me
C#
long longValue = (long)DataBinder.Eval(dataItem, dataField);
string gridValue = longValue.ToString();

should do it, or at least should allow to better understand where the problem lies.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900