数据库是Access数据库,id是自动编号的,老是出现标准表达式中数据类型不匹配,请高手们指点一下,错误出现在哪里,谢谢!
下面是程序:
private void Page_Load(object sender, System.EventArgs e)
{
if(!this.IsPostBack)
{
OleDbConnection con=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("./Test2.mdb"));
con.Open();
OleDbDataAdapter sda=new OleDbDataAdapter();
sda.SelectCommand=new OleDbCommand("select * from Test",con);
DataSet ds=new DataSet();
sda.Fill(ds,"test");
this.DataGrid1.DataKeyField="ID";
this.DataGrid1.DataSource=ds.Tables["test"].DefaultView;
this.DataGrid1.DataBind();
}
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string id=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
OleDbConnection con=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("./Test2.mdb"));
con.Open();
OleDbCommand cmd=new OleDbCommand("delete from Test where ID='"+id+"'",con);
cmd.ExecuteNonQuery();
错误信息是:
标准表达式中数据类型不匹配。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Data.OleDb.OleDbException: 标准表达式中数据类型不匹配。
源错误:
行 92: con.Open();
行 93: OleDbCommand cmd=new OleDbCommand("delete from Test where ID='"+id+"'",con);
行 94: cmd.ExecuteNonQuery();
行 95: }
行 96:
源文件: c:\inetpub\wwwroot\inserttest\webform1.aspx.cs 行: 94
堆栈跟踪:
[OleDbException (0x80040e07): 标准表达式中数据类型不匹配。]
System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr)
System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
insertTest.WebForm1.DataGrid1_DeleteCommand(Object source, DataGridCommandEventArgs e) in c:\inetpub\wwwroot\inserttest\webform1.aspx.cs:94
System.Web.UI.WebControls.DataGrid.OnDeleteCommand(DataGridCommandEventArgs e)
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()