2016-08-03 6 views
1

このコードを実行するたびにこのエラーが発生しています。事前'System.Data.OleDb.OleDbException'がSystem.Data.dllで発生しましたが、ユーザーコードで処理されませんでした

でエラー: 「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(); 
     } 
    } 
} 
} 

答えて

1

The criteria expression is the part of the query containing the conditions, as in WHERE .

問題はwhere句にあるようで、ID列の値(整数かもしれない)と文字列を比較しようとしています。

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}の周りの引用符を削除してみてください。

+0

それは自動番号で、私はそのintと思いますか?それが可能ならば、私がどのように値を置き換えることができるか教えてください。私はかなりアマチュアです。 –

+0

@sachinが提供するコードを最初に試してみてください。これで十分です。 – Dennisvdh

+0

本当に助けてくれてありがとう –

0

非常にラフな推測が、その文字列IDは奇妙な何かが含まれています。..例えば: これを試してみてください、私は{0}単一引用符の周りを削除しました"ヌル"

チェック何 String.Formatの( "選択ID、FunctionDate、FunctionTime、ID = '{0}' 関数からCelebrateName"、ID) がどのように見えると私は推測し、ここで、最終的なSQL文を投稿ここに原因を見ることができます。

関連する問題