Click here to Skip to main content
15,887,464 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
I developed a Windows Application using C# 2.0 and SQL Server 2005. In this application, I establish SQL Server connection using a form in which I used three textboxes, one combobox and two buttons controls like this
Server Name :- textBox1
Authentication:- comboBox1
User ID:- textBox2
Password:- textBox3
Login:- button1 Cancel:-button2

Code Sample:-
C#
private void button1_Click(object sender, EventArgs e)
        {
            Class1.cs = "Data Source= " + textBox1 + ";Initia Catalog=Kolkata_Dental;User ID=" + textBox2.Text + ";Password=" + textBox3.Text + "";
                    SqlConnection con = new SqlConnection();
                    con.ConnectionString = Class1.cs;
                    con.Open();
                    Class1.connection = true;
                    MessageBox.Show("Connection estabilished Successfully!!!");
                    con.Close();
                    Home_Page frm = new Home_Page();
                    frm.Show();
                    this.Hide();
        }


Now, I don't want to establish the SQL connection by using those controls. I want to create the connection directly in load event of that form without clicking a button control. Is it possible Sir? Please give me good solution.
Posted
Updated 28-Aug-13 1:53am
v5

I'm not absolutely sure what your problem is with doing this, but I'm guessing that you need to store the connection string once the user has got it working, and re-establish that string when the form loads. Fortunately, this is pretty simple: just use the Settings facility.
There are two ways you might want to do this: store the textbox values that make up the string (so you can load them into the textboxes if the user needs to change it) or store the whole string - most systems work on the whole string as the individual parts are not normally changed often. The principle is the same either way.

Open your project in VS, and open the Properties drop down in the Solution Explorer.
Double click the "Settings.settings" line.
In the resulting page, add a suitable name in the "Name" column ("strConnect" for example) and leave everything else as "String", "User" and blank (unless you want a default value, in which case replace the blank with the connection string.
Close the page (it may ask you to save, if it does, do so.

In your form Load event you can know access the string via
C#
string strConnect = Properties.Settings.Default.strConnect;
You use the same principle to store the various parts, with a separate setting for each.

If this isn't what you are trying to do, then you need to explain in better detail.
 
Share this answer
 
You could also use the registry for storing the connection string. Take a look at this Read, write and delete from registry with C#[^]. The first time you will need to initialize it with defaults, later on you may add some support in your program for editing the connection string.

For security reasons, it is not a good practice/idea to store credentials in plaint-text, take a look also to this, it could help: Encrypt and Decrypt Data with C#[^]
 
Share this answer
 
Yes You can use,

But you should assign the values with the corresponding textboxes.

Like

textBox1.Text=Server Name

textBox1.Text :User ID:
textBox3 Password
 
Share this answer
 
Comments
[no name] 28-Aug-13 8:16am    
You mean like he is already doing? And User ID is textbox2 not textBox1 again.
manikjeyam 28-Aug-13 8:23am    
sry Textbox2.text=userId

ypu already assign all the text box in page load event are design

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900