Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DBNull异常问题_C#_Sql - Fatal编程技术网

C# DBNull异常问题

C# DBNull异常问题,c#,sql,C#,Sql,我今天的代码有问题。每当我使用此方法将产品添加到本地数据库时,它都会抛出一个异常,该异常读取“对象不能从DBNull转换为其他类型”。在做了一些研究之后,我相信我的问题在于AddProduct方法中的insertCmd参数,但我似乎无法准确地找出导致它的原因。我创建的程序仍然可以正常运行(它添加了产品,然后可以成功选择),但是在我添加产品时,它似乎一直抛出这个异常 有人能提供我的代码中可能存在的问题的见解吗?如果您需要更多信息,请随时询问。谢谢你的时间和帮助 public static bool

我今天的代码有问题。每当我使用此方法将产品添加到本地数据库时,它都会抛出一个异常,该异常读取“对象不能从DBNull转换为其他类型”。在做了一些研究之后,我相信我的问题在于AddProduct方法中的insertCmd参数,但我似乎无法准确地找出导致它的原因。我创建的程序仍然可以正常运行(它添加了产品,然后可以成功选择),但是在我添加产品时,它似乎一直抛出这个异常

有人能提供我的代码中可能存在的问题的见解吗?如果您需要更多信息,请随时询问。谢谢你的时间和帮助

public static bool AddProduct(Product product)
{
SqlConnection connect = MMABooksDB.GetConnection();
string insert = "INSERT Products " + "(ProductCode, Description, UnitPrice) " + "VALUES (@code, @description, @price)";
SqlCommand insertCmd = new SqlCommand(insert, connect);
insertCmd.Parameters.AddWithValue("@code", product.code);
insertCmd.Parameters.AddWithValue("@description", product.description);
insertCmd.Parameters.AddWithValue("@price", product.price);
     try
        {
            connect.Open();
            insertCmd.ExecuteNonQuery();
            string selectStatement =
                "SELECT IDENT_CURRENT('Products') FROM Products";
            SqlCommand selectCommand = new SqlCommand(selectStatement, connect);
            int ProductCode = Convert.ToInt32(selectCommand.ExecuteScalar());
         return true;
        }
        catch (SqlException ex)
        {
            throw ex;
        }
        finally
        {
            connect.Close();
        }
}
这是产品类

public class Product
{
    public string code;
    public string description;
    public decimal price;

    public Product() { }

    public Product(string code, string description, decimal price)
    {
        this.Code = code;
        this.Description = description;
        this.Price = price;
    }

    public string Code
    {
        get
        {
            return code;
        }
        set
        {
            code = value;
        }
    }

    public string Description
    {
        get
        {
            return description;
        }
        set
        {
            description = value;
        }
    }

    public decimal Price
    {
        get
        {
            return price;
        }
        set
        {
            price = value;
        }
    }

    public string GetDisplayText()
    {
        return code + ", " + price.ToString("c") + ", " + description;
    }

    public string GetDisplayText(string sep)
    {
        return code + sep + price.ToString("c") + sep + description;
    }
}

如果产品被添加,我会假设无法转换来自
int ProductCode=convert.ToInt32(selectCommand.ExecuteScalar())我将添加一个检查,以查看scaler是否返回null

调试代码时,异常发生在哪一行?谢谢您的帮助!这就解决了问题。现在我有一个新问题要解决!我喜欢编程。。。英雄联盟
int ProductCode;  //declare variable
object dbProductID = selectCommand.ExecuteScalar(); //get the result into object
if(dbProductID !=null || dbProductID != DBNull.Value) //check if it is null
   ProductCode = 0; //assign some default value here if it has no productid
else
   ProductCode = Convert.ToInt32(dbProductID);//if its has productid cast it into int