当前位置:编程学习 > C#/ASP.NET >>

关于datagrid的增删改查问题!急用!在线等候!

以下是在CS中的部分代码:
protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
    {
        string Sqlstr = "delete employee where ID = " + this.DataGrid1.DataKeys[e.Item.ItemIndex];
        string Connstr = System.Configuration.ConfigurationManager.AppSettings["ConnectionStrings"];
        SqlConnection Conn = new SqlConnection(Connstr);
        SqlCommand Cmd = new SqlCommand(Sqlstr, Conn);
        try
        {
            if (Conn.State != ConnectionState.Open)
            {
                Conn.Open();
            }
            Cmd.ExecuteNonQuery();
            this.MyDataBander();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
            Conn.Close();
        }
        finally
        {
            Conn.Close();
            Conn.Dispose();
            Conn = null;
        }
    }
//提交按纽(增加数据)
    protected void Button_insert_Click(object sender, EventArgs e)
    {
        string Connstr = System.Configuration.ConfigurationManager.AppSettings["ConnectionStrings"];
        SqlConnection Conn = new SqlConnection(Connstr);
        string strInsert = "insert into employee(name , 易做图 , subject , e-mail,phonenumber ) VALUES ('" + this.TB_name.Text
        + "', '" +
       this.DDL_易做图.SelectedValue + "', '" +
        this.TB_subject.Text + "', '" +
        this.TB_email.Text + "', '" +
        this.TB_phonenumber.Text + "')";
        SqlCommand Cmd = new SqlCommand(Convert.ToString(strInsert), Conn);
        try
        {
            if (Conn.State != ConnectionState.Open)
            {
                Conn.Open();
            }
            Cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
            Conn.Close();
        }
        finally
        {
            Conn.Close();
            Conn.Dispose();
            Conn = null;
        }
        this.MyDataBander();
        TB_ID.Text = "";
        TB_name.Text = "";
        DDL_易做图.Text = "";
        TB_subject.Text = "";
        TB_email.Text = "";
        TB_phonenumber.Text = "";
    }
//更新
    protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
    {
        string ID = ((TextBox)e.Item.FindControl("TB_ID")).Text;
        string name = ((TextBox)e.Item.FindControl("TB_name")).Text;
        string 易做图 = ((DropDownList)e.Item.FindControl("DDL_易做图")).Text;
        string subject = ((TextBox)e.Item.FindControl("TB_subject")).Text;
        string email = ((TextBox)e.Item.FindControl("TB_email")).Text;
        string phonenumber = ((TextBox)e.Item.FindControl("TB_phonenumber")).Text;
        string Sqlstr = "update employee set ID='" + ID + "',name+'" + name + "',易做图+'" + 易做图 + "',subject+'" + subject + "',e-mail+'" + email + "',phonenumber+'" + phonenumber + "'where ID=" + this.DataGrid1.DataKeys[e.Item.ItemIndex];
        string Connstr = System.Configuration.ConfigurationManager.AppSettings["ConnectionStrings"];
        SqlConnection Conn = new SqlConnection(Connstr);
        SqlCommand Cmd = new SqlCommand(Sqlstr, Conn);
        try
        {
            if (Conn.State != ConnectionState.Open)
            {
                Conn.Open();
            }
            Cmd.ExecuteNonQuery();
            this.DataGrid1.EditItemIndex = -1;
            this.MyDataBander();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
            Conn.Close();
        }
        finally
        {
            Conn.Close();
            Conn.Dispose();
            Conn = null;
        }
   }
有一张employee的表,里面的字段分别为:ID,name,易做图,subject,e-mail,phonenumber,其中ID是主键;
不知是代码有什么问题还是,我以上功能均无法实现,请各位高手帮帮忙! --------------------编程问答-------------------- //string Sqlstr = "delete employee where ID = " + this.DataGrid1.DataKeys[e.Item.ItemIndex]; 


this.DataGrid1.DataKeys[e.Item.ItemIndex]
是这条记录在DataGrid中的序号,如一页显示10条,那么这个ItemIndex不会大于10
但你的SQL中使用的却是ID,很明显不对嘛 --------------------编程问答-------------------- 更新的问题同上,取ID取错了 --------------------编程问答-------------------- 呵呵,问题解决了,但不是您所说的问题;以上代码均没问题,可以用
只是我datagrid中的CommandName属性没设置!
不过同样感谢您的热心解答! --------------------编程问答-------------------- CommandName属性没设置! 
--------------------编程问答-------------------- 是啊,细节问题啊
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,