Skip to content

C# code for date wise filtering

shamshad c

Asked Jan 22, 2014, 2:30 AM EST1 answer
Share
LinkedInX
C# code for date wise filtering of datas from database and display the result on a gridview

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    First write down code into Program.cs then go to the form where you want to display data from command button.

    public static void DataGridView(string SQLString, DataGridView dataGrid1)
      {
      SqlConnection Conn = new SqlConnection(CONNECTION_STRING);
      SqlCommand cmd = new SqlCommand(SQLString, Conn);
      try
      {
      Conn.Open();
      SqlDataAdapter da = new SqlDataAdapter(cmd);
      DataTable ds = new DataTable();

      dataGrid1.Enabled = true;
      da.Fill(ds);
      if (ds.Rows.Count > 0)
      {
      dataGrid1.DataSource = ds.DefaultView;
      dataGrid1.Refresh();
      }
      else
      {
      ds.Rows.Clear();
      MessageBox.Show("No data exist in this selection");
      }
      }
      catch (SqlException ex)
      {
      MessageBox.Show(ex.Message);
      }
      finally
      {
      Conn.Close();
      }
      }


      int CountValue = 0;
      bool SearchingAll = false;
      string SearchCondition = "";
      string SearchSQL = "";

      private void btnShow_Click(object sender, EventArgs e)
      {
      CountValue = 0;
      SearchingAll = false;
      SearchCondition = "";
      SearchSQL = "";

      try
      {
      string X = "dd/MM/yyyy";
      string Y = "MM/dd/yyyy";

      DateTime A = DateTime.ParseExact(dtpBrSCDate.Text, X, null);

     

      if (chkDate.Checked == true)
      {
      if (cboTable.SelectedIndex == 0)
      {
      if (dtpBrSCDate.Text.Length > 0)
      {
      if (SearchCondition.Length > 0)
      {
      SearchCondition = SearchCondition + " AND VRDate='" + A.ToString(Y) + "'";
      }
      else
      {
      SearchCondition = SearchCondition + " VRDate='" + A.ToString(Y) + "'";
      }
      }
      }
      if (SearchCondition.Length > 0)
      {
      SearchCondition = SearchCondition + " AND EnteredBy='" + Program.EnteredBy + "'";
      }
      else
      {
      SearchCondition = SearchCondition + " EnteredBy='" + Program.EnteredBy + "'";
      }
      }
      if (SearchCondition.Length > 0 )
      {
      if (SearchingAll == false)
      {
      SearchSQL = "SELECT Hh, Slno, PID, Name, Sex, Eligibility, Dose, CONVERT(NVARCHAR(12),VRDate) as VRDate, VRTime, Reason, VialNo, EnteredBy, CONVERT(NVARCHAR(12),EnteredDate) as EnteredDate, EnteredTime, UpdatedBy, CONVERT(NVARCHAR(12),UpdatedDate) as UpdateDate, UpdatedTime FROM " + cboTable.Text.Trim() + " WHERE " + SearchCondition + " ORDER BY Hh, SlNo";
      CountValue = Program.GetNumericValue("SELECT COUNT(*) AS Knt FROM " + cboTable.Text.Trim() + " WHERE " + SearchCondition);
      }
      else
      {
      SearchSQL = "SELECT * FROM " + cboTable.Text.Trim() + " ORDER BY Hh,SlNo";
      CountValue = Program.GetNumericValue("SELECT COUNT(*) AS Knt FROM " + cboTable.Text.Trim());
      }

      if (CountValue > 0)
      {
      Program.DataGridView(SearchSQL, dgShow);
      label7.Text = CountValue.ToString();
      }
      else
      {
      dgShow.DataSource = null;
      MessageBox.Show("No records found in this selection.","Browse Data",MessageBoxButtons.OK,MessageBoxIcon.Information);
      dgShow.CurrentCell = null;
      dgShow.Refresh();
      }
      }
      else
      {
      txtHh.Focus();
      }
    0