asp.net c# 删除数据
usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingS...
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class manage : System.Web.UI.Page
{
/// <summary>
/// 获取链接字符串
/// </summary>
private string myConnectionString = ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
///创建SqlConnection
SqlConnection myConnection = new SqlConnection(myConnectionString);
string cmdText = "SELECT * FROM ss";
SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
///打开连接
myConnection.Open();
GridView1.DataSource = myCommand.ExecuteReader();
GridView1.DataBind();
///关闭数据库的链接
myConnection.Close();
}
protected void UserView_RowCommand(object sender, GridViewCommandEventArgs e)
{
///获取参数
string commandName = e.CommandName;
int ID = -1;
if (Int32.TryParse(e.CommandArgument.ToString(), out ID) == false || commandName == "")
{
return;
}
///创建User实例
if (!IsPostBack)
{
switch (commandName)
{
case "delete":
{
///删除选择的用户
SqlConnection myConnection = new SqlConnection(myConnectionString);
string cmdText = "SELECT * FROM ss where id="+"'"+ID.ToString()+"'";
SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
GridView1.DataBind();
break;
}
default:
break;
}
}
}
}
------------------------------
总是报错!!请给小弟看看!!非常的感激
回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的
这是manage.aspx部分代码
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" OnRowCommand="UserView_RowCommand" >
... ...
<asp:ImageButton ID="DeleteBtn" runat="server" CommandName="delete" ImageUrl="~/img/delete.gif" AlternateText="删除该用户" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"ID") %>' />
改完是不错了 但是也删不了数据啊 展开
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class manage : System.Web.UI.Page
{
/// <summary>
/// 获取链接字符串
/// </summary>
private string myConnectionString = ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
///创建SqlConnection
SqlConnection myConnection = new SqlConnection(myConnectionString);
string cmdText = "SELECT * FROM ss";
SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
///打开连接
myConnection.Open();
GridView1.DataSource = myCommand.ExecuteReader();
GridView1.DataBind();
///关闭数据库的链接
myConnection.Close();
}
protected void UserView_RowCommand(object sender, GridViewCommandEventArgs e)
{
///获取参数
string commandName = e.CommandName;
int ID = -1;
if (Int32.TryParse(e.CommandArgument.ToString(), out ID) == false || commandName == "")
{
return;
}
///创建User实例
if (!IsPostBack)
{
switch (commandName)
{
case "delete":
{
///删除选择的用户
SqlConnection myConnection = new SqlConnection(myConnectionString);
string cmdText = "SELECT * FROM ss where id="+"'"+ID.ToString()+"'";
SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
GridView1.DataBind();
break;
}
default:
break;
}
}
}
}
------------------------------
总是报错!!请给小弟看看!!非常的感激
回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的
这是manage.aspx部分代码
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" OnRowCommand="UserView_RowCommand" >
... ...
<asp:ImageButton ID="DeleteBtn" runat="server" CommandName="delete" ImageUrl="~/img/delete.gif" AlternateText="删除该用户" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"ID") %>' />
改完是不错了 但是也删不了数据啊 展开
4个回答
展开全部
你这里有2点错误
1,首先先解决提示错误
报此错误的原因是在PageLoad事件里面没加if(!IsPostBack)
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
///创建SqlConnection
SqlConnection myConnection = new SqlConnection(myConnectionString);
string cmdText = "SELECT * FROM ss";
SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
///打开连接
myConnection.Open();
GridView1.DataSource = myCommand.ExecuteReader();
GridView1.DataBind();
///关闭数据库的链接
myConnection.Close();
}
}
2.删除用户时,SQL语句有误
string cmdText = "SELECT * FROM ss where id="+"'"+ID.ToString()+"'";
改成
string cmdText = "DELETE FROM ss where id="+"'"+ID.ToString()+"'";
1,首先先解决提示错误
报此错误的原因是在PageLoad事件里面没加if(!IsPostBack)
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
///创建SqlConnection
SqlConnection myConnection = new SqlConnection(myConnectionString);
string cmdText = "SELECT * FROM ss";
SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
///打开连接
myConnection.Open();
GridView1.DataSource = myCommand.ExecuteReader();
GridView1.DataBind();
///关闭数据库的链接
myConnection.Close();
}
}
2.删除用户时,SQL语句有误
string cmdText = "SELECT * FROM ss where id="+"'"+ID.ToString()+"'";
改成
string cmdText = "DELETE FROM ss where id="+"'"+ID.ToString()+"'";
展开全部
///删除选择的用户
SqlConnection myConnection = new SqlConnection(myConnectionString);
string cmdText = "SELECT * FROM ss where id="+"'"+ID.ToString()+"'";
SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
GridView1.DataBind();
break;
}
你删除数据部分用的是selsct啊,怎么可能删除
SqlConnection myConnection = new SqlConnection(myConnectionString);
string cmdText = "SELECT * FROM ss where id="+"'"+ID.ToString()+"'";
SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
GridView1.DataBind();
break;
}
你删除数据部分用的是selsct啊,怎么可能删除
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
aspx代码第一行加上或修改成EnableEventValidation="false"
<%@ Page EnableEventValidation="false" %>
<%@ Page EnableEventValidation="false" %>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
case "delete":
{
///删除选择的用户
SqlConnection myConnection = new SqlConnection(myConnectionString);
string cmdText = "SELECT * FROM ss where id="+"'"+ID.ToString()+"'";
SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
GridView1.DataBind();
break;
}
///
string cmdText = "SELECT * FROM ss where id="+"'"+ID.ToString()+"'";
改成
string cmdText = "delete FROM ss where id="+"'"+ID.ToString()+"'";
{
///删除选择的用户
SqlConnection myConnection = new SqlConnection(myConnectionString);
string cmdText = "SELECT * FROM ss where id="+"'"+ID.ToString()+"'";
SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
GridView1.DataBind();
break;
}
///
string cmdText = "SELECT * FROM ss where id="+"'"+ID.ToString()+"'";
改成
string cmdText = "delete FROM ss where id="+"'"+ID.ToString()+"'";
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询