+ 1

How to write connection string in app.config file dynamicly in c# application??

14th Dec 2020, 11:19 AM
Amit Kumar Gupta
Amit Kumar Gupta - avatar
22 Réponses
+ 5
Hmmm... in that case, let's clarify a few things about your issue: 1. What type of application is this? WPF, WinForm, ASP.NET, Console, Xamarin, etc? 2. Is this application able to connect to the database from your development machine? 3. When running from your client's machine, is the application supposed to connect to the same database used by your development machine or a completely different instance of the database? 4. If this is supposed to connect to a different instance of the database, have you updated the connection string in the client's app.config file accordingly? 5. Have you tested the new connection string outside of the application to make sure it's valid? Example... connect using a database client or creating a DSN connection. 6. If you know your connection string is correct, what error messages are you receiving when attempting to connect from the application?
16th Dec 2020, 6:18 AM
David Carroll
David Carroll - avatar
+ 4
What do you mean by "dynamically"? The connection string is statically configured in the app.config (or web.config) and then used at runtime by the code. This article might help you with the specific syntax to use: https://www.c-sharpcorner.com/UploadFile/7d3362/various-ways-to-specify-connection-string-in-Asp-Net-web-app/
14th Dec 2020, 4:26 PM
David Carroll
David Carroll - avatar
+ 2
amit kumar Gupta Your response to #3 doesn't answer the question I was asking. I'm trying to figure out if the application on your development machine and your client's machine are accessing the same database. Or did you install a new database for the client machine to use? I also added notes to your last two responses. It also appears you haven't responded to questions #4 and #6. I'll wait to respond further once I know the answer to question #3.
16th Dec 2020, 5:40 PM
David Carroll
David Carroll - avatar
+ 2
If I understand correctly. You have a program that uses sql. You are looking to use this program on other PCs. And the connectionstring should be changed according to the user's needs. ok let's do it 1- at first add new project to your solution 2- Add new form. Write this form to receive connection string information from the user and save it in a file 2-in main form on constructor check if this file exists If there was read file and use this code var DBConnString = System.Configuration.ConfigurationManager.AppSettings("ConString"); //open connection var dataContext= new MyDataContext(sDBConnString); otherwise Give a message to require the user to enter information
18th Dec 2020, 4:12 PM
hossein B
hossein B - avatar
+ 2
David Carroll 👌👌 Exactly, This is also a good idea. But sometimes you have to hide important and sensitive information like password. In these projects, the file can also be hidden or Information can also be encrypted.
18th Dec 2020, 10:08 PM
hossein B
hossein B - avatar
+ 2
amit kumar Gupta Glad to hear it's working. But don't keep us in suspense. What was the issue?
19th Dec 2020, 3:22 AM
David Carroll
David Carroll - avatar
+ 1
I'm not sure what you're trying to do. I suppose you can just change the connection string during runtime and save the config value. Why do you want to change the connection string at runtime? Why not just use the configured connection string?
16th Dec 2020, 5:25 AM
David Carroll
David Carroll - avatar
+ 1
Sure... looking at the code might help.
17th Dec 2020, 2:35 AM
David Carroll
David Carroll - avatar
+ 1
Ef or ADO.NET?
17th Dec 2020, 9:33 AM
hossein B
hossein B - avatar
+ 1
hossein B Technically... you can skip the write to file bit. Just use the connection string from memory once fed by the user. Then he can tweak the string until he can verify connection is successful. 😉👌
18th Dec 2020, 9:54 PM
David Carroll
David Carroll - avatar
+ 1
Thanks friends for your help Now it's working fine
19th Dec 2020, 2:50 AM
Amit Kumar Gupta
Amit Kumar Gupta - avatar
+ 1
Suspence??😆😆😆 It's nothing like suspence friend If you are interested in application code then I can share complete application code Can you please give me your email address??
19th Dec 2020, 3:29 AM
Amit Kumar Gupta
Amit Kumar Gupta - avatar
0
What is your question? The connection string is a string. You can save it in a variable ? Do you need to know how to write, the app.config file ? or how to do it in a secure way ?
14th Dec 2020, 1:08 PM
sneeze
sneeze - avatar
0
But I want change connection string at application first run at another computer
16th Dec 2020, 4:21 AM
Amit Kumar Gupta
Amit Kumar Gupta - avatar
0
Please help
16th Dec 2020, 4:22 AM
Amit Kumar Gupta
Amit Kumar Gupta - avatar
0
Sorry man I don't understand I am a fresher and I am trying to build an application to manage a daybook Application is almost complete but when I am trying to test on a Clint machine application is not connecting to database and I don't know what to do
16th Dec 2020, 6:01 AM
Amit Kumar Gupta
Amit Kumar Gupta - avatar
0
زه خپله پرې صحي نه پوهېږم وروره نو تاسو ته به څه وښایم!
16th Dec 2020, 6:33 AM
Abdul Qayom Silab🇦🇫
Abdul Qayom Silab🇦🇫 - avatar
0
1 my application is a win form application 2 yes application is able to connect to the database on my development machine 3 no my application is not connecting any instance on Clint machine
16th Dec 2020, 7:10 AM
Amit Kumar Gupta
Amit Kumar Gupta - avatar
0
5. I am not yet tested a new connection string of the out side application Edit by David: amit kumar Gupta I assume you meant to list this as a response to #5. You originally numbered this as 4.
16th Dec 2020, 7:14 AM
Amit Kumar Gupta
Amit Kumar Gupta - avatar
0
when I configar SQL server instance and machine name on Clint machine as on my machine it work Edit by David: amit kumar Gupta You originally numbered this as 6. However, my question for #6 was in regards to providing error messages that can be reviewed.
16th Dec 2020, 7:16 AM
Amit Kumar Gupta
Amit Kumar Gupta - avatar