Results 1 to 14 of 14

Thread: how to remove hardcoding?

  1. #1

    Thread Starter
    Addicted Member haihems's Avatar
    Join Date
    Oct 2004
    Posts
    150

    Question 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

  2. #2
    New Member
    Join Date
    May 2005
    Posts
    2

    Thumbs up 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")

  3. #3

    Thread Starter
    Addicted Member haihems's Avatar
    Join Date
    Oct 2004
    Posts
    150

    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

  4. #4
    Lively Member skv_noida's Avatar
    Join Date
    May 2005
    Location
    Noida, India
    Posts
    76

    Re: how to remove hardcoding?

    i think there should be app.path type attrebut...

  5. #5

    Thread Starter
    Addicted Member haihems's Avatar
    Join Date
    Oct 2004
    Posts
    150

    Re: how to remove hardcoding?

    VB Code:
    1. Dim crTables As Tables
    2. Dim crTable As Table
    3. Dim crDoc As New ReportDocument
    4. Dim RptName As String
    5. Dim BookLocation As String = "C:\Abc\" & BookName & ".xls"
    6.  
    7. RptName = Directory.GetCurrentDirectory & "\Reports\Bal_Sht.rpt"
    8. crDoc.Load(RptName)
    9. crTables = crDoc.Database.Tables
    10. For Each crTable In crTables
    11.      crTable.Location = BookLocation
    12. Next
    13. 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

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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:
    1. Dim reportFullPath As String = String.Format("{0}\{1}.rpt", Application.StartupPath, reportName)

  7. #7

    Thread Starter
    Addicted Member haihems's Avatar
    Join Date
    Oct 2004
    Posts
    150

    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

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: how to remove hardcoding?

    Quote Originally Posted by haihems
    Is it necessary to write the coding in Xml as u done, bcos I'm using Windows application.
    That shouldn't matter, because the app.config file is already created for you

  9. #9

    Thread Starter
    Addicted Member haihems's Avatar
    Join Date
    Oct 2004
    Posts
    150

    Re: how to remove hardcoding?

    Quote 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

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: how to remove hardcoding?

    Quote 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:
    1. Try
    2.     'Execute desired code here.
    3. Catch ex As Exception
    4.     'Handle exceptions here.
    5. Finally 'Optional
    6.     'Put cleanup code here that will be executed whether an exception occurs or not.
    7. End Try

  11. #11

    Thread Starter
    Addicted Member haihems's Avatar
    Join Date
    Oct 2004
    Posts
    150

    Re: how to remove hardcoding?

    Thanks jmcilhinney
    Think Before Ink

    Visual Studio .NET 2002/.NET Framework 1.0

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: how to remove hardcoding?

    Quote 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.

  13. #13

    Thread Starter
    Addicted Member haihems's Avatar
    Join Date
    Oct 2004
    Posts
    150

    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

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: how to remove hardcoding?

    This is the code I use to serialise, or save, a ProgramOptions object:
    VB Code:
    1. Dim optionsSerialiser As New XmlSerializer(GetType(ProgramOptions))
    2.             Dim optionsStream As New FileStream(Application.StartupPath + Path.DirectorySeparatorChar + Me.OPTIONSFILENAME, _
    3.                                                 FileMode.Create)
    4.  
    5.             'Serialise the new options.
    6.             optionsSerialiser.Serialize(optionsStream, newOptions)
    7.             optionsStream.Close()
    This is the code I use to deserialise, or load, a ProgramOptions object:
    VB Code:
    1. Dim optionsPath As String = Application.StartupPath + Path.DirectorySeparatorChar + Me.OPTIONSFILENAME
    2.  
    3.         If File.Exists(optionsPath) Then
    4.             Dim optionsSerialiser As New XmlSerializer(GetType(ProgramOptions))
    5.             Dim optionsStream As New FileStream(optionsPath, FileMode.Open)
    6.  
    7.             'Deserialise the existing options.
    8.             Me.options = optionsSerialiser.Deserialize(optionsStream)
    9.             optionsStream.Close()
    10.         Else
    11.             'Load the default options.
    12.             Me.options = New ProgramOptions
    13.         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
  •  



Click Here to Expand Forum to Full Width