Reputation:
i keep getting this error whenever i run this code and i really cant find what im missing here so please help me thanks in advance
the error: 'System.Data.OleDb.OleDbException' occurred in System.Data.dll but was not handled in user code
Additional information: Data type mismatch in criteria expression.
the front end code:
<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>
Back end code:
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();
}
}
}
}
Upvotes: 1
Views: 4391
Reputation: 522
a very rough guess is, that string ID contains something strange.. e.g. "null"
check what string.Format("select ID,FunctionDate,FunctionTime,CelebrateName from function where ID='{0}'", ID) looks like and post the final sql statement here, i guess we can see the cause here.
Upvotes: 0
Reputation: 2361
The criteria expression is the part of the query containing the conditions, as in WHERE .
The problem seems to be with your where clause, you are trying to compare the ID column value(which might be an integer) with a string. Try this, I have removed the single quotes around {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();
}
}
Upvotes: 1
Reputation: 120
Is your ID field an int or a string field? Try changing the string to an int, and remove the quotes around {0}.
Upvotes: 0