nhoyti
nhoyti

Reputation: 1997

string.format error

I've been searching the internet with the string.format for my code and it seems that I can't find the right one that looks like on my code.

DataColumn dtCol;
dtCol = new DataColumn("ImagePath", System.Type.GetType("System.String"));
dtImages.Columns.Add(dtCol);
dtImages.Columns["ImagePath"].Expression = string.Format("<a href=\"'{0}'+ImageFilename\">View Image</a>", ImageDownloadPath);

(the ImageFilename is a column on my database table) the above code always throws an error of "Syntax error: Missing operand before '<' operator"

How do I do this properly?

Upvotes: 1

Views: 1366

Answers (4)

nhoyti
nhoyti

Reputation: 1997

Thanks for helping me out. I was able to figure out the way to do it. Thanks for all the posts.

alt text

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1504142

It's not at all clear from the question, but I believe the problem isn't a compile-time one at all... not indeed one with string.Format. It's a problem with DataColumn.Expression. You're giving an expression which includes angle brackets, so it thinks you're trying to perform comparisons.

I can't say I know much about DataColumn.Expression, but you should look into how it quotes strings... and how it quotes quotes within strings! For example, this might work:

dtCol.Expression = string.Format
    ("'<a href=\"{0}'+ImageFilename+'\">View Image</a>'", 
     ImageDownloadPath);

However, I think it's likely to make your life a lot simpler if you didn't try to compute the HTML in the expression to start with. Can you really not apply processing any later?

Upvotes: 1

Kyle Rosendo
Kyle Rosendo

Reputation: 25287

Perhaps try the following?

string.Format("<a href=\"{0}\\{1}\">View Image</a>", ImageDownloadPath, ImageFilename);

In your code you are using ''s around the string format identifier, which would have then shown up in your formatted string, and the ImageFilename property was not being used correctly. It would have simply been added as plain text.

The result of your string with the following values would be as such:

ImageFilename = "1.jpg";
ImageDownloadPath = "http://www.downloadme.com/images";

Yours: <a href="'http://www.downloadme.com/images'+ImageFilename">View Image</a>
Mine: <a href="http://www.downloadme.com/images/1.jpg">View Image</a>

Upvotes: 1

Brady Moritz
Brady Moritz

Reputation: 8909

Try this:

dtImages.Columns["ImagePath"].Expression = 
string.Format("&lt;a href=\"'{0}'+ImageFilename\"&gt;View Image&lt;/a&gt;", ImageDownloadPath); 

Im pretty sure the < and > are trying to be interpreted as part of the expression.

Upvotes: 0

Related Questions