vaquas khan

vaquas khan

  • NA
  • 45
  • 0

problem overriding connection string from web.config file

Nov 10 2010 1:08 AM
hi all,

i want use my web.config file connectionstring to my crystal report.my code is given below.

protected void Page_Load(object sender, EventArgs e)
    {
        TextBox1.Text = Session["Selected"].ToString();
        TextBox2.Text = Session["ReportBy"].ToString();
        TextBox3.Text = Session["FromDate"].ToString();
        TextBox4.Text = Session["ToDate"].ToString();
       ReportDocument repDoc = new ReportDocument();
       string MyconnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
     SqlConnectionStringBuilder SConn = new SqlConnectionStringBuilder(MyconnectionString);
      repDoc.DataSourceConnections[0].SetConnection(SConn.DataSource, SConn.InitialCatalog, SConn.UserID, SConn.Password);
       repDoc.Load(Server.MapPath("Report/RptExpiryListByitem.rpt"));
ParameterDiscreteValue objDiscreteValue;
        ParameterField objParameterField;
        objDiscreteValue = new ParameterDiscreteValue();
        objParameterField = new ParameterField();
        objDiscreteValue.Value =TextBox1.Text;
        objParameterField = CrystalReportViewer1.ParameterFieldInfo["@strIDs"];
        objParameterField.CurrentValues.Add(objDiscreteValue);
        CrystalReportViewer1.ParameterFieldInfo.Add(objParameterField);
        objDiscreteValue.Value = TextBox2.Text;
        objParameterField = CrystalReportViewer1.ParameterFieldInfo["@intFilterType"];
        objParameterField.CurrentValues.Add(objDiscreteValue);
        CrystalReportViewer1.ParameterFieldInfo.Add(objParameterField);
        objDiscreteValue.Value = TextBox3.Text;
        objParameterField = CrystalReportViewer1.ParameterFieldInfo["@dtFromDate"];
        objParameterField.CurrentValues.Add(objDiscreteValue);
        CrystalReportViewer1.ParameterFieldInfo.Add(objParameterField);
        objDiscreteValue.Value = TextBox4.Text;
        objParameterField = CrystalReportViewer1.ParameterFieldInfo["@dtToDate"];
        objParameterField.CurrentValues.Add(objDiscreteValue);
        CrystalReportViewer1.ParameterFieldInfo.Add(objParameterField);
        CrystalReportViewer1.ReportSource =repDoc;
    }

when i run my apllication and pass the parameter to the report its give the error ..

System.NullReferenceException: Object reference not set to an instance of an object.

at line 
 string MyconnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
please anyone provide me the solution for this problem..

thanks


Answers (3)