not able to display data in datagrid

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • geeteshss
    New Member
    • Jun 2007
    • 16

    not able to display data in datagrid

    First i would like to thank those persons who helped me out in solving my earlier problems
    the present problem is that i am unable to display data in datagrid....... but the data is visible in database..below is the code
    Code:
    private void Page_Load(object sender, System.EventArgs e)
    		{
    			if (!Page.IsPostBack)
    			{
    				BindData();
    			}
    
    
    			// Put user code to initialize the page here
    			OleDbDataAdapter da = new OleDbDataAdapter("SELECT *FROM CMRS_BUSINESSAREA_MASTER","Provider=MSDAORA.1;User ID=meter_reading;Password=colony;Data Source=windev;OLEDB.NET=true;" );
    			DataSet ds = new DataSet();
    			da.Fill(ds,"CMRS_BUSINESSAREA_MASTER");
    			DataGrid1.DataSource = ds.Tables["CMRS_BUSINESSAREA_MASTER"].DefaultView ;
    			DataGrid1.DataBind();
    
    		}
    	
    
    		public void BindData()
    		{
    			OleDbDataAdapter da = new OleDbDataAdapter("SELECT *FROM CMRS_BUSINESSAREA_MASTER","Provider=MSDAORA.1;User ID=meter_reading;Password=colony;Data Source=windev;OLEDB.NET=true;" ); 
    			DataSet ds = new DataSet(); 
    			da.Fill(ds);
    			
    			DataGrid1.DataSource = ds.Tables[0].DefaultView ;
    			DataGrid1.DataBind();
    			
    
    		}
    		public void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    		{
    			DataGrid1.EditItemIndex = e.Item.ItemIndex;
    			BindData(); 
    		}
    
    		public void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    		{
    			DataGrid1.EditItemIndex = -1; 
    			BindData(); 
    		}
    
    		public void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    		{
    			string str = ConfigurationSettings.AppSettings["ConnectionString"].ToString();
    			OleDbConnection con1 = new OleDbConnection();
    			con1.ConnectionString = str;
    			    
    			     	
    			TextBox txtAREADESCRIPTION =(TextBox) e.Item.Cells[1].Controls[0];
    			string strUpdateStmt;
    
    			strUpdateStmt = "UPDATE CMRS_BUSINESSAREA_MASTER SET " + "CBM_BUSAREA_DESC = '" + txtAREADESCRIPTION.Text + "' " + "WHERE CBM_BUSAREA_CODE ='" + e.Item.Cells[0].Text +"'";
    			
    			cmd = new OleDbCommand(strUpdateStmt,con1);
    			con1.Open();
    			cmd.ExecuteNonQuery();         
    			DataGrid1.EditItemIndex = -1;
    			BindData();
    		}
    
    
    		#region Web Form Designer generated code
    		override protected void OnInit(EventArgs e)
    		{
    			//
    			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
    			//
    			InitializeComponent();
    			base.OnInit(e);
    		}
    		
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		private void InitializeComponent()
    		{    
    			this.cmd = new System.Data.OleDb.OleDbCommand();
    			this.con1 = new System.Data.OleDb.OleDbConnection();
    			this.addbtn.Click += new System.EventHandler(this.addbtn_Click);
    			this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
    			this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
    			this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
    			this.Load += new System.EventHandler(this.Page_Load);
    
    		}
    		#endregion
    
    		private void addbtn_Click(object sender, System.EventArgs e)
    		{
    			string str = ConfigurationSettings.AppSettings["ConnectionString"].ToString();  
    
    			OleDbConnection con1 = new OleDbConnection();
    			con1.ConnectionString = str;
    			OleDbCommand cmd = new OleDbCommand("Insert into cmrs_businessarea_master values('"+TextBox1.Text+"','"+TextBox2.Text+"')",con1);		 
    			con1.Open();
    			cmd.ExecuteNonQuery();
    			con1.Close();
                BindData();
                
    	
    		}
    
    		
    
    		
    
    
    	}
    		
    		
    	
    }
  • dip_developer
    Recognized Expert Contributor
    • Aug 2006
    • 648

    #2
    Originally posted by geeteshss
    First i would like to thank those persons who helped me out in solving my earlier problems
    the present problem is that i am unable to display data in datagrid....... but the data is visible in database..below is the code
    Code:
    private void Page_Load(object sender, System.EventArgs e)
    		{
    			if (!Page.IsPostBack)
    			{
    				BindData();
    			}
     
     
    			// Put user code to initialize the page here
    			OleDbDataAdapter da = new OleDbDataAdapter("SELECT *FROM CMRS_BUSINESSAREA_MASTER","Provider=MSDAORA.1;User ID=meter_reading;Password=colony;Data Source=windev;OLEDB.NET=true;" );
    			DataSet ds = new DataSet();
    			da.Fill(ds,"CMRS_BUSINESSAREA_MASTER");
    			DataGrid1.DataSource = ds.Tables["CMRS_BUSINESSAREA_MASTER"].DefaultView ;
    			DataGrid1.DataBind();
     
    		}
     
     
    		public void BindData()
    		{
    			OleDbDataAdapter da = new OleDbDataAdapter("SELECT *FROM CMRS_BUSINESSAREA_MASTER","Provider=MSDAORA.1;User ID=meter_reading;Password=colony;Data Source=windev;OLEDB.NET=true;" ); 
    			DataSet ds = new DataSet(); 
    			da.Fill(ds);
     
    			DataGrid1.DataSource = ds.Tables[0].DefaultView ;
    			DataGrid1.DataBind();
     
     
    		}
    		public void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    		{
    			DataGrid1.EditItemIndex = e.Item.ItemIndex;
    			BindData(); 
    		}
     
    		public void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    		{
    			DataGrid1.EditItemIndex = -1; 
    			BindData(); 
    		}
     
    		public void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    		{
    			string str = ConfigurationSettings.AppSettings["ConnectionString"].ToString();
    			OleDbConnection con1 = new OleDbConnection();
    			con1.ConnectionString = str;
     
     
    			TextBox txtAREADESCRIPTION =(TextBox) e.Item.Cells[1].Controls[0];
    			string strUpdateStmt;
     
    			strUpdateStmt = "UPDATE CMRS_BUSINESSAREA_MASTER SET " + "CBM_BUSAREA_DESC = '" + txtAREADESCRIPTION.Text + "' " + "WHERE CBM_BUSAREA_CODE ='" + e.Item.Cells[0].Text +"'";
     
    			cmd = new OleDbCommand(strUpdateStmt,con1);
    			con1.Open();
    			cmd.ExecuteNonQuery(); 
    			DataGrid1.EditItemIndex = -1;
    			BindData();
    		}
     
     
    		#region Web Form Designer generated code
    		override protected void OnInit(EventArgs e)
    		{
    			//
    			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
    			//
    			InitializeComponent();
    			base.OnInit(e);
    		}
     
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		private void InitializeComponent()
    		{ 
    			this.cmd = new System.Data.OleDb.OleDbCommand();
    			this.con1 = new System.Data.OleDb.OleDbConnection();
    			this.addbtn.Click += new System.EventHandler(this.addbtn_Click);
    			this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
    			this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
    			this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
    			this.Load += new System.EventHandler(this.Page_Load);
     
    		}
    		#endregion
     
    		private void addbtn_Click(object sender, System.EventArgs e)
    		{
    			string str = ConfigurationSettings.AppSettings["ConnectionString"].ToString(); 
     
    			OleDbConnection con1 = new OleDbConnection();
    			con1.ConnectionString = str;
    			OleDbCommand cmd = new OleDbCommand("Insert into cmrs_businessarea_master values('"+TextBox1.Text+"','"+TextBox2.Text+"')",con1);		 
    			con1.Open();
    			cmd.ExecuteNonQuery();
    			con1.Close();
    BindData(); 
    		} 
    	} 
    }
    I didnt understand your logic......why the code between line 9 and 14 in Page_Load event...you are calling BinData() first time when page loads...thats fine...then why are you again binding the grid.......outs ide if block in Page_Load....

    Comment

    Working...