Emmmanuel FIADUFE

Emmmanuel FIADUFE

  • 693
  • 1.1k
  • 55.5k

The Connection was not close

Apr 22 2024 4:36 PM

Hello Ashutosh Singh

After modifying the code as you said, another error popup saying the connection was not close but when I check it closely all connection was close, and also can you help me with any form of validation if the user select a data range which doesn't exist it should alert the user of selecting wrong date range.

 public DataTable LoadFirstTimersReportByDate()
        {
            DataTable dataTable = new DataTable();
            int i = 0;
            dataGridView5.Rows.Clear();
            cn.Open();
            cm = new SqlCommand("Select * FROM tblFirstTimers WHERE FDate BETWEEN @StartDate AND @EndDate", cn);
            cm.Parameters.AddWithValue("@StartDate", FirstTimersDt1.Value.Date);
            cm.Parameters.AddWithValue("@EndDate", FirstTimersDt2.Value.Date);
            SqlDataAdapter da = new SqlDataAdapter(cm);
            da.Fill(dataTable);                       
            dr = cm.ExecuteReader();
            while (dr.Read())
            {
                i++;
                dataGridView5.Rows.Add(i, dr["id"].ToString(), dr["FDate"].ToString(), dr["FName"].ToString(), dr["LName"].ToString(), dr["FGender"].ToString(), dr["FDOB"].ToString(), dr["FLocation"].ToString(), dr["FPhoneNo"].ToString(), dr["FInvitedBy"].ToString(), dr["FPrayerRequest"].ToString());
            }
            dr.Close();
            cn.Close();
            return dataTable;
        }

 

  public void LoadFirstTimers()
        {
            ReportDataSource rptDataSource;
            this.reportViewer1.LocalReport.ReportPath = Application.StartupPath + @"\Reports\rptFirstTimers.rdlc";
            this.reportViewer1.LocalReport.DataSources.Clear();
            cn.Open();
            DataTable filteredData =fmlist.LoadFirstTimersReportByDate();
            rptDataSource = new ReportDataSource("DataSet1", filteredData);         
            reportViewer1.LocalReport.DataSources.Add(rptDataSource);
            reportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout);
            reportViewer1.ZoomMode = ZoomMode.Percent;
            reportViewer1.ZoomPercent = 100;
            cn.Close();
        }


Answers (5)