获取空引用异常调用存储过程

获取空引用异常调用存储过程

本文介绍了获取空引用异常调用存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行code产生的DataTable。从这个数据表,我gleen国家和放大器; CRID。我用这些信息来运行code,以获得PRID。问题是,我每次运行这个时候,我得到一个空引用异常。我试着运行code之前宣布INT PRID = 0;然而,然后,我每次0作为我的回答结束了。我已经使用相同的参数执行code在2008年的SQLServer,我也得到正确的结果。结果
我无法确定为什么这code未正常运行。

 公众诠释GetPRID(INT RRID,串州,INT PCID)
    {
        尝试
        {            SQLCON =新的SqlConnection(的connectionString);
            SQLCON.Open();
            SQLCMD =新的SqlCommand(spGetPRID,SQLCON);
            SQLCmd.CommandType = CommandType.StoredProcedure;
            SQLCmd.Parameters.Add(@ RRID,SqlDbType.Int).value的= RRID;
            SQLCmd.Parameters.Add(@国家,SqlDbType.VarChar).value的=状态;
            SQLCmd.Parameters.Add(@ PCID,SqlDbType.Int).value的= PCID;            INT PRID =(Int32)已SQLCmd.ExecuteScalar();
            返回PRID;
        }
        赶上(异常前)
        {
            HttpContext.Current.Response.Redirect(〜/ ErrorRedirect.aspx?+ ex.Message,FALSE);
            返回0;
        }
        最后
        {
            SQLCON.Close();
        }
    }


解决方案

我发现这个问题。这是不是在我的C#code - 这是SQL本身。我宣布@PRID为int,并要求其返回@PRID。

在我删除了这一点,它工作得很好。感谢大家的贡献。

I run code that produces a datatable. From this datatable, I gleen the State & CRID. I use these pieces of information to run code to get the PRID. Problem is, every time I run this, I get a Null Reference Exception. I've tried declaring int PRID = 0 before running the code; however, I then end up with 0 as my answer every time. I've executed the code in SQLServer 2008 using the same parameters, and I get the correct result.
I am unable to determine why this code is not running correctly.

    public int GetPRID(int RRID, string State, int PCID)
    {
        try
        {

            SQLCON = new SqlConnection(connectionString);
            SQLCON.Open();
            SQLCmd = new SqlCommand("spGetPRID", SQLCON);
            SQLCmd.CommandType = CommandType.StoredProcedure;
            SQLCmd.Parameters.Add("@RRID", SqlDbType.Int).Value = RRID;
            SQLCmd.Parameters.Add("@State", SqlDbType.VarChar).Value = State;
            SQLCmd.Parameters.Add("@PCID", SqlDbType.Int).Value = PCID;

            int PRID = (Int32)SQLCmd.ExecuteScalar();
            return PRID;
        }
        catch(Exception ex)
        {
            HttpContext.Current.Response.Redirect("~/ErrorRedirect.aspx?" + ex.Message, false);
            return 0;
        }
        finally
        {
            SQLCON.Close();
        }
    }
解决方案

I found the issue. It wasn't in my C# code - it was within the SQL itself. I was declaring @PRID as int, and asking it to return @PRID.

Once I removed this, it worked fine. Thank you all for your contributions.

这篇关于获取空引用异常调用存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 13:33