Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need to programmatically change connection string for SqlDataAdapter

Posted on 2007-11-26
11
Medium Priority
?
2,954 Views
Last Modified: 2011-10-03
Hello there,
I have a windows application written in vb.net 2005 / ADO.net v2.
I have a couple of forms in this application in which I have used the designer to add SqlConnection and SqlDataAdapter.
Is there any way I can change the Connection String by code, rather than sticking to the built in connection string that was generated while creating the SqlConnection and SqlDataAdapter from the designer.

The reason for that is because this application is used by many users, and each user has a different connection string

It appears to me that I have to create the whole thing in code from scratch, but bearing in mind that the designer has automatically generated the 'Select', 'Insert', 'Delete', & 'Update' commands for the DataAdapter, is there an easy way to create both the SqlConnection and SqlDataAdapter, without going through the hassle of writing code for the 'Select', 'Insert', 'Delete', & 'Update' commands.

Your suggestions are well appreciated.

Thank you

===============================================================
      I need to place a code here to force the following
      DataAdapters to use a Connection String different from
      the one that was generated when I first created those adapters
      ================================================================
 
            DsChList.Clear()
            daChList.Fill(DsChList, "ChList")
            DsChGroup.Clear()
            daChGroup.Fill(DsChGroup, "ChGroup")
            DsChType.Clear()
            daChType.Fill(DsChType, "ChType")

Open in new window

0
Comment
Question by:Mehawitchi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
11 Comments
 
LVL 12

Expert Comment

by:Munawar Hussain
ID: 20352676
connection for each adapter is specified in DataSet's Designer.vs class

you need to do following to modify that
1- selection you solution (top node in projects tree)
2- show all files(hidden)
3- open the designer for Dataset and look for this method

Private Sub InitConnection()
            Me._connection = New System.Data.SqlClient.SqlConnection
            Me._connection.ConnectionString = Global.DesignGraphics.My.MySettings.Default.NorthwindConnectionString
        End Sub
****************************

My.MySettings.Default.NorthwindConnectionString

this line is showing your connectionstring key saved in your applicaion config file
here you can  spedciy any connectionstring

thanks
0
 
LVL 12

Expert Comment

by:Munawar Hussain
ID: 20352687
sorry
Designer.vb class
0
 

Author Comment

by:Mehawitchi
ID: 20353039
Many thanks needo_jee for your help.

I did find this line in the designer file, but I still didn't figure out how to change it for each user.

Knowing that each user has a different connection string stored in an ini file, and also knowing that the application will read the connection string from the ini file upon startup, the question here is how to force the sqlconnection to use the string read from ini file.

Thanks again for your great support
0
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 12

Accepted Solution

by:
Munawar Hussain earned 2000 total points
ID: 20356858
I have draged droped objects (gridview, datasource, dataadapter) all are generated by default.
In the form load event here is how im changing connectionstring for autogenerated connection
'This line is added by me
Me.CategoriesTableAdapter.Connection.ConnectionString = "Data Source=NX-ONLINE01\SQL2K;Initial Catalog=Northwind;Integrated Security=True"

'This line was added auto
        Me.CategoriesTableAdapter.Fill(Me.NorthwindDataSet.Categories)
0
 
LVL 12

Expert Comment

by:Munawar Hussain
ID: 20356869
Me.NorthwindDataSet data set following classes are being generated (for a typed dataset)

NorthwindDataSet.xsd
NorthwindDataSet.Designer.vb ' this class is supposed to hold all autogenerated code for data manipulation
NorthwindDataSet.xsc
NorthwindDataSet.xss
0
 
LVL 12

Expert Comment

by:Munawar Hussain
ID: 20356877
why you need to store connectionstrings in INI files?
cant you put in applicationconfig file and the retrieving then using configurationmanager ?
0
 

Author Closing Comment

by:Mehawitchi
ID: 31411046
Many thanks needo_jee. This was so helpful and I managed to solve my problem.
Thanks again
0
 

Author Comment

by:Mehawitchi
ID: 20360198
I inherited this application from an old timer who is still using INI files to read the custom application sttings for each user.

The idea of using application config file seems interesting and I was wondering if you are aware of any link that would provide me with more details on the subject, such as how to read/write the settings and use it in the application.
Thanks again for all your great support  
0
 
LVL 12

Expert Comment

by:Munawar Hussain
ID: 20360249
here it is explained how to use configuration manager and webconfig to store and retireve connectionstring but keep in mind .. in 2.0 you can add namespace system.configuration but for that you must add reference to dll
using System.configuration in c# and Imports System.Configuration

http://www.dotnet-guide.com/configurationmanager-class.html

also here
http://codebetter.com/blogs/john.papa/archive/2005/10/07/132852.aspx
0
 

Author Comment

by:Mehawitchi
ID: 20366390
Thanks a million needo_jee
The second link is exactly what I was looking for
Greatly appreciated
0
 
LVL 12

Expert Comment

by:Munawar Hussain
ID: 20366577
cheers...........
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question