每当我运行此代码时,我一直收到此错误,我真的无法找到我在这里失踪的内容所以请提前帮助我
错误: System.Data.dll中发生'System.Data.OleDb.OleDbException',但未在用户代码中处理
其他信息:标准表达式中的数据类型不匹配。
前端代码:
<asp:GridView ID="gvFunction" runat="server" AutoGenerateColumns="false" CssClass="Grid"
DataKeyNames="ID" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img alt = "" style="cursor: pointer" src="images/plus.png" />
<asp:Panel ID="pnlOrders" runat="server" Style="display: none">
<asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" CssClass = "ChildGrid">
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField="ID" HeaderText="ID" />
<asp:BoundField ItemStyle-Width="150px" DataField="FunctionDate" HeaderText="Function Date" />
<asp:BoundField ItemStyle-Width="150px" DataField="FunctionTime" HeaderText="Function Time" />
<asp:BoundField ItemStyle-Width="150px" DataField="CelebrateName" HeaderText="Celebrate Name" />
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width="150px" DataField="ID" HeaderText="ID" />
<asp:BoundField ItemStyle-Width="150px" DataField="FunctionDate" HeaderText="Function Date" />
<asp:BoundField ItemStyle-Width="150px" DataField="CelebrateName" HeaderText="Celebrate Name" />
</Columns>
</asp:GridView>
后端代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.OleDb;
using System.Data;
namespace mntfinal
{
public partial class editreport : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvFunction.DataSource = GetData("select ID,
FunctionDate, CelebrateName from function");
gvFunction.DataBind();
}
}
private static DataTable GetData(string query)
{
string strConnString = ConfigurationManager.ConnectionStrings
["MandapamDatabase"].ConnectionString;
using (OleDbConnection con = new OleDbConnection(strConnString))
{
using (OleDbCommand cmd = new OleDbCommand())
{
cmd.CommandText = query;
using (OleDbDataAdapter sda = new OleDbDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string ID = gvFunction.DataKeys[e.Row.RowIndex].Value.ToString();
GridView gvOrders = e.Row.FindControl("gvOrders") as GridView;
gvOrders.DataSource = GetData(string.Format("select ID,FunctionDate,FunctionTime,CelebrateName from function where ID='{0}'", ID));
gvOrders.DataBind();
}
}
}
}
答案 0 :(得分:1)
条件表达式是包含的查询的一部分 条件,如WHERE。
问题似乎在于您的where子句,您试图将ID列值(可能是整数)与字符串进行比较。 试试这个,我删除了{0}附近的单引号:
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string ID = gvFunction.DataKeys[e.Row.RowIndex].Value.ToString();
GridView gvOrders = e.Row.FindControl("gvOrders") as GridView;
gvOrders.DataSource = GetData(string.Format("select ID,FunctionDate,FunctionTime,CelebrateName from function where ID={0}", ID));
gvOrders.DataBind();
}
}
答案 1 :(得分:0)
您的ID字段是int还是字符串字段?尝试将字符串更改为int,并删除{0}周围的引号。
答案 2 :(得分:0)
一个非常粗略的猜测是,该字符串ID包含一些奇怪的东西......例如“空”
检查一下 string.Format(“select ID,FunctionDate,FunctionTime,CelebrateName from function where ID ='{0}'”,ID) 看起来并在这里发布最终的sql语句,我想我们可以在这里看到原因。