需要处理NULL异常

时间:2014-01-17 05:13:11

标签: c# asp.net null

private int MyTimeValue()
{
    int Value = 0;
    string connectionString = GetConfigurationSettingValue("ConnectionString");
    int Days = 120;

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        using (SqlCommand cmd = new SqlCommand("select [dbo].[My_function] (@Span)", connection))
        {
            cmd.CommandType = System.Data.CommandType.Text;

            cmd.Parameters.Add(new SqlParameter("@Span", Days));

            Value = Convert.ToInt32(cmd.ExecuteScalar());

        }
    }

    return (Value);
}

我正在Object cannot be cast from DBNull to other types.

如何使用trycatch

处理null异常

请帮我解释一下语法。

5 个答案:

答案 0 :(得分:3)

您可以在转换前验证

object result = cmd.ExecuteScalar();
if(result != null)
    Value = Convert.ToInt32(result);

并添加如下参数

 cmd.Parameters.AddWithValue("@Span", Days);

作为旁注,行cmd.CommandType = System.Data.CommandType.Text;是没有必要的。默认命令类型是文本

答案 1 :(得分:2)

我猜错误就在这里:

Value = Convert.ToInt32(cmd.ExecuteScalar());

试试这个:

var result = cmd.ExecuteScalar();
Value = result == DBNull.Value ? 0 : Convert.ToInt32(result);

注意:你的命令字符串对我来说也不对。

答案 2 :(得分:2)

您需要先检查ExecuteScalar返回的值是否为null,以便在尝试将对象强制转换为Int32时不会发生异常。

private int MyTimeValue()
    {
        int Value = 0;
        string connectionString = GetConfigurationSettingValue("ConnectionString");
        int Days = 120;

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            using (SqlCommand cmd = new SqlCommand("select [dbo].[My_function] (@Span)", connection))
            {
                cmd.CommandType = System.Data.CommandType.Text;

                cmd.Parameters.Add(new SqlParameter("@Span", Days));
                object o=cmd.ExecuteScalar();
                if(o!=null)
                   Value = Convert.ToInt32(o);

            }
        }

        return (Value);

    }

答案 3 :(得分:1)

这可以通过两种方式完成。

案例1:处理特定异常

private int MyTimeValue()
{
    int Value = 0;
    try
    {      
       string connectionString = GetConfigurationSettingValue("ConnectionString");
       int Days = 120;

       using (SqlConnection connection = new SqlConnection(connectionString))
       {
           connection.Open();
           using (SqlCommand cmd = new SqlCommand("select [dbo].[My_function] (@Span)", connection))
           {
               cmd.CommandType = System.Data.CommandType.Text;

               cmd.Parameters.Add(new SqlParameter("@Span", Days));

               Value = Convert.ToInt32(cmd.ExecuteScalar());

           }
       }
     catch(NullReferenceException ex)
     {
        \\Handle Exception
     }

     return (Value);

}

案例2:检查Null的变量以及其他意外异常的Catch General Exception

    private int MyTimeValue()
{
    int Value = 0;
    try
    {      
       string connectionString = GetConfigurationSettingValue("ConnectionString");
       int Days = 120;

       using (SqlConnection connection = new SqlConnection(connectionString))
       {
           connection.Open();
           using (SqlCommand cmd = new SqlCommand("select [dbo].[My_function] (@Span)", connection))
           {
               cmd.CommandType = System.Data.CommandType.Text;

               cmd.Parameters.Add(new SqlParameter("@Span", Days));
               var result = cmd.ExecuteScalar();
               if(result !=null)
                    Value = Convert.ToInt32(result );

           }
       }
     catch(Exception ex)
     {
        \\For handling unexpected exceptions
     }   
     return (Value);
}

答案 4 :(得分:1)

试试这个

          Value = cmd.ExecuteScalar();    
          if (((Value != null) || (Value != DBNull.Value)))
          {
          Value =(int)Value ;
          }