1

このコードを実行するたびにこのエラーが発生し続けますが、ここに欠けているものが本当に見つからないので、事前に助けてください

エラー: 'System.Data.OleDb.OleDbException' が 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();
        }
    }
}
}
4

3 に答える 3

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();
        }
    }
于 2016-08-03T07:12:33.257 に答える
0

非常に大まかな推測として、その文字列 ID には何か奇妙なものが含まれています。たとえば、「null」です。

string.Format("select ID,FunctionDate,FunctionTime,CelebrateName from function where ID='{0}'", ID) がどのように見えるかを確認し、最終的な SQL ステートメントをここに投稿してください。ここで原因がわかると思います。

于 2016-08-03T07:12:58.360 に答える
0

ID フィールドは int フィールドですか、それとも文字列フィールドですか? 文字列を int に変更して、{0} を囲む引用符を削除してみてください。

于 2016-08-03T07:08:54.827 に答える