2014-03-25 48 views
0

我試圖使用網格視圖來顯示從數據庫中我也想要編輯,並在網格視圖刪除這些記錄,然後項目,我使用ASP的示例代碼。淨片段asp.net網格視圖編輯和刪除記錄

http://aspsnippets.com/Articles/Simple-Insert-Select-Edit-Update-and-Delete-in-ASPNet-GridView-control.aspx

它說「的GetData」這個名字並不在當前的背景下

using System; 
using System.Collections.Generic; 
using System.Configuration; 
using System.Data; 
using System.Data.SqlClient; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

public partial class Default10 : System.Web.UI.Page 
    { 

     SqlCommand comm; 
     string connectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ConnectionString; 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      BindData(); 
     } 
    } 

    private void BindData() 
    {         
     comm = new SqlCommand("select EmployeeID,Name,Password" + 
         " from Employee");  

     GridView1.DataSource = GetData(comm); 
     GridView1.DataBind();   
    } 


    protected void EditEmployee(object sender, GridViewEditEventArgs e) 
    { 
     GridView1.EditIndex = e.NewEditIndex; 
     BindData(); 
    } 
    protected void CancelEdit(object sender, GridViewCancelEditEventArgs e) 
    { 
     GridView1.EditIndex = -1; 
     BindData(); 
    } 
    protected void UpdateEmployee(object sender, GridViewUpdateEventArgs e) 
    { 
     string EmployeeID = ((Label)GridView1.Rows[e.RowIndex] 
          .FindControl("lblEmployeeID")).Text; 
     string Name = ((TextBox)GridView1.Rows[e.RowIndex] 
          .FindControl("txtName")).Text; 
     string Password = ((TextBox)GridView1.Rows[e.RowIndex] 
          .FindControl("txtPassword")).Text; 


      comm = new SqlCommand(); 
      comm.CommandType = CommandType.Text; 
      comm.CommandText = "update Employee set [email protected]," + 
           "[email protected] where [email protected];" + 
           "select EmployeeID,Name,Password from Employee"; 

      comm.Parameters.Add("@EmployeeID", SqlDbType.VarChar).Value = EmployeeID; 
      comm.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name; 
      comm.Parameters.Add("@Password", SqlDbType.VarChar).Value = Password; 
      GridView1.EditIndex = -1;   

      GridView1.DataSource = GetData(comm); 
      GridView1.DataBind();   
    } 

    protected void DeleteEmployee(object sender, EventArgs e) 
    { 
     LinkButton lnkRemove = (LinkButton)sender; 

     comm = new SqlCommand(); 
     comm.CommandType = CommandType.Text; 
     comm.CommandText = "delete from Employee where " + 
          "[email protected];" + 
          "select EmployeeID,Name,Password from Employee"; 
     comm.Parameters.Add("@EmployeeID", SqlDbType.VarChar).Value 
                = lnkRemove.CommandArgument; 

     GridView1.DataSource = GetData(comm); 
     GridView1.DataBind();    
    } 
} 
+0

我沒有看到任何的GetData()在你的代碼。你只有這個:GetData(comm)。方法聲明在哪裏? –

+0

現在加入的GetData()函數與您的代碼,並檢查 –

+0

@ user3395738只是檢查我的代碼,並與您的完整的男女同校代替.. – 2014-03-25 04:49:05

回答

2

您正在使用的舊編碼所以我的代碼替換代碼存在..

using System; 
using System.Collections.Generic; 
using System.Configuration; 
using System.Data; 
using System.Data.SqlClient; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

public partial class Default10 : System.Web.UI.Page 
    { 

     SqlCommand comm; 
     SqlConnection connectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ConnectionString; 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      BindData(); 
     } 
    } 

    private void BindData() 
    {  
     connectionString.Open();  
     comm = new SqlCommand("select EmployeeID,Name,Password from Employee", connectionString);       
     DataTable dt =new DataTable(); 
     SqlDataAdapter adp= new SqlDataAdapter(comm); 
     adp.Fill(dt); 
     connectionString.Close(); 
     GridView1.DataSource =dt; 
     GridView1.DataBind();   
    } 


    protected void EditEmployee(object sender, GridViewEditEventArgs e) 
    { 
     GridView1.EditIndex = e.NewEditIndex; 
     BindData(); 
    } 
    protected void CancelEdit(object sender, GridViewCancelEditEventArgs e) 
    { 
     GridView1.EditIndex = -1; 
     BindData(); 
    } 

//you should Write this code on Rowupdating and give command name 'update' to link button 
    protected void UpdateEmployee(object sender, GridViewUpdateEventArgs e) 
    { 
//start here 
     string EmployeeID = ((Label)GridView1.Rows[e.RowIndex] 
          .FindControl("lblEmployeeID")).Text; 
     string Name = ((TextBox)GridView1.Rows[e.RowIndex] 
          .FindControl("txtName")).Text; 
     string Password = ((TextBox)GridView1.Rows[e.RowIndex] 
          .FindControl("txtPassword")).Text; 

     connectionString.Open(); 
     comm = new SqlCommand("update Employee set [email protected],[email protected] where [email protected]", connectionString); 

     comm.Parameters.AddWithValue("@EmployeeID", EmployeeID); 
     comm.Parameters.AddWithValue("@Name", Name); 
     comm.Parameters.AddWithValue("@Password", Password); 
     comm.ExecuteNonQuery(); 
     connectionString.Close(); 
     GridView1.EditIndex = -1; 
     BindData();  
//end here     
    } 

//you should Write this code on RowDeleting and give command name 'delete' to link button 
    protected void DeleteEmployee(object sender, EventArgs e) 
    { 
//stat here 
     string EmployeeID = ((Label)GridView1.Rows[e.RowIndex] 
          .FindControl("lblEmployeeID")).Text; 

     connectionString.Open(); 
     comm = new SqlCommand("delete from Employee where [email protected]", connectionString);  
     comm.Parameters.AddWithValue("@EmployeeID", lnkRemove); 

     comm.ExecuteNonQuery(); 
     connectionString.Close(); 
     BindData(); 
//end here 
    } 
}