By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,301 Members | 1,999 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,301 IT Pros & Developers. It's quick & easy.

unable to view data in datagrid

P: 16
the present problem is that i am unable to display data in datagrid....... but the data is visible in database..below is the code what should i do...earlier i could view it also below this code is the code which is in my html page plz help........


Expand|Select|Wrap|Line Numbers
  1. private void Page_Load(object sender, System.EventArgs e)
  2.         {
  3.             if (!Page.IsPostBack)
  4.             {
  5.                 BindData();
  6.             }
  7.  
  8.  
  9.             // Put user code to initialize the page here
  10.             OleDbDataAdapter da = new OleDbDataAdapter("SELECT *FROM CMRS_BUSINESSAREA_MASTER","Provider=MSDAORA.1;User ID=meter_reading;Password=colony;Data Source=windev;OLEDB.NET=true;" );
  11.             DataSet ds = new DataSet();
  12.             da.Fill(ds,"CMRS_BUSINESSAREA_MASTER");
  13.             DataGrid1.DataSource = ds.Tables["CMRS_BUSINESSAREA_MASTER"].DefaultView ;
  14.             DataGrid1.DataBind();
  15.  
  16.         }
  17.  
  18.  
  19.         public void BindData()
  20.         {
  21.             OleDbDataAdapter da = new OleDbDataAdapter("SELECT *FROM CMRS_BUSINESSAREA_MASTER","Provider=MSDAORA.1;User ID=meter_reading;Password=colony;Data Source=windev;OLEDB.NET=true;" ); 
  22.             DataSet ds = new DataSet(); 
  23.             da.Fill(ds);
  24.  
  25.             DataGrid1.DataSource = ds.Tables[0].DefaultView ;
  26.             DataGrid1.DataBind();
  27.  
  28.  
  29.         }
  30.         public void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs   e)
  31.         {
  32.             DataGrid1.EditItemIndex = e.Item.ItemIndex;
  33.             BindData(); 
  34.         }
  35.  
  36.         public void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs   e)
  37.         {
  38.             DataGrid1.EditItemIndex = -1; 
  39.             BindData(); 
  40.         }
  41.  
  42.         public void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs   e)
  43.         {
  44.             string str = ConfigurationSettings.AppSettings["ConnectionString"].ToString();
  45.             OleDbConnection con1 = new OleDbConnection();
  46.             con1.ConnectionString = str;
  47.  
  48.  
  49.             TextBox txtAREADESCRIPTION =(TextBox) e.Item.Cells[1].Controls[0];
  50.             string strUpdateStmt;
  51.  
  52.             strUpdateStmt = "UPDATE CMRS_BUSINESSAREA_MASTER SET " + "CBM_BUSAREA_DESC = '" + txtAREADESCRIPTION.Text + "' " + "WHERE CBM_BUSAREA_CODE ='" + e.Item.Cells[0].Text +"'";
  53.  
  54.             cmd = new OleDbCommand(strUpdateStmt,con1);
  55.             con1.Open();
  56.             cmd.ExecuteNonQuery();         
  57.             DataGrid1.EditItemIndex = -1;
  58.             BindData();
  59.         }
  60.  
  61.  
  62.         #region Web Form Designer generated code
  63.         override protected void OnInit(EventArgs e)
  64.         {
  65.             //
  66.             // CODEGEN: This call is required by the ASP.NET Web Form Designer.
  67.             //
  68.             InitializeComponent();
  69.             base.OnInit(e);
  70.         }
  71.  
  72.         /// <summary>
  73.         /// Required method for Designer support - do not modify
  74.         /// the contents of this method with the code editor.
  75.         /// </summary>
  76.         private void InitializeComponent()
  77.         {    
  78.             this.cmd = new System.Data.OleDb.OleDbCommand();
  79.             this.con1 = new System.Data.OleDb.OleDbConnection();
  80.             this.addbtn.Click += new System.EventHandler(this.addbtn_Click);
  81.             this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHand  ler(this.DataGrid1_CancelCommand);
  82.             this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHand  ler(this.DataGrid1_EditCommand);
  83.             this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHand  ler(this.DataGrid1_UpdateCommand);
  84.             this.Load += new System.EventHandler(this.Page_Load);
  85.  
  86.         }
  87.         #endregion
  88.  
  89.         private void addbtn_Click(object sender, System.EventArgs e)
  90.         {
  91.             string str = ConfigurationSettings.AppSettings["ConnectionString"].ToString();  
  92.  
  93.             OleDbConnection con1 = new OleDbConnection();
  94.             con1.ConnectionString = str;
  95.             OleDbCommand cmd = new OleDbCommand("Insert into cmrs_businessarea_master values('"+TextBox1.Text+"','"+TextBox2.Text+"')",con1);       
  96.             con1.Open();
  97.             cmd.ExecuteNonQuery();
  98.             con1.Close();
  99.             BindData();
  100.  
  101.  
  102.         }
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.     }
  110.  
  111.  
  112.  
  113. }

Expand|Select|Wrap|Line Numbers
  1. <asp:datagrid id="DataGrid1" style="Z-INDEX: 104; LEFT: 440px; POSITION: absolute; TOP: 304px"
  2.                 runat="server" Height="125px" Width="432px" OnUpdateCommand="DataGrid1_UpdateCommand" OnCancelCommand="DataGrid1_CancelCommand"
  3.                 OnEditCommand="DataGrid1_EditCommand" AutoGenerateColumns="False">
  4.                 <Columns>
  5.                     <asp:BoundColumn HeaderText="AREA CODE"></asp:BoundColumn>
  6.                     <asp:BoundColumn HeaderText="AREA DESCRIPTION"></asp:BoundColumn>
  7.                     <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
  8.                 </Columns>
  9.             </asp:datagrid></form>
Jul 26 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.