I am developing a software using vb.net and sql server as database.It takes name of my computer as a connection string.when i load this software on other computers it searches the name of my system.how can i define a connection string that automatically takes the name of system on which the software is loaded.

please help

You can use the My namespace. My.Computer.Name This will get the name of the computer currently running your program.

but does it work after setup file creation.and it showing error as MY is not decared

Odd. I can't answer for C#, but VB.NET has by default a namespace called My.
You can check that by typing Imports <your application name>. and in the IntelliSense there should be a selection available called My.

How are you declaring your connectionstring?

Take a look at this.

>how can i define a connection string that automatically takes the name of system on which the software is loaded.

1. Add connection string entry into app.config

<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
      <add name="CnStr" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
.....

2. Add the reference of System.Configuration assembly.

3. Retrieve connection string ,

Dim str = System.Configuration.ConfigurationManager.ConnectionStrings("CnStr").ConnectionString

if u want to change your connection string at run time. then create one txt file and put you connection string in that file and use file handling to read the connection from that file keep that variable Global for each connection now when you want to change a computer or make a setup then manually change the txt file connection string of that machine and your program will run easily. it's simple try every one.

give me replay is it work

Personally I would seriously consider the security consequences of putting the connection string into an external file.

If I were you, I would follow the advice from Mr. adapost.
It's easy and quite safe, if you also set the Build Action for app.config to Embedded Resource.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.