Blog Posts List

Having a “No image available” if your image URL errors out – RadGrid

by Yugolancer

Posted on January 08, 2016



It often happens that you’ve got no image for all the items in your store.

It could be caused by different reasons like your server where you host the images is temporary down, the third party cannot serve the image at the moment or you just have no appropriate image at the moment. Anyway this is how you should keep your store consistent without those broken image icons that internet browsers add in there.

Usually i do that from code-behind like the following:

VB.NET code:


Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs) Handles RadGrid1.ItemDataBound
        ' set the markup for each item
        If TypeOf e.Item Is GridDataItem Then
            Dim dataItem As GridDataItem = CType(e.Item, GridDataItem)            
 
            Dim thumbnail As Image = CType(dataItem.FindControl("imageThumbnail"), Image)
            If thumbnail IsNot Nothing Then
                Dim imagedbvalue As String = DataBinder.Eval(dataItem.DataItem, "imageThumbnail").ToString
                thumbnail.ImageUrl = imagedbvalue
                ' if the image does not exist we load an image with "photo coming soon" title
                Dim comingsoon As String = "http://" & Request.Url.Host & "/images/ComingSoon.jpg"
                thumbnail.Attributes.Add("onerror", "this.src= '" & comingsoon & "';")
            End If
            ' other code 
       End If
End Sub

C# code:


protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
	// set the markup for each item
	if (e.Item is GridDataItem) {
	        GridDataItem dataItem = (GridDataItem)e.Item;
 
		Image thumbnail = (Image)dataItem.FindControl("imageThumbnail");
		if (thumbnail != null) {
			string imagedbvalue = DataBinder.Eval(dataItem.DataItem, "imageThumbnail").ToString;
			thumbnail.ImageUrl = imagedbvalue;
			// if the image does not exist we load an image with "photo coming soon" title
			string comingsoon = "http://" + Request.Url.Host + "/images/ComingSoon.jpg";
			thumbnail.Attributes.Add("onerror", "this.src= '" + comingsoon + "';");
		}
		// other code 
	}
}

Read More


System.InvalidOperationException – Eval(), XPath(), or Bind()

by Yugolancer

Posted on February 04, 2016



If you get the following error you are about to get the solution either :)

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

Cause:

Usually it happens when you directly call say an Eval() function in the code-behind.

For example, you may have a GridView control with a label inside of the Template column:

<asp: id=" lblDescription " runat=" server " text="< %# Eval('Description') % >"></asp:>

 

Then for some reason you decide to move that to the code-behind AS IS so you say:


Dim lblDescription As Label = CType(e.Item.FindControl("lblDescription"), Label)
lblDescription.Text = Eval("Description").ToString()

Your project will compile but as soon as you try to open the certain Page, an exception ‘System.InvalidOperationException’ will be thrown.

Solution:

Just replace Eval() with DataBinder.Eval() making your code to looks like this:


Dim lblDescription As Label = CType(e.Item.FindControl("lblDescription"), Label)
lblDescription.Text = DataBinder.Eval(dataItem.DataItem, "Description").ToString()

Voila! Your problem has been solved.

Read More


Full Name – get rid of the empty space if middle name is null

by

Posted on June 19, 2017



I see people bother with checking against NULL, using Coalesce, Substring etc. etc. The solution is pretty simple actually. You just need to replace the two empty space by one if the middle name is null or empty e.g.

C#

string fullName =
(reader["FirstName"].ToString() + " " + 
reader["MiddleName"].ToString() + " " + 
reader["LastName"].ToString())
.Replace("  ", " ");

 

T-SQL

REPLACE(FirstName + ' ' + MiddleName + ' ' + LastName + ' ' + Suffix, '  ', ' ') AS FullName

HTH

Read More


Order By Except certain records

by

Posted on September 17, 2017



SELECT
   NameEN
FROM
   Country
ORDER BY
  CASE WHEN NameEN = 'Switzerland' THEN 0 ELSE 1 END, NameEN

Read More


Sql Server - Update Trigger

by

Posted on April 05, 2018



CREATE TRIGGER [dbo].[ESR_INSERT]
   ON  [dbo].[Loan] FOR INSERT AS
UPDATE 
	Loan 
SET 
	ESR = '01' + ' ' + FORMAT(Loan.Id, '00000') + '...'
FROM 
	Loan
JOIN 
	Inserted 
ON 
	Inserted.Id = Loan.Id
WHERE 
	Loan.ESR IS NULL

Read More


Page 1 of 1

Copyright © ASPNETer 2006 - 2016