Question

I am using sql store procedure to get the list of data, but currently I am getting an error as An unhandled exception of type 'System.StackOverflowException' occurred in System.Data.dll, This program should be able to get data for today's date. This is my modal:

And this my code;

   public List<ClassName> GetStaffs()
    {
         List<ClassName> staff = new List<ClassName>();

        SqlConnection connection = new SqlConnection(ConfigurationManager.etc...);

        SqlCommand mycomm = new SqlCommand("ProcedureName",connection);
        mycomm.CommandType = CommandType.StoredProcedure;
        mycomm.Parameters.AddWithValue("dates",DateTime.Today);
        SqlDataReader reader;
        connection.Open();
        reader = mycomm.ExecuteReader();
        connection.Close();
        return staff;
     }

In this line I get error reader = mycomm.ExecuteReader(); I am also not sure if the code will get me the list of staff data I requested. Can someone guide me to the right direction. This is the example I have used link

Store procedure in SQL 2008

    USE databaseName
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[ProcedureName] @dates Datetime   

AS
BEGIN
SET NOCOUNT ON;

SELECT ID, fName, sName, dates,   
COUNT(CASE TotalStaff WHEN 0 THEN 0 ELSE NULL END) AS TotalStaffs, 
FROM dbo.DatabaseName WHERE dates>= @dates  
GROUP BY ID, fName, sName, dates 

END

UPDATE I manage to solve the error but i am getting no data from my store procedure.

Was it helpful?

Solution

Okay in your SP i have ignore the time portion in where condition by casting "dates" column in Date. So now your SP look like this -

ALTER PROCEDURE [dbo].[ProcedureName] 
@dates Datetime   

AS
BEGIN
SET NOCOUNT ON;

SELECT ID, fName, sName, dates,   
COUNT(CASE TotalStaff WHEN 0 THEN 0 ELSE NULL END) AS TotalStaffs, 
FROM dbo.DatabaseName WHERE CAST(dates As Date) >= CAST(@dates  As Date)
GROUP BY ID, fName, sName, dates 

END

and in your c# code, you have not added object in your list. So here is the modified code.

public List<ClassName> GetStaffs()
{
            List<ClassName> staff = new List<ClassName>();

            SqlConnection connection = new SqlConnection(ConfigurationManager.etc...);

            SqlCommand mycomm = new SqlCommand("ProcedureName",connection);
            mycomm.CommandType = CommandType.StoredProcedure;
            mycomm.Parameters.AddWithValue("dates",DateTime.Today);
            SqlDataReader reader;
            connection.Open();
            reader = mycomm.ExecuteReader();
            while (reader.Read())
            {
                staff.Add(new ClassName { fName = Convert.ToString(reader["fName"]), sName = Convert.ToString(reader["sName"]), ..so-on });
            }
            connection.Close();
            return staff;
}

Hope this will help you...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top