How to use different connection strings in asp.net application

By
There are some connection strings in asp.net for database connectivity.There are mainly two architecture are used in ADO.NET for database connectivity in asp.net application.
  1. Connected architecture
  2. Disconnected architecture
If we add .mdf file(database) in asp.net application(website) or create table in sql server database then we can use different connection strings for database connectivity.I have seen many students face problem in database connectivity in asp.net application. that's why i am going to explain database connectivity in asp.net application.In this tutorial i am going to insert some values in table with help of different-different connection strings.You can use any of one connection strings to perform the insertion,deletion and updation operations in database.It is very easy concept.If you understand this whole concept which i have explained below then you never face any type of connectivity problem in database in future.  
  In below application i have performed only insertion operation with the help of different connection strings.In this application i have used six different connection strings(only four main strings) which are given below.
There are some steps to implement this whole concepts which are given below:-
Step 1:-First open your visual studio -->File -->New -->Website -->Select ASP.NET Empty website --> OK-->Drag and Drop Text Box,Label ,Button ,SqlDataSource and two Gridview controls as shown below.

layout

Step 2:-Now open your Sql server Database -->create a students table and a stored procedure prcinsert as shown below:-

commands

Step 3:-Now Double click on page (or press F7) -->Write the below codes on page load as given below:-



using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 //----------------------------------Conneted architecture codes------------------------------------------------
        
        //SqlConnection con = new SqlConnection("data source=DIAMOND-PC;Integrated Security=Yes;Database=master");
        //con.Open();
        //SqlCommand cmd = new SqlCommand("select*from students",con);
        //SqlDataReader dr = cmd.ExecuteReader();
        //DataTable dt =new DataTable();
        //DataColumn dcname=new DataColumn("name",typeof(String));
        //DataColumn dcage=new DataColumn("age",typeof(int));
        //DataColumn dcloc=new DataColumn("location",typeof(String));
        //dt.Columns.Add(dcname);
        //dt.Columns.Add(dcage);
        //dt.Columns.Add(dcloc);
        //while (dr.Read())
        //{
        //    dt.Rows.Add(dr[0], dr[1], dr[2]);
        //}
        //GridView1.DataSource = dt;
        //GridView1.DataBind();
        //con.Close();
//----------------------------------Disconneted architecture codes------------------------------------------------
        SqlConnection con = new SqlConnection("data source=DIAMOND-PC;Integrated Security=Yes;Database=master");
        con.Open();
        SqlDataAdapter da =new SqlDataAdapter("select*from students", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }

Description :- Here i have used connected and Disconnected architecture for Binding the table ( students) data in Gridview1 control.You can use any of one connection.

Step 4:- Now Double click on submit 1 ,submit 2 ,submit 3 button -->write the following codes as given below.

protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("data source=DIAMOND-PC;Integrated Security=Yes;Database=master");
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into students values(@a,@b,@c)",con);
        cmd.Parameters.AddWithValue("a", txtname.Text);
        cmd.Parameters.AddWithValue("b", textage.Text);
        cmd.Parameters.AddWithValue("c", txtloc.Text);
        int i = cmd.ExecuteNonQuery();
        if (i > 0)
        {
            Label1.Text = "Data inserted successfully";
        }  
}
    protected void Button2_Click(object sender, EventArgs e)
    {

       SqlConnection con = new SqlConnection("data source=DIAMOND-PC;Integrated Security=Yes;Database=master");
       con.Open();
       SqlCommand cmd = new SqlCommand("insert into students values('" + txtname.Text + "','" + textage.Text + "','" + txtloc.Text + "')", con);
       int i = cmd.ExecuteNonQuery();
       if (i > 0)
       {
           Label1.Text = "Data inserted successfully";
       }

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("data source=DIAMOND-PC;Integrated Security=Yes;Database=master");
        con.Open();
        SqlCommand cmd = new SqlCommand("prcinsert",con);
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("name", txtname.Text);
        cmd.Parameters.AddWithValue("age", textage.Text);
        cmd.Parameters.AddWithValue("location", txtloc.Text);
        int i = cmd.ExecuteNonQuery();
        if (i > 0)
        {
            Label1.Text = "Data inserted successfully";
        }
    }
Note:- For Security purpose you have to use parameter value or stored procedure which have used in Button 1 and Button 3 click's codes.
Step 5:- Now open your Solution Explorer -->Add Database.mdf file on your application-->create a student 1 table as shown below:-
student 1 table
Note :-If you are facing problem in database connectivity or adding Database.mdf file on website then follow below links which are given below:-
  1. How to solve sql server Database problem in asp.net
  2. How to add .mdf file on asp.net website
Step 6:- Now configure your sql server Database and follow the Next -->Next-->Test Query -->Finish as shown below:-


configurations

Step 7: Now Double click on submit 4 ,submit 5 ,submit 6 button -->write  the following codes as given below:-


protected void Button4_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=.\;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;");
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into student1 values(@a,@b,@c)", con);
        cmd.Parameters.AddWithValue("a", txtname.Text);
        cmd.Parameters.AddWithValue("b", textage.Text);
        cmd.Parameters.AddWithValue("c", txtloc.Text);
        int i = cmd.ExecuteNonQuery();
        if (i > 0)
        {
            Label1.Text = "Data inserted successfully";
        }  
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into student1 values(@a,@b,@c)", con);
        cmd.Parameters.AddWithValue("a", txtname.Text);
        cmd.Parameters.AddWithValue("b", textage.Text);
        cmd.Parameters.AddWithValue("c", txtloc.Text);
        int i = cmd.ExecuteNonQuery();
        if (i > 0)
        {
            Label1.Text = "Data inserted successfully";
        }  
    }
    protected void Button6_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into student1 values('" + txtname.Text + "','" + textage.Text + "','" + txtloc.Text + "')", con);
        int i = cmd.ExecuteNonQuery();
        if (i > 0)
        {
            Label1.Text = "Data inserted successfully";
        }
    }
Note:- For Security purpose you have to use parameter value or stored procedure which have used in Button 4 and Button 5 click's codes.
Step 8:-Now Run the Application (press F5) --> Enter Text Box values (name,age,location) --> press any of one Submit button --> Data will be inserted in table successfully as shown below:-



Description :- In this application i have performed only insert operation in database.You can perform delete and Update operations also in database.

  • When you click Submit 1 ,Submit 2 and Submit 3 button then data will be inserted in students table which is already created in sql server Database.
  • When you click Submit 3 ,Submit 4 and Submit 5 button then data will be inserted in student 1 table which is already created in this website (.mdf).
  • Page load code is used when you want to display the table (students or student 1) data in Gridview control.
  • If you want to display the Database.mdf file's table data then you will have to bind the table data in Gridview control  by configuring the SqlDataSource.
  • For Real knowledge of insertion ,deletion and updation follow Data List control
For More....
  1. Create dll file for asp.net application
  2. Data set and Data Adapter in ado.net
  3. Create Data set and Data Adapter using Menu
  4. Abstract class and abstract method
  5. Make xml Document at RunTime
  6. Page Directives and page properties in asp.net
  7. Display the XML data in Gridview control
  8. Properties in c#
  9. Structure and classes in c#
  10. Generate Unique Number in asp.net application


To Get the Latest  Free Updates Subscribe

Download whole Attached  file From below
                     Download

0 comments:

Post a Comment

Powered by Blogger.