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
Manikavelu VelayuthamPosted Nov 10, 2010, 2:48 AM
You have stored the connectionstring in appsettings. Thats why the error occured.
You have to fetch from appsettings using the below code
string MyconnectionString = ConfigurationManager.AppSettings["ConnectionString"].ToString();
vaquas khanPosted Nov 10, 2010, 2:37 AM
Manikavelu VelayuthamPosted Nov 10, 2010, 1:52 AM
1. Check in IIS, whether the application pointing to correct web.config file. Becuase sometimes, the virtual directory will be pointing to different web.config file.
2. Try the below code to read the connection string, it will fetch it without any error.
Public Shared Sub ReadConnectionStrings()
' Get the ConnectionStrings collection.
Dim connections _
As ConnectionStringSettingsCollection = _
ConfigurationManager.ConnectionStrings
If connections.Count <> 0 Then
Console.WriteLine()
Console.WriteLine( _
"Using ConnectionStrings property.")
Console.WriteLine( _
"Connection strings:")
' Get the collection elements.
For Each connection _
As ConnectionStringSettings In connections
Dim name As String = connection.Name
Dim provider As String = _
connection.ProviderName
Dim connectionString As String = _
connection.ConnectionString
Console.WriteLine( _
"Name: {0}", name)
Console.WriteLine( _
"Connection string: {0}", _
connectionString)
Console.WriteLine( _
"Provider: {0}", provider)
Next
Else
Console.WriteLine()
Console.WriteLine( _
"No connection string is defined.")
Console.WriteLine()
End If
End Sub
3. Check the name of connectionstring in the web.config file, sometimes if its mismatches, it will throw this error.
4. Try to add a quick watch while debugging and see what are all the values available in the configuratino manager.
let me know if you still cant fix the issue