Ahmet Taha

Ahmet Taha

  • NA
  • 381
  • 20.3k

cannot display data between date in gridview c#.

Jun 1 2021 9:21 AM
hello guyz , i need yor help , i cannot display the data between gridview when i filtering it in date , attach here is my source code in this project
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data.SqlClient;  
  8.   
  9.   
  10. namespace onlinestudinformation  
  11. {  
  12.     public partial class studentaccountlst : System.Web.UI.Page  
  13.     {  
  14.         SqlCommand cmd;  
  15.         SqlConnection cn;  
  16.         SqlDataAdapter da;  
  17.         SqlDataReader dr;  
  18.         public void connectdb()  
  19.         {  
  20.             cn = new SqlConnection(@"Data Source=LAPTOP-93441BLD\HUSSIEN;Initial Catalog=studentinfodb;Integrated Security=True");  
  21.             cn.Open();  
  22.         }  
  23.         protected void Page_Load(object sender, EventArgs e)  
  24.         {  
  25.             if (!IsPostBack)  
  26.             {  
  27.                 this.connectdb();  
  28.                 this.showlist();  
  29.             }  
  30.   
  31.         }  
  32.         private void showlist()  
  33.         {  
  34.             this.connectdb();  
  35.             cmd = new SqlCommand("select *from tblOnlineuserregistration ", cn);  
  36.             dr = cmd.ExecuteReader();  
  37.             GridView1.DataSource = dr;  
  38.             GridView1.DataBind();  
  39.         }  
  40.   
  41.         protected void btnload_Click(object sender, EventArgs e)  
  42.         {  
  43.             loadadata();  
  44.         }  
  45.         void loadadata()  
  46.         {  
  47.   
  48.             this.connectdb();  
  49.           //  cmd = new SqlCommand(" SELECT lrn, lastname, firstname, middlename, email, username from tblOnlineuserregistration  where registereddate between '" + DateTime.ParseExact(TextBox1.Text, "dd-MM-yyyy", null).ToString("dd/MMM/yyyy") + "' and '" + DateTime.ParseExact(TextBox2.Text, "dd-MM-yyyy", null).ToString("dd/MMM/yyyy") + "'", cn);  
  50.             cmd = new SqlCommand(" SELECT lrn, lastname, firstname, middlename, email, username,registereddate from tblOnlineuserregistration  where registereddate between '" + TextBox1.Text + "' and '" + TextBox2.Text + "'", cn);  
  51.   
  52.             // cmd = new SqlCommand("Select * from tbl_EmpPayroll where DateFrom between '" + dateTimePicker1.Value.ToString("MM/dd/yyyy") + "' and '" + dateTimePicker2.Value.ToString("MM/dd/yyyy") + "'", conn);  
  53.             dr = cmd.ExecuteReader();  
  54.             if (dr.Read())  
  55.             {  
  56.                 this.connectdb();  
  57.                 cmd = new SqlCommand(" SELECT lrn, lastname, firstname, middlename, email, username,registereddate from tblOnlineuserregistration  where registereddate between '" + DateTime.ParseExact(TextBox1.Text, "dd/MM/yyyy"null).ToString("dd/MMM/yyyy") + "' and '" + DateTime.ParseExact(TextBox2.Text, "dd/MM/yyyy"null).ToString("dd/MMM/yyyy") + "'", cn);  
  58.                 GridView1.DataSource = dr;  
  59.                 GridView1.DataBind();  
  60.             }  
  61.             else  
  62.             {  
  63.                 dr.Close();  
  64.                // MessageBox.Show("error", "Message....", MessageBoxButtons.OK, MessageBoxIcon.Warning);  
  65.                 ScriptManager.RegisterStartupScript(thisthis.GetType(), "script""alert('No record has been found');"true);  
  66.             }  
  67.         }  
  68.     }  
  69. }  
 

Answers (2)