Hi
In Crystal Reports I'm using DAO to connect to Excel. During the connectivity I'm giving the path as "C:\Abc\bin\a1.xls". (which is in my products bin directory) I dont want to hard code this part. I'm using Vb.net 2003 as my frontend.
Printable View
Hi
In Crystal Reports I'm using DAO to connect to Excel. During the connectivity I'm giving the path as "C:\Abc\bin\a1.xls". (which is in my products bin directory) I dont want to hard code this part. I'm using Vb.net 2003 as my frontend.
Give the connection path in your app.config file with some variable name
and call it as follows in your code
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ReportPath" value="E:\ABC\Reports"/>
</appSettings>
</configuration>
OledbConnectionString = System.Configuration.ConfigurationSettings.AppSettings("ReportPath")
:eek2: :afrog: Is it necessary to write the coding in Xml as u done, bcos I'm using Windows application. :confused:
i think there should be app.path type attrebut...
VB Code:
Dim crTables As Tables Dim crTable As Table Dim crDoc As New ReportDocument Dim RptName As String Dim BookLocation As String = "C:\Abc\" & BookName & ".xls" RptName = Directory.GetCurrentDirectory & "\Reports\Bal_Sht.rpt" crDoc.Load(RptName) crTables = crDoc.Database.Tables For Each crTable In crTables crTable.Location = BookLocation Next CrystalReportViewer1.ReportSource = crDoc
Actually i created a report for "C:\Abc\A.xls". During the runtime i'm changing the book name to user's bookname. But I dont know how to get the path of the .xls, whereas i know the name of the .xls... Can anybody help me?
If you know that the file you want is in a specific directory relative to your program you can use Application.StartupPath to get the location from which the application was started, e.g.VB Code:
Dim reportFullPath As String = String.Format("{0}\{1}.rpt", Application.StartupPath, reportName)
hi jmcilhinney,
Thanks. I'll try ur coding. But i've one more doubt, that will crystal reports cause any problem if the connected database not found in a specified location?If so, how can i handle that?? :eek2:
Regards,
Hems.
That shouldn't matter, because the app.config file is already created for youQuote:
Originally Posted by haihems
Ok Mendhak.. If i use that xml coding, how can I pass my path name (actually i dont know that during coding) to the 'value' tag. And where can i find this app.config and where can i write this coding??? Thanks.Quote:
Originally Posted by mendhak
Any code where factors beyond your control could cause an exception, like accessing files on disk or connecting to a database, should always be enclosed in Try blocks. If you aren't familiar with structured exception handling you should lookup the "Try" keyword in the help for an explanation. Here is a very brief example.Quote:
Originally Posted by haihems
VB Code:
Try 'Execute desired code here. Catch ex As Exception 'Handle exceptions here. Finally 'Optional 'Put cleanup code here that will be executed whether an exception occurs or not. End Try
Thanks jmcilhinney :)
You have to add a configuration file to your project using the Solution Explorer. You then access its contents using members of the System.Configuration namespace. The purpose of a configuration file is to allow various program settings to be altered easily because the config file is human-readable XML and can be edited using Notepad. The problem is that the values (other than dynamic properties) cannot be altered programmatically, short of writing over the config file itself. .NET 2.0 adds some new features that fill some of the void here, but you can still store program settings in a serialisable object and persist them between sessions in an XML file. These settings can then be easily loaded, edited and saved. This is the method I always use for program settings that I want to be user-editable.Quote:
Originally Posted by haihems
Hi jmcilhinney,
I'm new to these type of coding. Can u explain this with some coding, pls.. :) thanks.
Regards,
Hems.
This is the code I use to serialise, or save, a ProgramOptions object:This is the code I use to deserialise, or load, a ProgramOptions object:VB Code:
Dim optionsSerialiser As New XmlSerializer(GetType(ProgramOptions)) Dim optionsStream As New FileStream(Application.StartupPath + Path.DirectorySeparatorChar + Me.OPTIONSFILENAME, _ FileMode.Create) 'Serialise the new options. optionsSerialiser.Serialize(optionsStream, newOptions) optionsStream.Close()Note that to use this code "as is" I have imported the System.IO and System.Xml.Serialization namespaces.VB Code:
Dim optionsPath As String = Application.StartupPath + Path.DirectorySeparatorChar + Me.OPTIONSFILENAME If File.Exists(optionsPath) Then Dim optionsSerialiser As New XmlSerializer(GetType(ProgramOptions)) Dim optionsStream As New FileStream(optionsPath, FileMode.Open) 'Deserialise the existing options. Me.options = optionsSerialiser.Deserialize(optionsStream) optionsStream.Close() Else 'Load the default options. Me.options = New ProgramOptions End If