2016-08-03 102 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列值(這可能是一個整數)用字符串。 試試這個,我已刪除單引號各地{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}周圍的引號。

+0

它的自動編號,我猜它是一個int?如果是這樣,你可以告訴我我可以如何替換value.Tostring()?因爲我相當業餘 –

+0

嘗試@sachin提供的代碼,我想這就夠了。 – Dennisvdh

+0

非常感謝幫助,非常感謝 –

0

一個非常粗略的猜測是,該字符串ID包含一些奇怪的事情..例如「空」

檢查什麼 的String.Format( 「選擇ID,FunctionDate,FunctionTime,從功能,其中ID = '{0}' CelebrateName」,ID) 的樣子,並在這裏發表最後的SQL語句,我猜我們可以在這裏看到原因。

相關問題