Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Filter expression '1' does not evaluate to a Boolean term. how to solve this error .when i click button only this error came
C#
public DataSet Fill_Dataset()
    {
        DataSet ds = new DataSet();

        con = new SqlConnection(str);
        con.Open();
        cmd = new SqlCommand("sp_new", con);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds);

        DataView dv = ds.Tables[1].DefaultView;

        if(ds.Tables[0].Rows.Count>1)
        {
            for (int i = 0; i < ds.Tables[0].Rows.Count;i++ )
            {
                DataRow dr = ds.Tables[0].Rows[i];
                DataTable dt1;
                dv.RowFilter=dr["country_id"].ToString();
                dt1 = dv.ToTable();
                ds.Tables.Add(dt1);
            }
        }
        return ds;
}

protected void excel_generation_Click(object sender, EventArgs e)
    {
        try
        {
            Export_Excel();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    void Export_Excel()
    {
        con = new SqlConnection(str);
        con.Open();
        DataSet ds = Fill_Dataset();

        using (XLWorkbook wb = new XLWorkbook())
        {
            wb.Worksheets.Add(ds);
            wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            wb.Style.Font.Bold = true;
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "";
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AddHeader("content-disposition", "attachment;filename=Summary_Rpt.xlsx");

            using (MemoryStream MyMemoryStream = new MemoryStream())
            {
                wb.SaveAs(MyMemoryStream);
                MyMemoryStream.WriteTo(Response.OutputStream);
                Response.Flush();
                Response.End();
            }
        }
    }
Posted
Updated 18-Feb-15 18:25pm
v3

Most like the error is on row
C#
dv.RowFilter=dr["country_id"].ToString();

If country_id column contains a value of 1 then the filter would be just "1".

For the filter you need to have a proper boolean evaluation so that the dataview can decide if the row is to be included or not. For example
C#
dv.RowFilter="country_id = 1";
 
Share this answer
 
Comments
Member 10918596 19-Feb-15 0:37am    
i used datarow in one table.how to map this table to data row
Member 10918596 19-Feb-15 0:41am    
i used to two table table...my stored procedure

ALTER PROCEDURE [dbo].[sp_new]

AS
BEGIN
declare @sql varchar(max)
SET NOCOUNT ON;

select @sql='select * from country;select * from state'
exec(@sql)
print(@sql)

END
Wendelius 19-Feb-15 1:03am    
If you want to use the data from another datatable as a filter, concatenate the value to the filter property. Like

dv.RowFilter="country_id = " + dr["country_id"].ToString();

I didn't quite understand how the stored procedure is related to this...
DataSet ds = new DataSet();
DataSet dstemp = new DataSet();

con = new SqlConnection(str);
con.Open();
cmd = new SqlCommand("sp_new", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);

DataView dv = ds.Tables[1].DefaultView;

if(ds.Tables[0].Rows.Count>1)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
DataRow dr = ds.Tables[0].Rows[i];

dv.RowFilter = "country_id = " + dr["country_id"];

dstemp.Tables.Add(dv.ToTable(dr["country_name"].ToString()));
}
}
return dstemp;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900