1.	protected void Button1_Click(object sender, EventArgs e)
2.	{string s="Data Source=Black-PC\\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True";
3.	SqlConnection Conn = new SqlConnection(s);
4.	
5.	Conn.Open();
6.	string strqry = “Insert into students values (” + TextBox1.Text +
7.	“,’” + TextBox2.Text + “‘,’” + TextBox3.Text + “‘)”;
8.	 
9.	SqlCommand Com = new SqlCommand(strqry, Conn);
10.	Com.ExecuteNonQuery();
11.	Conn.Close();
12.	 
13.	}

when i create another page i have to rewrite connection string again, can anybody explain how to avoid writing connection string variable again and again, plz help me,

Thanks adatapost. are there any video tut to learn sql database connection with C#

/*The best way of creating connetion is using a separate class for connection.. */

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;

namespace ImageUpload_Download
{
    class connection_class
    {

        public String connection_string = "Data Source=MY_PC;integrated security=true;initial catalog=master";
        public SqlConnection conn;
        public connection_class()
        {
            //default constructor here
        }
        public bool create_connection()
        {
            try
            {
                conn = new SqlConnection(connection_string);
                conn.Open();
                return true;
            }
            catch (Exception a)
            {
                System.Console.Write(a.ToString());
                return false;
            }
        }

        public bool delete_connection()
        {
            try
            {
                conn.Close();
                return true;
            }
            catch (Exception c)
            {
                System.Console.Write(c.ToString());
                return false;
            }
        }



    }

/*The best way of creating connetion is using a separate class for connection.. */

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;

namespace ImageUpload_Download
{
    class connection_class
    {

        public String connection_string = "Data Source=MY_PC;integrated security=true;initial catalog=master";
        public SqlConnection conn;
        public connection_class()
        {
            //default constructor here
        }
        public bool create_connection()
        {
            try
            {
                conn = new SqlConnection(connection_string);
                conn.Open();
                return true;
            }
            catch (Exception a)
            {
                System.Console.Write(a.ToString());
                return false;
            }
        }

        public bool delete_connection()
        {
            try
            {
                conn.Close();
                return true;
            }
            catch (Exception c)
            {
                System.Console.Write(c.ToString());
                return false;
            }
        }



    }

Thnanks a lot ,i m new to asp.net and C# how can i use this class when i inserting data, in normal way i pass Sqlconnecton variable and string variable(where i insert data)to SqlCommand, then close connection ,plzzzzzzzz
explain with me a sample code

Thnanks a lot ,i m new to asp.net and C# how can i use this class when i inserting data, in normal way i pass Sqlconnecton variable and string variable(where i insert data)to SqlCommand, then close connection ,plzzzzzzzz
explain with me a sample code

you just need to create properties in connection class and use this properties in your code wherever you want. you can define property in below way in your connection class.

public SqlConnection dbCon = new SqlConnection("your connection string or retrieve it from config file");

public SqlConnection dbConString
{
get { return dbCon; }
}

you just need to create properties in connection class and use this properties in your code wherever you want. you can define property in below way in your connection class.

public SqlConnection dbCon = new SqlConnection("your connection string or retrieve it from config file");

public SqlConnection dbConString
{
get { return dbCon; }
}

Thanks a lot

Please mark this thread as solved if you have found an answer to your question and good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.