Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Experts,

I have tried a lot to call parameters grammatically for my sub report but failed badly, Need your suggestions in this regards.

Requirements are like that i don't want to add crystal-viewer, When user will click on Button report will directly download locally on his machine, Sample code as below:

What I have tried:

public partial class RFSW0001 : System.Web.UI.Page
{
    public string query, constr;
    public SqlConnection con;

    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["FoodRepConnectionString"].ConnectionString);
    ReportDocument rpdoc = new ReportDocument();

    ReportDocument sub_rpdoc = new ReportDocument();

    public void connection()
    {
        constr = ConfigurationManager.ConnectionStrings["FoodRepConnectionString"].ToString();
        con = new SqlConnection(constr);
        con.Open();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        //--------------Start Bind Supervisor
        if (!IsPostBack)
        {
            ddlSupervisor.AppendDataBoundItems = true;
            connection();
            String strSupQuery = "SELECT DISTINCT Parent_Code FROM Users WHERE Parent_Code !='' ORDER BY Parent_Code ASC";
            SqlConnection con = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = strSupQuery;
            cmd.Connection = con;

            try
            {
                con.Open();
                ddlSupervisor.DataSource = cmd.ExecuteReader();
                ddlSupervisor.DataTextField = "Parent_Code";
                ddlSupervisor.DataValueField = "Parent_Code";
                ddlSupervisor.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
        }
        //--------------End Bind Supervisor
        //--------------Start Bind Route
        if (!IsPostBack)
        {
            ddlRoute.AppendDataBoundItems = true;
            connection();
            String strSupQuery = "SELECT DISTINCT User_Code ,(User_Code + ' - ' + User_Description) as UsersCode FROM Users WHERE User_Type = '0' and Is_Blocked = '0' ORDER BY User_Code ASC";
            SqlConnection con = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = strSupQuery;
            cmd.Connection = con;

            try
            {
                con.Open();
                ddlRoute.DataSource = cmd.ExecuteReader();
                ddlRoute.DataTextField = "UsersCode";
                ddlRoute.DataValueField = "User_Code";
                ddlRoute.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
        }
    }
        //--------------End Bind Route

        protected void btnRFSW0001Pdf_Click(object sender, EventArgs e)
        {

            conn.Open();

            RFSW0001Search();

            rpdoc.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "RFSW0001");

            conn.Close();

        }
        int dayOfWeek;
        public void RFSW0001Search()
        {
            System.Data.SqlClient.SqlCommand cmd1 = new System.Data.SqlClient.SqlCommand();
            cmd.CommandTimeout = 120;
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_RFSW0001";
			/*
            System.Data.SqlClient.SqlCommand cmd2 = new System.Data.SqlClient.SqlCommand();
            cmd2.CommandTimeout = 120;
            cmd2.Connection = conn;
            cmd2.CommandType = CommandType.StoredProcedure;
            cmd2.CommandText = "sp_RFSW0001_CTBV";
			*/

            if (ddlSupervisor.SelectedValue == "-1")
            {
                cmd.Parameters.AddWithValue("@strSupervisor", DBNull.Value);
                
				//These are the SubReport Parameters
				//cmd2.Parameters.AddWithValue("@strSupervisor", DBNull.Value);
            }
            else
            {
                cmd.Parameters.AddWithValue("@strSupervisor", Convert.ToString(ddlSupervisor.SelectedValue));
                
				//These are the SubReport Parameters
				//cmd2.Parameters.AddWithValue("@strSupervisor", Convert.ToString(ddlSupervisor.SelectedValue));
            }
            if (ddlRoute.SelectedValue == "-1")
            {
                cmd.Parameters.AddWithValue("@strgRoute", DBNull.Value);
                
				//These are the SubReport Parameters
				//cmd2.Parameters.AddWithValue("@strgRoute", DBNull.Value);
            }
            else
            {
                cmd.Parameters.AddWithValue("@strgRoute", Convert.ToString(ddlRoute.SelectedValue));
                
				//These are the SubReport Parameters
				//cmd2.Parameters.AddWithValue("@strgRoute", Convert.ToString(ddlRoute.SelectedValue));
            }

            DateTime tempDate = System.Convert.ToDateTime(txtDate.Value.ToString());
            DateTime startOfMonth = new DateTime(tempDate.Year, tempDate.Month, 1);
            DateTime endOfMonth = new DateTime(tempDate.Year, tempDate.Month, DateTime.DaysInMonth(tempDate.Year, tempDate.Month));

            cmd.Parameters.AddWithValue("@dtpFirstDate", startOfMonth);
            cmd.Parameters.AddWithValue("@dtpLastDate", endOfMonth);

            cmd.Parameters.AddWithValue("@dtpFrom", txtDate.Value.ToString());
            cmd.Parameters.AddWithValue("@dtpTo", txtDate.Value.ToString());
			
			/* These are the SubReport Parameters
            DateTime dateInput = Convert.ToDateTime(txtDate.Value);
            DayOfWeek today = dateInput.DayOfWeek;

            string weekDay = Convert.ToString(today);

            if (weekDay == "Saturday")
            {
                dayOfWeek = 1;
            }
            else if (weekDay == "Sunday")
            {
                dayOfWeek = 2;
            }
            else if (weekDay == "Monday")
            {
                dayOfWeek = 3;
            }
            else if (weekDay == "Tuesday")
            {
                dayOfWeek = 4;
            }
            else if (weekDay == "Wednesday")
            {
                dayOfWeek = 5;
            }
            else if (weekDay == "Thursday")
            {
                dayOfWeek = 6;
            }
            else
            {
                dayOfWeek = 7;
            }

            //datecount.Text = Convert.ToString(dayOfWeek);

            cmd2.Parameters.AddWithValue("@dayNumber", Convert.ToString(dayOfWeek));
            */

            System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd);

            DataTable dt = new DataTable("sp_RFSW0001");
            try
            {
                da.Fill(dt);
            }
            catch (Exception)
            {
                //Page.Response.Redirect("NetworkError.aspx");
            }

            rpdoc.Load(Server.MapPath("reports/RFSW0001.rpt"));
            
            rpdoc.SetDatabaseLogon("sa", "ABC4RAK");
			
            rpdoc.SetDataSource(dt);
        }
Posted
Updated 22-Mar-18 2:59am
v2

1 solution

see i dont know exact solution for your question but when you are trying to display something in crystal reports so do not use parameters which passes its data to crystal report other than datasets or datatables .you will inbuilt formulas to gain this kine of requirement .this answer i have for your question regarding parameters passing.
and to download crystal report directly you will have print option automatically there will a code to trigger it
 
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