Click here to Skip to main content
15,850,445 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey Guys,

Just wanted to know that is there any way to extract sql data connection and its commands(i.e. insert and select).

And when needed just call it from one word code?
protected void insrt_btn_Click(object sender, EventArgs e)
    SqlConnection conn = new SqlConnection("Data Source=;Initial Catalog=DataRecord;User ID=sa;Password=super");
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "Insert into EntryTableTest(Name, [N.I.C], EntryDatetime, ToSee, PurposeOfVisit, VehicalNO) values('" + name_txt.Text + "','" + nic_txt.Text + "','" + System.DateTime.Now + "','" + tosee_txt.Text + "','" + pov_txt.Text + "','" + veh_txt.Text + "')";
private void bindGrid()
    string connectionString = "Data Source=;Initial Catalog=DataRecord;User ID=sa;Password=super";
    string sql = "SELECT * FROM EntryTableTest";

    using (var connection = new SqlConnection(connectionString))
    using (var command = new SqlCommand(sql, connection))
    using (var adapter = new SqlDataAdapter(command))
        var dataEntry1 = new DataTable();
        GridView1.DataSource = dataEntry1;
        GridView1.Visible = true;
Not clear. Could you explain again what exactly you want?
xibit89 20-Aug-13 4:48am    
just wanted to remove sql connection string. and to put it into another class or function

and when needed the connection string just want to call it from the other class or function
Okay... I have added one answer. Please check.

Sample application using WinForms, C#.NET, ADO.NET and MS Access[^]

This article might give you some tip on storing the connection string in a config file and accessing it. Try to understand the 3-tier approach discussed in the article. It will help you separate the data-access code from the presentation logic.
Share this answer
xibit89 20-Aug-13 4:53am    
Thanks buddy!
John-ph 20-Aug-13 5:04am    
You're welcome!
xibit89 20-Aug-13 4:53am    
Exactly what I needed..
John-ph 20-Aug-13 5:14am    
glad :)
Store it in app.config or web.config or create a Constant Class and store there as a Variable.

Check the article - Read or Write connection strings in web.config file using[^] for one example.
Share this answer

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