Click here to Skip to main content
15,884,017 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Hello,

I am getting an
'object' does not contain a definition for 'Trim' and no extension method 'Trim' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
error on this line:

SourcePageFile = SourceFolder + "\\pg-" + Convert.ToDecimal(DataGridView1.Rows[irow].Cells[0].Value.Trim(' ')) - 1 + ".jpg";


It is a converted code from Visual Basic that goes like this:
SourcePageFile = SourceFolder & "\pg-" & CInt(Trim(DataGridView1.Rows(irow).Cells(0).Value)) - 1 & ".jpg"


How do I successfully execute the syntax to get rid of this error?

Please help.

Regards
Aman Chaurasia

What I have tried:

I have tried the following:

SourcePageFile = SourceFolder + "\\pg-" + Convert.ToDecimal(DataGridView1.Rows[irow].Cells[0].Value.Trim(' ')) - 1 + ".jpg"


SourcePageFile = SourceFolder + "\\pg-" + Convert.ToInt32(DataGridView1.Rows[irow].Cells[0].Value.Trim(' ')) - 1 + ".jpg"


SourcePageFile = SourceFolder + "\\pg-" + Convert.ToUInt32(DataGridView1.Rows[irow].Cells[0].Value.Trim(' ')) - 1 + ".jpg"


SourcePageFile = SourceFolder + "\\pg-" + Convert.ToString(DataGridView1.Rows[irow].Cells[0].Value.Trim(' ')) - 1 + ".jpg"
Posted
Updated 16-May-18 0:43am

1 solution

You can only "Trim" on a string variable, the error you are getting suggested that "Value" is an object. How you deal with this depends on what is in the cell. The solution might be
SourcePageFile = SourceFolder + "\\pg-" + Convert.ToDecimal(DataGridView1.Rows[irow].Cells[0].Value.ToString().Trim(' ')) - 1 + ".jpg"

or
SourcePageFile = SourceFolder + "\\pg-" + Convert.ToDecimal(((string)DataGridView1.Rows[irow].Cells[0].Value).Trim(' ')) - 1 + ".jpg"

Also converting to decimal isn't the same as converting to int which is what the original did. Your code is also very fragile and prone to throwing an exception if values are not what you expect. You should use int.TryParse rather than Convert.ToDecimal. Google for usage.
 
Share this answer
 
v2
Comments
Primo Chalice 17-May-18 5:03am    
None of them worked. I tried all but it did not work. Now, only Trim is underlined in red. When I am writing Vaue.ToString(), then the whole syntax is showing an error. Please help.
F-ES Sitecore 17-May-18 5:12am    
Try this

SourcePageFile = SourceFolder + "\\pg-" + (Convert.ToDecimal(dataGridView1.Rows[irow].Cells[0].Value.ToString().Trim(' ')) - 1).ToString() + ".jpg";
Primo Chalice 17-May-18 5:14am    
It worked :D. You are amazing :) :).

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