How to create a dynamic query in a local report(.rdlc) in c#

浪子不回头ぞ 提交于 2019-12-25 05:08:40

问题


In my application I have 3 report.rdlc files that records are not different I am creating a query in a form with options buttons, checkboxes and combobox values

string qry = "";
SqlCeConnection cnn = new 

SqlCeConnection(Properties.Settings.Default.ConnectionString.ToString());
    SqlCeCommand cmd = new SqlCeCommand();

    if (radioButton1.Checked == true)
    {
        qry = @"Select Did,Cid,Source,Destination,Sid,cost,sdate,Driver.fname+' '+Driver.lname as driver,payed from Service,Driver where Service.Did=Driver.id";
    }
    else if (radioButton2.Checked == true)
    {
        button3.Enabled = true;
        qry = @"Select Did,Cid,Source,Destination,Sid,cost,sdate,Driver.fname+' '+Driver.lname as driver,payed from Service,Driver where Service.Did=Driver.id and Did=@param ";

        cmd.Parameters.Add("@param", CmbDriver.SelectedValue);
    }

    if (payedRB.Checked == true)
    {
        qry += @" and Service.payed=1";
    }
    else if (notpayedRB.Checked == true)
    {
        qry += @" and Service.payed=0";
    }

    if (txtDate1.Text != "" && txtDate2.Text != "")
    {
        qry += @" and Service.sdate between @pdate1 AND @pdate2";
        cmd.Parameters.Add("@pdate1", SqlDbType.NVarChar, 20).Value = txtDate1.Text.Trim();
        cmd.Parameters.Add("@pdate2", SqlDbType.NVarChar, 20).Value = txtDate2.Text.Trim();
        state = 3;
    }
    else if (txtDate1.Text != "")
    {
        qry += @" and Service.sdate >= @pdate1";
        cmd.Parameters.Add("@pdate1", SqlDbType.NVarChar, 20).Value = txtDate1.Text.Trim();
        state = 1;
    }
    else if (txtDate2.Text != "")
    {
        qry += @" and Service.sdate <= @pdate2";
        cmd.Parameters.Add("@pdate2", SqlDbType.NVarChar, 20).Value = txtDate2.Text.Trim();
        state = 2;
    }

    if (activdr.Checked == true)
    {
        qry += @" and Driver.isActive=1";
    }
    else
    {
        qry += @" and Driver.isActive=0";
    }

    qry += @" ORDER BY Service.sdate desc,Service.Sid desc";

    cmd.Connection = cnn;
    cmd.CommandText = qry;
    SqlCeDataAdapter adapter = new SqlCeDataAdapter(cmd);


                cnn.Open();

                adapter.Fill(dt);......

I used reporttype variable in option buttons event to know which report will be shown in report viewer. and set selected records to data grid viewer

my reports get data from a temp table that is compatible with record's field. before this I insert all records( that will be printed in the report viewer) into temp table;

How can I transfer records into reports without temp table;


回答1:


In local reports it is not possible to have the report itself query the database. Your application needs to query the database and pass the results to the report as a data source.

You need to fill a data set now and pass this as a DataSource to the report. The report needs to define a respective data source to provide values for the fields.



来源:https://stackoverflow.com/questions/22062282/how-to-create-a-dynamic-query-in-a-local-report-rdlc-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!