C# System.Data.OleDb.OLEDBEException在System.Data.dll中出现,但未在用户代码中处理

C# System.Data.OleDb.OLEDBEException在System.Data.dll中出现,但未在用户代码中处理,c#,asp.net,oledbexception,C#,Asp.net,Oledbexception,每当我运行这段代码时,我总是会遇到这个错误,我真的找不到我在这里缺少的东西,所以请帮助我,提前谢谢 错误: System.Data.OleDb.OLEDBEException在System.Data.dll中出现,但未在用户代码中处理 其他信息:条件表达式中的数据类型不匹配 前端代码: <asp:GridView ID="gvFunction" runat="server" AutoGenerateColumns="false" CssClass="Grid" DataKeyNam

每当我运行这段代码时,我总是会遇到这个错误,我真的找不到我在这里缺少的东西,所以请帮助我,提前谢谢

错误: System.Data.OleDb.OLEDBEException在System.Data.dll中出现,但未在用户代码中处理

其他信息:条件表达式中的数据类型不匹配

前端代码:

<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();
        }
    }
}
}

您的ID字段是整型字段还是字符串字段?尝试将字符串更改为int,并删除{0}周围的引号

条件表达式是包含 条件,如在哪里

问题似乎出在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();
        }
    }

一个非常粗略的猜测是,该字符串ID包含一些奇怪的内容。。e、 g.空

检查什么 string.Formatselect ID、FunctionDate、FunctionTime、CelebrateName来自函数,其中ID='{0}',ID
看起来像是最后的sql语句,我想我们可以在这里看到原因。

它在自动编号上,我想它是int?如果是,请告诉我如何替换value.Tostring?因为我对@sachin首先提供的代码相当业余,我想这就足够了。