Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

C# datareadeer return null

// ****** Use MySqlDataReader instead of SqlReader if you are using MySQL *******

//You need to check for IsDBNull:
//That's your only reliable way to detect and handle this situation.
//I wrapped those things into extension methods and tend to return a default value 
//if the column is indeed null:

public static string SafeGetString(this SqlDataReader reader, int colIndex)
{
   if(!reader.IsDBNull(colIndex))
       return reader.GetString(colIndex);
   return string.Empty;
}

//Now you can call it like this:

employee.FirstName = SqlReader.SafeGetString(indexFirstName);

// and you'll never have to worry about an exception or a null value again.
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# distinct array of objects by values 
Csharp :: update listbox using class c# 
Csharp :: get percentage c# 
Csharp :: multiplication of long number 
Csharp :: orElseThrow 
Csharp :: how to write a list to csv c# 
Csharp :: asp.net 5 iis HTTP Error 500.19 - Internal Server Error 
Csharp :: c# datagridview select row right click 
Csharp :: c# file watcher 
Csharp :: array object to datatable c# 
Csharp :: limiting the amount of decimal places c# 
Csharp :: c# get function name 
Csharp :: c# template 
Csharp :: c# switch statements 
Csharp :: c# delete files in directory and subdirectories 
Csharp :: c# handle dbnull value 
Csharp :: msbuild publish to folder command line .net 
Csharp :: c# loop through dictionary 
Csharp :: what is void onmousedown() 
Csharp :: meaning of ??= in c# 
Csharp :: c# pi 
Csharp :: Map Range Clamped unity 
Csharp :: c# loop through repeater items 
Csharp :: C# extract all of a property from a list of objcets 
Csharp :: linq find object from id 
Csharp :: ihttpactionresult to object c# 
Csharp :: window height in C# forms 
Csharp :: c# get all letters 
Csharp :: enum in c# 
Csharp :: vb.net center form in screen 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =