asp.net网格视图编辑和删除记录

时间:2014-03-25 03:31:16

标签: asp.net ado.net

我正在尝试使用网格视图来显示数据库中的项目我还要编辑和删除这些记录然后在网格视图中,我在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 Name=@Name," +
                              "Password=@Password where EmployeeID=@EmployeeID;" +
                              "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 " +
                           "EmployeeID=@EmployeeID;" +
                           "select EmployeeID,Name,Password from Employee";
        comm.Parameters.Add("@EmployeeID", SqlDbType.VarChar).Value
                                                   = lnkRemove.CommandArgument;

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

1 个答案:

答案 0 :(得分: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 Name=@Name,Password=@Password where EmployeeID=@EmployeeID",  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 EmployeeID=@EmployeeID", connectionString);       
       comm.Parameters.AddWithValue("@EmployeeID", lnkRemove);

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