sp giving output but SqlDataReader not

女生的网名这么多〃 提交于 2019-12-08 11:59:59

问题


I am using SP in C# project to retrieve the output using SqlDataReader. Below is the code.

 public List<LMTUsage> GetCompanyID(string userID, int roleId, String Organisation, String BusinessArea)
    {
        List<LMTUsage> objLMT = new List<LMTUsage>();
        LMTUsage _oELMTUsage;

        SqlConnection oCon = new SqlConnection(ConfigurationManager.ConnectionStrings["LMTConnectionString"].ConnectionString);
        oCon.Open();

        try
        {
            using (SqlCommand _oCmd = new SqlCommand())
            {
                _oCmd.Connection = oCon;

                _oCmd.CommandType = CommandType.StoredProcedure;
                _oCmd.CommandText = "[SC_GetDropdownValues]";

                _oCmd.Parameters.Add(new SqlParameter("@UserId", userID));
                _oCmd.Parameters.Add(new SqlParameter("@RoleId", roleId));

                if (Organisation == "")
                    _oCmd.Parameters.Add(new SqlParameter("@Organisation", DBNull.Value));
                else
                    _oCmd.Parameters.Add(new SqlParameter("@Organisation", Organisation));

                if (BusinessArea == "")
                    _oCmd.Parameters.Add(new SqlParameter("@BusinessArea", DBNull.Value));
                else
                    _oCmd.Parameters.Add(new SqlParameter("@BusinessArea", BusinessArea));

                _oCmd.Parameters.Add(new SqlParameter("@Type", 3));


                using (SqlDataReader _oRdr = _oCmd.ExecuteReader())
                {
                   // _oRdr.Close();
                    while (_oRdr.Read())
                    {
                        _oELMTUsage = new LMTUsage();
                        _oELMTUsage.Company = _oRdr["Company"].ToString();

                        objLMT.Add(_oELMTUsage);
                    }
                    _oRdr.Close();
                }
            }
            return objLMT;
        }
        catch (Exception Ex)
        {
            throw Ex;
        }
        //finally
        //{
        //    oCon.Close();
        //    oCon.Dispose();
        //}
    }

It is very Simple SP with the select statement. SP return the output when executed from SQL 2014 but when implemented in the above method it doesn't return any output. Below is the screen for reference.

Please guide.


回答1:


Please check values in parameters. And use Dataset with fill method to retrive data from sp instead of using ExecuteReader().




回答2:


Unless your stored procedure is specifically named [SC_GetDropdownValues] (including the square brackets), you don't need to define square brackets in your SqlCommand's CommandText. Try:

_oCmd.CommandText = "SC_GetDropdownValues";


来源:https://stackoverflow.com/questions/51167794/sp-giving-output-but-sqldatareader-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!