473,388 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

several windowsforms

Hi!

I'm building a windowsapplication with several windowsforms including a
MDI-parent with MDIchildren. They are all belonging to the same namespace.
Each windowsform has its own connectionstring connecting up to one
SQL-server. This is created every time I configure a sqlDataAdapter if there
isn't a connectionstring from before.
It works fine for my purpose, but I find it bothersome when I decide to
take the application home and test it to a local SQL-server. Then I need to
redirect each connectionstring.

Is it possible for me to create one public instance of a connectionstring
for every
windowsform? If so... How??? And how do I bind my sqlDataAdapters when I
drag and drop them from toolbox into my form?

Appreciate every help!

Hans
Nov 16 '05 #1
3 1804
Hi Hans,

Actually, you should create a data access tier where all data access logic
would be encapsulated. It could be as simple as a single class designed
according to the Singleton design pattern to ensure only one instance of the
class. This class should expose methods required by the forms to read and
update the data. Thus, you will have a single point where the connection
string is defined, and it will be very easy to change it.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Hans" <ha*****@online.noHanshaarsjoonlineno@discussions. microsoft.com>
wrote in message news:DC**********************************@microsof t.com...
Hi!

I'm building a windowsapplication with several windowsforms including a
MDI-parent with MDIchildren. They are all belonging to the same namespace. Each windowsform has its own connectionstring connecting up to one
SQL-server. This is created every time I configure a sqlDataAdapter if there isn't a connectionstring from before.
It works fine for my purpose, but I find it bothersome when I decide to
take the application home and test it to a local SQL-server. Then I need to redirect each connectionstring.

Is it possible for me to create one public instance of a connectionstring
for every
windowsform? If so... How??? And how do I bind my sqlDataAdapters when I
drag and drop them from toolbox into my form?

Appreciate every help!

Hans


Nov 16 '05 #2
Hi...

I'm not really sure if I understood what you're trying to do, but any way,
here it goes...

You can create a single place for storing some application settings such as
connection strings, so let's create a class named AppConfig (or any other
name you choose):

public class AppConfig
{
public static string SqlConnectionString
{
get
{
// Retrieve the connection string from any data store
return ""; // A real connection string must be returned
}
set
{
// Save the new connection string in your data store
}
}
}

Notice that you can declare properties and any member of the AppConfig class
as
static members since you don't need an instance to access your settings.

Now let's examine what happens in your Form.
First, in the constructor you can see a call to the InitializeComponent
method, wich is generated by the Windows Forms Designer.
When you add components to your form, such as SqlConnection or
SqlDataAdapter, the Windows Forms Designer adds some code to the body of the
InitializeComponent method.
The code generated for a SqlConnection object includes the assignment of the
ConnectionString property
based on your desing-time conditions, for example if you write your
application with a server named "SERVER1", that machine name will be stored
in the connection string.
You may think you have to know the final server name in design-time and even
to have a connection to it available, but you haven't.
What you can do is something like this:

public MyForm() // Form constructor
{
InitializeComponent(); // Line added by the Windows Forms Designer
mySqlConnection.ConnectionString = AppConfig.SqlConnectionString; //
This line will override whatever the InitializeComponent method has set

// Writing your code this way, you can even test your application with
your local machine and let the Windows Forms Designer to set the server name
as "localhost"
// and while you are developing your application set the sever name to
"localhost" in your AppConfig.SqlConnectionString property, and when you
want to deploy your application
// you just have to change a small part of your code in the
AppConfig.SqlConnectionString property
}

As you can see in the definition of AppConfig.SqlConnectionString property,
you don't have to write the connection string as a string literal in your
code, because you can retrieve it from any data store for application
settings.

By doing so, you just have to write a sentence like
mySqlConnection.ConnectionString = AppConfig.SqlConnectionString; in any
Form constructor after the InitializeComponent call in your project.

Besides, you can provide your end-user with features like Settings Dialogs
and he or she will be able to set that options as desired.

A simple way to save and restore the current state of an object (all public
properties and fields) is Serialization. You can find helpful information in
the MSDN documentation.

The last thing I want to tell you is about the answer to your message sent
by Dmitry.
The better way of developing applications is based on a multi-tier model,
where you separate your whole application in modules like the Data Access
Layer, Business Rules Layer and the Presentation Layer,so the Windows Forms
never "talks" directly to the Data Access components, because the
Presentation Layer communicates with Business Layer and tthe Business Layer
interacts with the Data Access Layer.
This is the better aproach, but you have to study it in much more detail.

I hope it'll be helpful for you.

You can contact me via e-mail (av*****@hotmail.com) or via the news groups.

Bye...

"Hans <ha*****@online.no>" <Ha*****************@discussions.microsoft.com>
escribió en el mensaje
news:DC**********************************@microsof t.com...
Hi!

I'm building a windowsapplication with several windowsforms including a
MDI-parent with MDIchildren. They are all belonging to the same
namespace.
Each windowsform has its own connectionstring connecting up to one
SQL-server. This is created every time I configure a sqlDataAdapter if
there
isn't a connectionstring from before.
It works fine for my purpose, but I find it bothersome when I decide to
take the application home and test it to a local SQL-server. Then I need
to
redirect each connectionstring.

Is it possible for me to create one public instance of a connectionstring
for every
windowsform? If so... How??? And how do I bind my sqlDataAdapters when I
drag and drop them from toolbox into my form?

Appreciate every help!

Hans


Nov 16 '05 #3
Hi Alx!

I really appreciate your comments. I have stored your hotmail for later use
:-)
Thanks.
Hans.
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: William Gill | last post by:
I need to display a couple of labels and a checkbox from each entry in my database. Simple enough, but there are several hundred records, and I only want to display 5 or 10 at a time. Can this be...
3
by: Matthias Kaeppler | last post by:
Hello, this more a question about good practice than it is about the C++ language itself, but still... my question is this: I have a class representing the main window of my application which...
1
by: Toma Marinov | last post by:
Where I can download .bmp for WindowsForms buttons /and .ico too/ ? 10x!
7
by: Matt Osborne | last post by:
Good afternoon all! I am wondering if it is possible to somehow host ASP.Net inside an executable or do something similar. I am wanting create a UI similar to that of TurboTax or Microsoft...
1
by: Toma MArinov | last post by:
Hello ! First excuse me for my not very well English ! My question is : Can I use WebServices from WindowsForms Application ? Application will run in LAN and WebServices will be on the local...
5
by: Mike Lopez | last post by:
Hello. For an Intranet application, what would be some reasons to use ASP.Net and NOT use WindowsForms? I can't think of any. Thanks in advance, Mike
0
by: Jon Gabel | last post by:
I have a WindowsForms application that communicates via TCP/IP or SNMP (using IP*Works components) with various devices on my local network. I would like to duplicate the same functionality in a...
1
by: PHether | last post by:
SO, I have a WindowsForms based Usercontrol being hosted in a ASP.NET page using the <object> tag method. What I am trying to do now is have that control access the Session object of the ASP.NET...
3
by: ton | last post by:
Hi, I'm using AJAXPRO this works very well. What I want to do is to add new page elements at my web site without using a postback. And I do not mean listitems but a complete dialog. Let me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.