How to Establish Connection string With Insert Records from C#,.Vb,MVC Different Approcah

Through-out the article we will learn step-by-step: How to perform Establish Connection string Dotnet Framework(C#,,MVC). We will discuss. How to Connect the Database then Retrieve the Data and Binding Gridview Controls (,C#.MVC).


Through-out the article we will learn step-by-step: How to perform Establish Connection string Dotnet Framework.

We will discuss How to Connect the Database then Retrieve the Data and Binding Gridview Controls (C#,,MVC) of pure Sql server


In these days, Dotnet framework is most growing framework and as a developer I am curious to know and learn about the new features of this framework.

From last couple of days I was wondering how can I perform Establish Connection string Dotnet Framework.

We will discuss How to Connect the Database then Retrieve the Data and Binding Gridview Controls (,C#.MVC) of pure Sql server. For the same I have gone through various forums etc. Unfortunately, I did saw very few posts/articles for the same.

Finally, I tried and came with a solution to perform Connection string Establish operations from Server side . In this article, I am going to share my findings with all of you.

Why Step-by-Step

Ah! This question came to my mind while I was finding the solution of my problem, I jotted down few points and finally called them as a steps to perform Connection string from connect the database Operations.


To implement the solution and to feel the taste of code, you should:
<li>Have Visual Studio 2013 or later with the support of Dotnet</li>
<li>Have Basic Idea of SQL Server</li>
<li>Have Basic Idea of Sql queries</li>

<h1>What we are waiting for?</h1>
In this Article I have explain different type of connection string we can access db differed Approch

Web Application - C#,Sql -> Static and Stored Procedure.
<h1>This code Implemented c# code for Connect db and fetch the records and binding the
web application Control</h1>

string connectionstring = "Data source=PCNAME; Initial Catalog=Test; Integrated Security=true";
SqlConnection conn = new SqlConnection(connectionstring);
SqlCommand comm = new SqlCommand("SELECT * from tst111 where studid=1123", conn);
SqlDataAdapter da = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
Grd1.DataSource = ds;
protected void Btsubmit_Click(object sender, EventArgs e)
SqlConnection conn = new SqlConnection(connectionstring);
SqlCommand comm = new SqlCommand("Insert into StudentEnq values("+ TxtStudId.Text + ",'"+ TxtName.Text + "')",conn);
int Result=comm.ExecuteNonQuery();

protected void Submit_Click(object sender, EventArgs e)

SqlConnection con = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand("AddStudInfo", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@StudId", SqlDbType.Int).Value = Convert.ToInt32(TxtStudId.Text);
cmd.Parameters.Add("@studname", SqlDbType.VarChar, 50).Value = TxtName.Text.ToString();
int Result=cmd.ExecuteNonQuery();

Html Design page this

Student ID
<asp:TextBox ID="TxtStudId" runat ="server"></asp:TextBox>
Student Name
<asp:TextBox ID="TxtName" runat ="server"></asp:TextBox>
<td colspan="2">
<asp:Button ID="Btsubmit" runat ="server" Text ="Submit" OnClick="Btsubmit_Click" />
<asp:Button ID="Submit" runat ="server" Text ="Stored Proc" OnClick="Submit_Click" />

<h1>How to use connection string from webconfig</h1>

<add name="TestConnectionString" connectionString="Data Source=PCNAME;Initial Catalog=Test;Integrated Security=True" providerName="System.Data.SqlClient" >

<h1>How to Establish Connection string from Console Application </h1>

Console Application

SqlConnection MyConn = new SqlConnection("server=PCNAME;database=Test;Integrated Security=SSPI");
SqlCommand command = MyConn.CreateCommand();
command = new SqlCommand("SELECT * FROM TblEmp", MyConn);
using (SqlDataReader reader = command.ExecuteReader())
Console.WriteLine("Emp Id\t\t\t\t\t Name \t\t\t\t\t\t EmpNo\t");
while (reader.Read())
Console.WriteLine(String.Format("{0} \t\t\t | {1} \t | {2}",
reader[0], reader[1], reader[2]));
Console.WriteLine("Data displayed! Now press enter to move to the next section!");

<h1>How to Establish Connection string from Web Application </h1>

Imports System.Data
Imports System.Data.SqlClient
Public Class WebForm1
Inherits System.Web.UI.Page
Dim Sqlcon As New SqlConnection("Data Source=PCNAME;Initial Catalog=Test;Integrated Security=True")
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dt As New DataTable
Dim sqladp As New SqlDataAdapter("Select * from tbltmp", Sqlcon)
End Sub
End Class

Protected Sub Btsubmit_Click(sender As Object, e As EventArgs)
Dim sqlcmd As New SqlCommand("Insert into StudentEnq values(" + TxtStudId.Text + ",'" + TxtName.Text + "')", Sqlcon)
Dim i As Integer = sqlcmd.ExecuteNonQuery()
End Sub

<h1>How to Establish Connection string from EDMX and MVC Patteren </h1>

//Model -1

//Model -2
lstemp = db.Tables.Where(x=> x.Id==2);

List<EmployeeList> lstemp=new List<EmployeeList>();
// GET: Employee

Database1Entities db = new Database1Entities();

<add name="Database1Entities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\Database1.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />

How to Insert Records using Edmx Concept

public ActionResult Create()
return View();

public ActionResult Create(StudentEnq empcls)
return View();

Entry form View this

<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.StudId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.StudId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.StudId, "", new { @class = "text-danger" })

<div class="form-group">
@Html.LabelFor(model => model.StudName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.StudName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.StudName, "", new { @class = "text-danger" })

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />

Entry screen view this:

Entry Screen Add Records EDMX in MVC

How to Insert Records using MVC(EDMX) Concept:


How to List Records using MVC(EDMX) Concept:

Retrieve List Value

Save the Database using Edmx

Hope it will helpful to you.

Save the Database using Edmx

Entry the Data Screen


Save Record using Stored Prodecure insert



No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: