Gridview编辑ASP.NET中的删除和更新

时间:2016-06-14 03:30:53

标签: c# mysql asp.net gridview

我尝试为gridview实现编辑和更新按钮的代码,但它似乎对我不起作用。添加按钮运行良好,但删除和更新不起作用。在运行时错误期间,错误是“MySql.Data.dll中发生类型'MySql.Data.MySqlClient.MySqlException'的异常,但未在用户代码中处理 附加信息:'where子句'中的未知列'p001'“

注意:数据库中的P_Id类型是varchar(10),name varch(100),level varchar,value varchar

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

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindData();
    }
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
    TextBox txtID = (TextBox)GridView1.FooterRow.FindControl("txtID");
    TextBox txtSubject = (TextBox)GridView1.FooterRow.FindControl("txtSubject");
    RadioButtonList Level = (RadioButtonList)GridView1.FooterRow.FindControl("RadioButtonList2");
    RadioButtonList PType = (RadioButtonList)GridView1.FooterRow.FindControl("RadioButtonList1");
    AddPractice(txtID.Text.Trim(), txtSubject.Text.Trim(), Level.Text.Trim(), PType.Text.Trim());
    BindData();
}
private void AddPractice(string P_Id, string subject, string level, string type)
{
    string connStr = @"Data Source=localhost;Database=ahsschema;User Id=webuser;Password=webuser2014";

    using (MySqlConnection cn = new MySqlConnection(connStr))
    {
        string query = "insert into practice(P_Id,name,level,value) values ('" + P_Id + "','" + subject + "','" + level + "','" + type + "')";
        MySqlCommand cmd = new MySqlCommand(query, cn);
        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();

    }
}
private void BindData()
{
    DataTable dt = new DataTable();
   string connStr = @"Data Source=localhost;Database=ahsschema;User Id=webuser;Password=webuser2014";
    using (MySqlConnection cn = new MySqlConnection(connStr))
    {
        MySqlDataAdapter adp = new MySqlDataAdapter("select P_Id,level,name,value from practice", cn);
        adp.Fill(dt);

    }
    if (dt.Rows.Count > 0)
    {
        GridView1.DataSource = dt;
        GridView1.DataBind();

    }
}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    GridView1.EditIndex = -1;
    BindData();
}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    string id = (GridView1.DataKeys[e.RowIndex].Value.ToString ());
    DeletePractice(id);
    BindData();


}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    BindData();
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
  //  int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
    TextBox txtID = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtID");
    TextBox txtSubject = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtSubject");
   // TextBox Level1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtlevel");
   // TextBox PType1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtPType1");
     RadioButtonList Level = (RadioButtonList)GridView1.Rows[e.RowIndex].FindControl("RadioButtonList2");
     RadioButtonList PType = (RadioButtonList)GridView1.Rows[e.RowIndex].FindControl("RadioButtonList1");

    UpdatePractice( txtID.Text , txtSubject.Text, Level.Text, PType.Text);
    GridView1.EditIndex = -1;
    BindData();

}
private void UpdatePractice( string P_Id, string name, string level, string value)
{
     string connStr = @"Data Source=localhost;Database=ahsschema;User Id=webuser;Password=webuser2014";
    using (MySqlConnection cn = new MySqlConnection(connStr))
    {
        string query = "UPDATE practice SET P_Id='" + P_Id + "',name='" + name + "',level='" + level + "',value='" + value + " WHERE P_Id=" + P_Id + "";
        MySqlCommand cmd = new MySqlCommand(query, cn);
        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();
    }
}
private void DeletePractice(string id)
{
   string connStr = @"Data Source=localhost;Database=ahsschema;User Id=webuser;Password=webuser2014";
    using (MySqlConnection cn = new MySqlConnection(connStr))
    {
        string query = "DELETE FROM practice WHERE P_Id=" + id + "";
        MySqlCommand cmd = new MySqlCommand(query, cn);
        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();
    }
}

1 个答案:

答案 0 :(得分:0)

第一眼:

由于P_Id列具有 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sudeep.loginactivity"> <!-- To auto-complete the email text field in the login form with the user's emails --> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.READ_PROFILE" /> <uses-permission android:name="android.permission.READ_CONTACTS" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".LoginActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <action android:name="com.package.MyActivity"/> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 数据类型,我发现你忘了在UPDATE&amp;上添加一些额外的撇号。 DELETE子句。正确形式的SQL语句应该是这样的(注意P_Id列周围的附加撇号):

VARCHAR(10)

string query = "UPDATE practice SET P_Id='" + P_Id + "',name='" + name + "',level='" + level + "',value='" + value + "' WHERE P_Id= '" + P_Id + "'";

只有字符串值需要围绕它们的撇号,数字不需要。