Error: Object cannot be cast from DBNull to other types.


Error: Object cannot be cast from DBNull to other types.

Description: An invalid cast exception, when trying to convert a nonexistent value of table column to other types.

Solution: Use DBNull.Value to validate the column having a valid value or not.


if (!DBNull.Value.Equals(row[FirstName]))
{
  return Convert.ToString(row[FirstName]);
}

Error: Object reference not set to an instance of an object.


Error: Object reference not set to an instance of an object.

Description: This error throws when a program trying to access a member fields, or function types, on an object reference that points to null. Using NullReferenceException, will throw exception when the variable is null.

Solution: Put a null check on the object and also implement Exception Handling techniques to catch the error.

Error: System.Data.SqlClient.SqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.


Error: System.Data.SqlClient.SqlException: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.

Description: There can be many reason.

  1. The request to SQL Server or query is taking a long time to execute.
  2. The result is returning large amount of records.
  3. Slow network connection issue.
  4. SQL Command connection timeout is not configured properly.
  5. Badly made SQL statements

Solution:

  1. Check the network connectivity is up and running.
  2. Check the SQL Statements, if required tuned and optimize it.
  3. Check the result size.
  4. Configure Connection Timeout property, by default it’s 15 seconds.

Error: ‘System.Array’ does not contain a definition for ‘FirstOrDefault’ and no extension method ‘FirstOrDefault’ accepting a first argument of type ‘System.Array’ could be found


Error: ‘System.Array’ does not contain a definition for ‘FirstOrDefault’ and no extension method ‘FirstOrDefault’ accepting a first argument of type ‘System.Array’ could be found (are you missing a using directive or an assembly reference?)

Description: When we trying to use FirstOrDefault to an object this error pop’s up. The reason behind is that we not providing the correct references.

Solution: Add reference, using System.Linq;

Error: ‘Object.get’ must declare a body because it is not marked abstract or extern. Automatically implemented properties must define both get and set accessors.


Error: ‘Object.get’ must declare a body because it is not marked abstract or extern. Automatically implemented properties must define both get and set accessors.

Description: This error occurs when you have a property with only get accessor or an Automatic property with only get. The reason behind the auto generated properties with get can only access the value but cannot ever set the property value because we have no access to the internal storage of property.

Example: public static string StringNull { get; }

Solution: By defining a set accessor this error can be resolved.

Example: public static string StringNull { get; private set; }

Here private is the access modifier.

Error: SQLLite error, insufficient parameters supplied to the command.


Error:  SQLLite error, insufficient parameters supplied to the command.

DescriptionThis error occurs when you failed to provide or supply all the input output parameters to the command text or a stored procedure.

For example:

using (SQLiteCommand cmd = new SQLiteCommand("update usermaster set emailid = @emailid, @phone = @phone where userid = @userid", con))
{
  cmd.Parameters.Add(new SqliteParameter("@emailid", "test@in.com"));
  cmd.Parameters.Add(new SqliteParameter("@userid", 1));
}

Above command having three input parameters but we are supplying only two @emailid and @userid. During runtime, code will through above error.

Solution: Simple, just add the missing parameter to the command.

cmd.Parameters.Add(new SqliteParameter("@phone", 1));

Note: Also remember not to have any extra spaces, that will again throw above error. Avoid,

cmd.Parameters.Add(new SqliteParameter(" @phone", 1));

Error: Project <Name> could not be opened because the Visual C# 2012 compiler could not be created.


Error: Project ‘WebApplication1’ could not be opened because the Visual C# 2012 compiler could not be created. Error creating instance of managed object ‘Microsoft.VisualStudio.CSharp.Services.Language.ManagedObjectFactory’ (Error code is 0x80131513).

Description: Facing this compiler issue, while creating a new Web Application through VS 2012.

Solution

1. Open exe, VS2012 x86 Native Tools Command Prompt (Run as administrator).

Path: C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Visual Studio 2012\Visual Studio Tools

2. Then run gacutil /u Microsoft.VisualStudio.CSharp.Services.Language.Interop

This will successfully uninstall the service from GAC, which was causing the problem.

Open the VS 12 again and start creating projects…..