logo
down
shadow

C# SqlDataReader = null?


C# SqlDataReader = null?

By : Adnan
Date : January 02 2021, 06:48 AM
this will help , You didn't connect your reader to your SQL connection/command?
code :


Share : facebook icon twitter icon
.Net SqlDataReader Item is Null [VB]

.Net SqlDataReader Item is Null [VB]


By : userK
Date : March 29 2020, 07:55 AM
wish helps you IIf always evaluates both the true and false parts - use inline If instead if you want to avoid this:
code :
While dr.Read()
  corporateTable.Rows.Add(New Object() { _
    Convert.ToDateTime(dr("EventBeginDate")) _
    , If(dr("EventEndDate") Is DBNull.Value, Convert.ToDateTime(dr("EventBeginDate")).AddDays(1), Convert.ToDateTime(dr("EventEndDate"))) _
    , Convert.ToString(dr("EventType")) _
    , Convert.ToString(dr("EventDescription")) _
    , Convert.ToString(dr("EventMessage")) _
  })
End While
SQLDataReader Null to datetime

SQLDataReader Null to datetime


By : Kartik Kannao
Date : March 29 2020, 07:55 AM
it fixes the issue Having an issue with casting a datetime from a reader where the value is null. , I suspect you meant:
code :
form._date101 = reader[52] == DBNull.Value ? null : (DateTime?)reader[52];
how to get null values in sqldatareader

how to get null values in sqldatareader


By : kishan
Date : March 29 2020, 07:55 AM
I hope this helps you . You can use the IsDBNull method. Something like that:
code :
int ordinal_so_type=reader.GetOrdinal("so_type");
//...
while(reader.Read()==true)
{
  //reading other columns here
  if (reader.IsDBNull(ordinal_so_type)==false)
  {
    s.type=reader.GetInt32(ordinal_so_type);
  }
  else
  {
    //do whatever you like if the so_type column is null
  }
}
SqlDataReader Best way to check for null values -sqlDataReader.IsDBNull vs DBNull.Value

SqlDataReader Best way to check for null values -sqlDataReader.IsDBNull vs DBNull.Value


By : Capo Benn
Date : March 29 2020, 07:55 AM
I wish this help you I would not get too caught up in the which method is better, because both work and I have used both in code before.
For instance, here is a utility function I dug up from one of my old projects:
code :
/// <summary>
/// Helper class for SqlDataReader, which allows for the calling code to retrieve a value in a generic fashion.
/// </summary>
public static class SqlReaderHelper
{
    private static bool IsNullableType(Type theValueType)
    {
        return (theValueType.IsGenericType && theValueType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)));
    }

    /// <summary>
    /// Returns the value, of type T, from the SqlDataReader, accounting for both generic and non-generic types.
    /// </summary>
    /// <typeparam name="T">T, type applied</typeparam>
    /// <param name="theReader">The SqlDataReader object that queried the database</param>
    /// <param name="theColumnName">The column of data to retrieve a value from</param>
    /// <returns>T, type applied; default value of type if database value is null</returns>
    public static T GetValue<T>(this SqlDataReader theReader, string theColumnName)
    {
        // Read the value out of the reader by string (column name); returns object
        object theValue = theReader[theColumnName];

        // Cast to the generic type applied to this method (i.e. int?)
        Type theValueType = typeof(T);

        // Check for null value from the database
        if (DBNull.Value != theValue)
        {
            // We have a null, do we have a nullable type for T?
            if (!IsNullableType(theValueType))
            {
                // No, this is not a nullable type so just change the value's type from object to T
                return (T)Convert.ChangeType(theValue, theValueType);
            }
            else
            {
                // Yes, this is a nullable type so change the value's type from object to the underlying type of T
                NullableConverter theNullableConverter = new NullableConverter(theValueType);

                return (T)Convert.ChangeType(theValue, theNullableConverter.UnderlyingType);
            }
        }

        // The value was null in the database, so return the default value for T; this will vary based on what T is (i.e. int has a default of 0)
        return default(T);
    }
}
yourSqlReaderObject.GetValue<int?>("SOME_ID_COLUMN");
yourSqlReaderObject.GetValue<string>("SOME_VALUE_COLUMN");
SqlDataReader returns null

SqlDataReader returns null


By : Meganathan Develop
Date : March 29 2020, 07:55 AM
To fix the issue you can do You are adding the same four columns to the DataTable each time you load a row.
Related Posts Related Posts :
  • Looking for a .Net ORM
  • Cost of SortedDictionary.Count
  • All possible combinations of n items selected randomly from a set of x items (algorithm)
  • Displaying process percentage on DOS screen
  • C# Importing Large Volume of Data from CSV to Database
  • c# control location precision
  • Printing to Zebra printer gives faint and blurry images
  • How to avoid stepping into dll code file?
  • Programmaticaly prevent Vista desktop search (WDS) from indexing pst files placed on mapped network drives
  • Is there anyway to reduce IsolatedStorage capacity in Silverlight?
  • C#: How to print a unicode string to console?
  • Frequency Table with Random Values
  • My Visual Studio 2008 web application keeps throwing a .Net error when I first run it, but refreshing fixes it
  • TCP Connection Creation and Closing Event Hooking
  • Find changed properties of a class
  • C#: Object with custom marshaller not containing data after PInvoke call
  • Version Control System with API. Need to get metrics
  • Performance comparison of Dictionaries
  • How to get process name and title of the top window on Windows / C#
  • How to save the ListView contents to a text file?
  • Why not .NET-style delegates rather than closures in Java?
  • Get CLSID by PIA interface Type
  • Visual Studio 2008 C# - Recover solution from application manifest possible?
  • Is a good practice to initialize private data members from within ctor by calling the associated properties instead of t
  • Inserting into a bitstream
  • How do you write byte[] array using log4.net
  • Clickonce + .NET client profile 4 framework + offline
  • Show form that showDialog from mainForm won't block it, but closing mainForm will close it
  • Developing a GPS car tracking system
  • dynamic log4net appender name?
  • Visual studio formatting issue
  • IndexOutOfRangeException when a stream is a multiple of the buffer size
  • How to deal with calculated values with Dependency Properties on a custom WPF control
  • counting from textbox,asp.net
  • Threading best practice when using SFTP in C#
  • how to load PDF file in website page using C#
  • Parent child class relationship design pattern
  • MessageQueue.BeginReceive() null ref error - c#
  • A C# collection, which behaves like C++ set or priority_queue?
  • Animating WPF buttons from inside a style
  • WPF Grid : GridSplitter event
  • Server 2008 Task Scheduler Mapped Drive Access C#
  • Cast problem with LINQ
  • Combo box Item source = ObservableCollection & I need a '-None-' dummy entry at the top
  • Which GUI control is suitable for this purpose?
  • Trying to access App.config file for mail settings but fails to work
  • Invoke PowerShell script from c# in runspace and get line number where error occured
  • How to show html contents with a RichTextBox?
  • FTP monitoring and downloading of new files
  • MEF + Plug-In not updating
  • Efficient handling of super wide, but not so tall, bitmap?
  • .NET 3.0 Unit Testing getting System.MethodAccessException calling .NET 2.0
  • Web Services vs Persistent Sockets
  • How do I dynamically reference incremented properties in C#?
  • Windows Installer (C#) error code 2869
  • Exception "The operation is not valid for the state of the transaction" using TransactionScope
  • Tools for listing database accesses from .NET code
  • Excel VSTO set selection
  • Creating a subscription based website in ASP.NET
  • Merging Resources Dictionaries
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk