|
-
May 30th, 2005, 03:04 AM
#1
Thread Starter
Addicted Member
how to remove hardcoding?
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.
Think Before Ink
Visual Studio .NET 2002/.NET Framework 1.0
-
May 30th, 2005, 03:31 AM
#2
New Member
Re: how to remove hardcoding?
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")
-
May 30th, 2005, 03:34 AM
#3
Thread Starter
Addicted Member
Re: how to remove hardcoding?
Is it necessary to write the coding in Xml as u done, bcos I'm using Windows application.
Think Before Ink
Visual Studio .NET 2002/.NET Framework 1.0
-
May 30th, 2005, 03:37 AM
#4
Lively Member
Re: how to remove hardcoding?
i think there should be app.path type attrebut...
-
May 30th, 2005, 03:49 AM
#5
Thread Starter
Addicted Member
Re: how to remove hardcoding?
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?
Think Before Ink
Visual Studio .NET 2002/.NET Framework 1.0
-
May 30th, 2005, 04:31 AM
#6
Re: how to remove hardcoding?
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)
-
May 30th, 2005, 08:52 PM
#7
Thread Starter
Addicted Member
Re: how to remove hardcoding?
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??
Regards,
Hems.
Think Before Ink
Visual Studio .NET 2002/.NET Framework 1.0
-
May 30th, 2005, 09:03 PM
#8
Re: how to remove hardcoding?
 Originally Posted by haihems
That shouldn't matter, because the app.config file is already created for you
-
May 30th, 2005, 09:10 PM
#9
Thread Starter
Addicted Member
Re: how to remove hardcoding?
 Originally Posted by mendhak
That shouldn't matter, because the app.config file is already created for you
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.
Think Before Ink
Visual Studio .NET 2002/.NET Framework 1.0
-
May 30th, 2005, 09:16 PM
#10
Re: how to remove hardcoding?
 Originally Posted by haihems
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??
Regards,
Hems.
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.
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
-
May 30th, 2005, 09:19 PM
#11
Thread Starter
Addicted Member
Re: how to remove hardcoding?
Thanks jmcilhinney
Think Before Ink
Visual Studio .NET 2002/.NET Framework 1.0
-
May 30th, 2005, 09:34 PM
#12
Re: how to remove hardcoding?
 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.
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.
-
May 30th, 2005, 09:40 PM
#13
Thread Starter
Addicted Member
Re: how to remove hardcoding?
Hi jmcilhinney,
I'm new to these type of coding. Can u explain this with some coding, pls.. thanks.
Regards,
Hems.
Think Before Ink
Visual Studio .NET 2002/.NET Framework 1.0
-
May 31st, 2005, 01:18 AM
#14
Re: how to remove hardcoding?
This is the code I use to serialise, or save, 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()
This is the code I use to deserialise, or load, a ProgramOptions object:
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
Note that to use this code "as is" I have imported the System.IO and System.Xml.Serialization namespaces.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|