【问题标题】:how to update data in webform using Gridview?如何使用 Gridview 更新 web 表单中的数据?
【发布时间】:2016-06-22 09:42:46
【问题描述】:

我想给我的用户一个数据表,他/她可以在其中编辑数据。 我使用这种方法从 sql server 获取我的数据

        public static List<TestAsfa> GetRecordsMan() {
        using (var d = new TestEntities())
        {
            return d.TestAsfa.ToList();
        }

我想写删除函数的时候卡住了

 protected void RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            dt.Rows[GridView1.Rows[e.RowIndex].RowIndex].Delete();
            int id =Convert.ToInt32(GridView1.Rows[e.RowIndex].ID);

            FillGridView();
        }
        catch
        {
            Response.Write("<script> alert('Record not deleted...') </script>");
        }
    }

这是删除功能。我写了下面的方法

public static void deleteRecord(int id)
    {

        using (var d = new TestEntities())
        {
            TestAsfa tb = d.TestAsfa.SingleOrDefault(t => t.ID == id);
            d.TestAsfa.Remove(tb);
            d.SaveChanges();
        }
    }

但我不知道它没有从发件人那里获得正确的行 ID 我不明白我的错误

【问题讨论】:

    标签: c# asp.net sql-server gridview webforms


    【解决方案1】:

    不要忘记防止错误,您需要确保您检查的 id 是实际项目。像这样的东西,例如:

        public static void deleteRecord(TestAsfa TA,int id)
        { 
             using(var d = new TestEntities())
             {
                TA= d.TestAsfa.Where(item => item.ItemId == id).FirstOrDefault(); 
                if (itemTA!= null) {
                   d.TestAsfa.Remove(TA);
                }
             }             
    
        }
    

    【讨论】:

      【解决方案2】:

      要删除选定的行,请使用此代码

       using(var d = new TestEntities())
       {
           d.TestAsfa.Remove(d.TestAsfa.Find(id));
           d.SaveChanges();
       }
      

      或者使用这个

       using(var d = new TestEntities())
        {
           TestAsfa tb = d.TestAsfa.SingleOrDefault(t => t.Id == id);
            d.TestAsfa.Remove(tb);
            d.SaveChanges();
         }
      

      【讨论】:

      • 谢谢,但我收到此错误 ex = {"Value cannot be null.\r\nParameter name: entity"}
      • 这似乎你的 Id 有空值,
      • 问题是 rowID 它总是得到 0
      • 你的意思是有问题 int id =Convert.ToInt32(GridView1.Rows[e.RowIndex].ID);
      • 是的,我想我应该使用另一种方式来获取行 ID
      【解决方案3】:

      你好,看看这个

      受保护的 void GridView1_RowDeleting(对象发送者,GridViewDeleteEventArgs e)
      {

              GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];  
              Label lbldeleteid = (Label)row.FindControl("lblID");   
              conn.Open();  
      
              SqlCommand cmd = new SqlCommand("delete FROM tableName where   
              id='"+Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString())+"'", conn);     
      
              cmd.ExecuteNonQuery();       
      
              conn.Close();     
              gvbind();
      
          }
      

      【讨论】:

      • 对不起,conn 是什么??
      • 数据库连接字符串..我们必须打开连接才能执行查询!
      • 你的意思是我应该添加这个字符串 conn = ConfigurationManager.ConnectionStrings["TestEntities"].ConnectionString;
      • 是的..如果可能是“TestEntities”你的连接字符串名称
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多