Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9111929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:36:59+00:00 2026-06-17T03:36:59+00:00

There is an exception message when I run the website Unable to cast object

  • 0

There is an exception message when I run the website

” Unable to cast object of type ‘System.DBNull’ to type
‘System.String’.”

emereging from the ‘value’ part of 'return (string)c.Parameters["@vat"].Value;‘ in the below code. I checked the stored procedure to see if it executes the query, it does and its output is a string as well. Can Anyone help..??

namespace CamOnlineAccess
{
  public class Utilities
  {
    public SqlConnection GetConnection()
    {
        SqlConnection c = new SqlConnection();
        c.ConnectionString = ConfigurationManager.ConnectionStrings["CamOnlineConnectionString"].ConnectionString;
        return c;
    }

    public SqlCommand GetCommandSP(string SPName)
    {
        SqlCommand c = new SqlCommand();
        c.Connection = GetConnection();
        c.CommandType = CommandType.StoredProcedure;
        c.CommandText = SPName;
        c.CommandTimeout = 150;

        return c;
    }
  }
}

public string ViewUserVat(string code)
{
    CamOnlineAccess.Utilities u = new CamOnlineAccess.Utilities();
    SqlCommand c = u.GetCommandSP("dbo.ViewUserVat");

    c.Parameters.AddRange(new System.Data.SqlClient.SqlParameter[] {
        new System.Data.SqlClient.SqlParameter("@code", System.Data.SqlDbType.VarChar,50),
            new System.Data.SqlClient.SqlParameter("@vat",SqlDbType.VarChar, 50, System.Data.ParameterDirection.Output, false, ((byte)(0)), ((byte)(0)), "", System.Data.DataRowVersion.Current, null)});
    c.Parameters["@code"].Value = code;
    c.Connection.Open();
    c.ExecuteScalar();//because we have output parameters
    c.Connection.Close();

    return (string)c.Parameters["@vat"].Value;
 }

STRORED PROCEDURE

 ALTER PROCEDURE [dbo].[ViewUserVat] 
-- Add the parameters for the stored procedure here
@code varchar,
@vat varchar(50) output

    AS
SELECT top 1 @vat=vattable from dbo.portfolio 
where owner=@code
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T03:37:01+00:00Added an answer on June 17, 2026 at 3:37 am

    You return directly the parameter value assuming that your stored procedure will always find a record for the parameter @code and the column vattable is never null.

    This is not the case as you can see, and you get a DBNull in return.
    You should change your code to

     return (c.Parameters["@vat"].Value == DBNull.Value ? 
                  null : 
                  c.Parameters["@vat"].Value.ToString());
    

    Of course, assuming a null is an allowed/expected return value for the rest of your code.
    Otherwise, you can replace the null with another acceptable value (eg string.Empty)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to filter exceptions in elma using the exception message? Examples:
I get an exception with the message: There is an error in XML document.
Is there some sort of exception in Java to catch an invalid Date object?
There are some articles concluding never throw an exception from a destructor, and std::uncaught_exception()
Are there any memory leaks when I throw an exception from a constructor like
We have code that is producing the following unhandled exception: Error Message: System.Reflection.TargetParameterCountException: Parameter
We have been trying to analyse this exception: Message: Error: Object reference not set
Can I avoid the open DataReader exception (There is already an open DataReader associated
If there's an exception in a Rails application, one gets an error page with
I have problem with lazy loading in many-to-many. There is no exception and there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.