0

I am running code to fill a dataset from a SQL Server table, but for some reason it is not filling the datatable. I have run the stored procedure with the values when stepping through the code and it returns rows. I have checked via SQL Server Profiler what command runs and then executed the same command through SQL Server Management Studio and it returns rows, but for some reason in Visual Studio it returns no rows, yet it is not returning any exception.

The code is :

try
{
    using (SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["Intranet"].ConnectionString))
    {
        switch (command)
        {
            case "radiobutton":
                string school = ParameterString[0];

                using (SqlDataAdapter da = new SqlDataAdapter(GET_TEMPLATE_NAMES, cnn))
                {
                    DataTable dt = new DataTable();

                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    da.SelectCommand.Parameters.AddWithValue("@TemplateforSchool", school);

                    cnn.Open();
                    da.Fill(dt);

                    lstTemplates.DataSource = dt;
                    lstTemplates.DataBind();
                }
                break;

            case "listbox":  // If the listbox triggers the callback then update the grid
                switch (radSchools.SelectedItem.Text)
                {
                    case "Junior School":
                        StartYear = 1;
                        EndYear = 5;
                        break;

                    case "Middle School":
                        StartYear = 6;
                        EndYear = 8;
                        break;

                    case "Senior School":
                        StartYear = 9;
                        EndYear = 12;
                        break;
                }

                string TemplateName = ParameterString[0];
                int DetentionCount = Convert.ToInt16(ParameterString[1]);
                string DetentionType = ParameterString[2];

                using (SqlDataAdapter da = new SqlDataAdapter(GET_CAREGIVER_EMAIL_LIST, cnn))
                {
                    DataTable dt = new DataTable();

                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    da.SelectCommand.Parameters.Add(new SqlParameter
                            {
                                ParameterName = "@DetentionType",
                                SqlDbType = SqlDbType.VarChar,
                                Value= DetentionType
                            });
                    da.SelectCommand.Parameters.Add(new SqlParameter
                            {
                                ParameterName="@DetentionCounter",
                                SqlDbType=SqlDbType.Int,
                                Value=DetentionCount
                            });
                    da.SelectCommand.Parameters.Add(new SqlParameter
                            {
                                ParameterName = "@StartYear",
                                SqlDbType = SqlDbType.Int,
                                Value = StartYear
                            });
                    da.SelectCommand.Parameters.Add(new SqlParameter
                            {
                                ParameterName = "@EndYear",
                                SqlDbType = SqlDbType.Int,
                                Value = EndYear
                            });

                  cnn.Open();

                  da.Fill(dt);

                  gdvCaregiverEmailList.DataSource = dt;
                  gdvCaregiverEmailList.DataBind();
             }
             break;
         }
     }
 }
 catch(Exception ex)
 {
 }

The problem is in the listbox: section of the switch statement. The SqlDataAdapter works fine in the radio button section of the switch.

I am not sure what I am missing so any help would be much appreciated.

I have checked SqlDataAdapter does not fill DataSet and also SqlDataAdapter not filling DataTable but the first was not relevant to my situation and the second had no accepted answer that I could find.

The SQL Server stored procedure that I am calling is

ALTER PROCEDURE [dbo].[GetCaregiverEmailList]
    @DetentionType CHAR(2),
    @DetentionCounter INT,
    @StartYear INT,
    @EndYear INT
AS
BEGIN
    SELECT DISTINCT
        sdc.StudentCode, s.Student#, s.GIVEN_NAME, s.SURNAME,
        sdc.DetentionCount, sdc.DetentionType, 
        s.GIVEN_NAME + ' ' + s.SURNAME AS FullName,
        LTRIM(s.CURRENT_YEAR) AS CurrentYear,
        i.EMAIL_HOME, RTRIM(i.SALUTATION) AS EmailTitle,  
        ax.CORR_PREFERENCE, ar.CAREGIVER_NO
    FROM  
        StudentDetentionCounter sdc
    LEFT JOIN
        [PCSchool].[dbo].[Student] s ON sdc.StudentCode = s.STUDENT#
    INNER JOIN
        [PCSchool].[dbo].[ALUMREL] ar ON sdc.StudentCode = ar.CHILD#
    LEFT JOIN 
        [PCSchool].[dbo].[IDENTITY] i ON ar.PARENT# = i.[MEMBER#]
    LEFT JOIN
        [PCSchool].[dbo].[ALUMREL_EX] ax ON ar.parent# = ax.PARENT#
    WHERE
        (ar.PARENT# <> ar.FAMILY_HASH) 
        AND ar.EN_EMAIL IN ('I','A')
        AND (ax.CORR_PREFERENCE = 1)
        AND (sdc.DetentionCount = @DetentionCounter 
             AND sdc.DetentionType = @DetentionType)
        AND (CONVERT(INT, (LTRIM(s.CURRENT_YEAR))) BETWEEN @StartYear AND @EndYear)
    ORDER BY 
        s.SURNAME
END

The command that is executed when I look at SQL Server Profiler is

exec GetCaregiverEmailList @DetentionType='LT',@DetentionCounter=3,@StartYear=9,@EndYear=12

And the following are the rows that are returned if I execute the stored procedure manually or running the above exec command.

Rows returned from executing script

Any suggestions or help would be much appreciated.

Regards, Darren

Darren M
  • 65
  • 1
  • 8
  • Why are you hiding the exception? You could be getting an error, which your code ignores? First thing I would do is put some indication out that an exception occur, and a minimum, look at the ex.Message value. This might give you your answer – Sparky Feb 18 '19 at 01:51
  • hi Sparky, what do you mean with hiding the exception? When I step through the code there is no exception thrown. The datatable is just empty. – Darren M Feb 20 '19 at 03:35
  • Anyone know why the question was edited with a hole lot of lines through the code etc? – Darren M Feb 20 '19 at 03:38
  • Is it generally not good practice to show actual names and e-mail addresses. If this is actual data sample, showing it on a website could have legal repercussions. – Sparky Feb 20 '19 at 04:05
  • I redacted the email address in the bitmap before I posted it, but when I first went into the question there were lines through the code samples etc. Just want to make sure I haven't done anything incorrect in posting. – Darren M Feb 20 '19 at 04:15

0 Answers0