Check Null value in ExecuteScalar in asp.net c#

preview_player
Показать описание
As we know that when you try to find the result of a single column, we can use executeScalar which is much faster process of data retrieval. But its not a good option for null value records or no result queries. So using this method you can use to check if the result obtained is null or if it has any value.
#ExecuteScalar #nullvalueinExecuteScalar #csharp
You can also use isnull(sum(columnName),0) in your query to show 0 when there is null value. This is very important as it can throw exception in your c# application.
Рекомендации по теме
Комментарии
Автор

Brilliant, please zoom in to show the code bigger font when watching in a 5" cell phone.

GFactFinder
Автор

cmd = new SqlCommand("select ProviderName from tblProvider where Userid='"+txtIntroducerid.Text+"'", cons);
cons.Open();
object sd = cmd.ExecuteScalar();
if (sd != null)
{
lblReferalid.Text = sd.ToString();
}
else
{
lblReferalid.Text = "null";
}
cons.Close();

fired_developer