locked
SqlDataReader is not returning any records RRS feed

  • Question

  • User-305496339 posted

    Hi Friends,

    I am using the sqldatareader in MVC to return records and populate a model for my view. For some reason my code does not execute the while loop. I was wondering if someone could help me with a problem they see maybe in how I have structured the  connection, command, and reader object and why the reader is not returning any records. Thanks !!! 

    public ActionResult ShowSSN(ShowSSN.Models.ShowSSNModel model)
            {
    
    
               
    
                string conn = "";
                conn = ConfigurationManager.ConnectionStrings["Dalton"].ToString();
               
    
                  
    
    
             
                using (SqlConnection sqlConnection = new SqlConnection(conn))
                {
                   using (SqlCommand sqlCommand = new SqlCommand("[dbo].[spGetSSN]", sqlConnection))
                    {
                        sqlCommand.CommandType = CommandType.StoredProcedure;
                        sqlCommand.Parameters.Add(new SqlParameter("@ssn", model.SSN));
    
                        //using (SqlDataReader sqlDataReader =  new sqlCommand.ExecuteReader();
                        {
                            sqlConnection.Open();
                           using (SqlDataReader rdr = sqlCommand.ExecuteReader())
                            {
                                while (rdr.Read())
                                {
                        
                                 var value= Convert.ToInt32(rdr["DataBaseName"]);
                                 //var student = new List<SSNs>();
                                 // student. = rdr["numberofssn"];
                                 ////student.LastName = rdr["LastName"];
                                  ////student.Class = rdr["Class"];
                                 //model.Add(student);
                                  }
                            }
                           
                        }
                    }

    Thursday, April 16, 2020 5:16 PM

All replies

  • User1120430333 posted

    Bad parm maybe. Did you take the parm after stopping on a debug breakpoint and used it in executing the sproc using SSMS?

    Thursday, April 16, 2020 6:48 PM
  • User-305496339 posted

    Thanks for your help !!! I apologize for the oversight. Yes, you are correct the parameter value was truncated and I am not sending the entire string. Let me make the changes and see if it will fix the problem. Thank You ver much. 

    Thursday, April 16, 2020 7:05 PM