Blog Posts List

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

by Yugolancer

Posted on March 06, 2016



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#


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

T-SQL


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

Read More


The SqlMembershipProvider requires a database schema compatible with schema version 1

by Yugolancer

Posted on October 31, 2016



If you get this error it means that the default values are missing from your database.

To resolve/fix the issue, just execute the following SQL code against your database:


INSERT INTO 
    dbo.aspnet_SchemaVersions 
VALUES
   ('common', 1, 1),
   ('health monitoring', 1, 1),
   ('membership', 1, 1),
   ('personalization', 1, 1),
   ('profile', 1, 1),
   ('role manager', 1, 1);
GO

Read More


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


ASP.NET MVC AJAX request returns an Internal Server Error (FIX)

by Yugolancer

Posted on June 25, 2016



Usually you get this error if you have the data misspelled e.g. say that your ActionResult expects a param named userid while you are sending user (without id)


[HttpPost]
public ActionResult AddComment(string productid, string userid, string comment){
  // add the new comment and return Json response
}


$(".btn-comment").click(function(){
 
            var data = {};
            data.productid = @ViewBag.ProductID;
            data.user = @ViewBag.UserID;
            data.comment = $('#CommentText').val();
 
            $.ajax({
                url: '/Product/AddComment',
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: JSON.stringify(data),
                success: function (result) {
                    alert("The new comment '" + result.comment + "' has been added successfully!");
                },
                error: function (status, ex) {
                    alert("Error Code: Status: " + status.statusText + " Ex: " + ex);
                }
            }); 
        });

meaning it is expecting data.userid and you send data.user :(

As you guess just change the data.user to data.userid and it will work as expected.

HTH

Read More


C# .NET in UNITY 3D

by

Posted on December 12, 2016



Since recently you can use C#.NET to code AR/VR applications using Unity. 

Several AR/VR headset manufactures support Unity as a platform to build apps for their products.

Now you have no excuse to avoid Unity anymore.

Read More


Retrieve data contained in a comma-delimited list of values Warning!

by

Posted on June 03, 2017



Say that you have a comma-separated String containing user IDs like following:

String userIDs = "1,2,5,8,23,67,101";

and that you want to fetch those users without splitting and looping their IDs.

Usually you do use CharIndex to search the expression:

command.CommandText = 
    "SELECT * FROM Users WHERE  CharIndex(CAST(userID AS varchar(8)) + ',', + @userIDs) > 0";
command.Parameters.AddWithValue"@userIDs", userIDs;

However if you execute the above query the last ID will be omitted YEAH it will be indeed because it checks against value + comma which does not match when it comes to 101.

FIX: add the missing comma to the end of the string:

command.Parameters.AddWithValue"@userIDs", userIDs + ",";

Hope this helps someone!

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


Get RadAjaxManager from within User Control

by

Posted on November 24, 2017



ContentPlaceHolder content = 
  this.Page.Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
var ajax = content.FindControl("AjaxMng") as RadAjaxManager;
ajax.Alert("Test");

Read More


Redirect users after n seconds from code-behind

by

Posted on November 27, 2017



C#

// redirect them to login in 4 sec
HtmlMeta meta = new HtmlMeta();
meta.HttpEquiv = "Refresh";
meta.Content = "4;url=/login";
this.Page.Controls.Add(meta);

Read More


Page 1 of 2

Copyright © ASPNETer 2006 - 2016