简体   繁体   中英

C# windows form shows 0 in datagridview where for data grouped by hour, but SQL developer shows the correct results

I have a query in Oracle to display number of transactions by hour, then the last column displays the sum for all hours. The query works fine in SQL developer. However, when I try to display the results in a datagridview of my windows form application, I get 0 for all the hour data. I do however get the final total.

This is the first time I have had trouble using the date picker and displaying data in the datagridview.

Here is a screenshot of the results in SQL developer (I realize the total column does not equal the total of the columns shown. The screen shot does not show all columns.)

在此输入图像描述

And here is the screenshot of the same data in window forms.

在此输入图像描述

This is what I believe is the relevant SQL code. Please forgive me if it is too much.

SELECT
 user_name,
 SUM(CASE
     WHEN substr(hr,11) = '19:00:00' THEN t
     ELSE 0
 END) AS eight_pm,
 SUM(CASE
     WHEN substr(hr,11) = '20:00:00' THEN t
     ELSE 0
 END) AS nine_pm,
 SUM(CASE
     WHEN substr(hr,11) = '21:00:00' THEN t
     ELSE 0
 END) AS ten_pm,
 SUM(CASE
     WHEN substr(hr,11) = '22:00:00' THEN t
     ELSE 0
 END) AS eleven_pm,
 SUM(t) total
 FROM
 (
     SELECT
         user_name,
         COUNT(*) t,
         TO_CHAR(trunc(last_update_date,'HH24') ) AS hr
     FROM
         cte
     GROUP BY
         user_name,
         TO_CHAR(trunc(last_update_date,'HH24') )
 )
 GROUP BY
 user_name
 ORDER BY
 user_name

I am not doing anything with the date picker other that using it as a parameter to query the data. I can show the custom format or some of the C# code if necessary.

Please note, I will not be able to test any solutions until tomorrow morning. Thank you for your time.

Update Adding code for populating datagridview:

 private void btnSecondShiftStats_Click(object sender, EventArgs e)
    {
        var start = startDatePicker.Value;
        var end = endDatePicker.Value;
        string name = tbName.Text;

        dvgSecondShiftStats.DataSource = _secondShiftStatsData.GetStats(start, end, name);
    }

The GetStats method:

   public List<SecondShiftStats> GetStats(DateTime fromDate, DateTime toDate, string uName)
    {
        var statList = new List<SecondShiftStats>();
        var conString = ConfigurationManager.ConnectionStrings["SecondShiftStatsConnection"].ConnectionString;
        using (OracleConnection con = new OracleConnection(conString))
        {
            string sql = @"WITH loaded AS (
                                             SELECT...";

            using (OracleCommand cmd = new OracleCommand(sql, con))
            {
                cmd.Parameters.Add(new OracleParameter(":start_time", OracleDbType.Date)).Value = fromDate;
                cmd.Parameters.Add(new OracleParameter(":end_time", OracleDbType.Date)).Value = toDate;

                cmd.Parameters.Add("name", uName);

                con.Open();
                using (OracleDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var stats = new SecondShiftStats();
                        stats.USER_NAME = reader["USER_NAME"].ToString();
                        stats.THREE_PM = Convert.ToInt32(reader["THREE_PM"]);
                        stats.FOUR_PM = Convert.ToInt32(reader["FOUR_PM"]);
                        stats.FIVE_PM = Convert.ToInt32(reader["FIVE_PM"]);
                        stats.SIX_PM = Convert.ToInt32(reader["SIX_PM"]);
                        stats.SEVEN_PM = Convert.ToInt32(reader["SEVEN_PM"]);
                        stats.EIGHT_PM = Convert.ToInt32(reader["EIGHT_PM"]);
                        stats.NINE_PM = Convert.ToInt32(reader["NINE_PM"]);
                        stats.TEN_PM = Convert.ToInt32(reader["TEN_PM"]);
                        stats.ELEVEN_PM = Convert.ToInt32(reader["ELEVEN_PM"]);
                        stats.TOTAL = Convert.ToInt32(reader["TOTAL"]);

                        statList.Add(stats);
                    }
                }

            } 

        }
        return statList;
    }

I figured out solution to this problem. I have another app showing the same query, but without the grouping. I saw a difference in the datetime format in sql developer vs the datagridview.

SQL Developer:

在此输入图像描述

Datagridview:

在此输入图像描述

And finally the format of the Datetimepicker:

在此输入图像描述

I never changed the way the datgridview displays the date. I only customized the datetimepicker and used it as a parameter in the query. This appeared to be the issue.

So I changed the relevant column in the select statement to:

TO_CHAR(last_update_date, 'DD/MON/YYYY HH24:MI:SS')AS last_update_date.

Of course, I knew I would have to change my final select to:

select ...
case when substr(hr,11)...

to

select ...
case when substr(hr,13)...

Or so I thought. I would get an ORA-01722: invalid-number message when I tried to run the query in SQL Developer.

I determined it was in the grouping statement again, and looked for other solutions to group by the hour. I tried using the accepted answer from this Stack Overflow question , but received a message stating the hour number must be between 1 and 12, even though I have my format set to 'HH24'.

To fix this issue, I changed the final subquery to:

SELECT
 user_name,
 SUM(CASE
     WHEN hr = '19' THEN t
     ELSE 0
 END) AS eight_pm,
 SUM(CASE
     WHEN hr  = '20' THEN t
     ELSE 0
 END) AS nine_pm,
 SUM(CASE
     WHEN hr = '21' THEN t
     ELSE 0
 END) AS ten_pm,
 SUM(CASE
     WHEN hr = '22' THEN t
     ELSE 0
 END) AS eleven_pm,
 SUM(t) total
 FROM
 (
     SELECT
         user_name,
         COUNT(*) t,
         substr(last_update_date,13,2)as hr
     FROM
         cte
     GROUP BY
         user_name,
        substr(last_update_date,13,2)
 )
 group by
 user_name
 order by
 user_name

After making these changes, my applications displays the results I expect it to:

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM