In this article I will explain uses of using statement and how to declare and use using statement in asp.net.
In Previous posts I explained lot of articles regarding Asp.net, Gridview, SQL Server, Ajax, JavaScript etc. In many of articles I used connectionStrings to get data from database.
SqlConnection con = new SqlConnection(connectionString);
con.Open();
SqlCommand cmd = new SqlCommand (commandString, con);
cmd.ExecuteNonQuery();
con.Close();
|
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand (commandString, con))
{
con.Open();
cmd.ExecuteNonQuery();
}
}
|
Using con As New SqlConnection(connectionString)
Using cmd As New SqlCommand(commandString, con)
con.Open()
cmd.ExecuteNonQuery()
End Using
End Using
|
SqlConnection con = null;
SqlCommand cmd = null;
try
{
con = new SqlConnection(connectionString);
cmd = new SqlCommand(commandString, con);
con.Open();
cmd.EndExecuteNonQuery();
}
finally
{
if (con != null)
con.Dispose();
if (cmd != null)
cmd.Dispose();
}
|
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvDetails" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
|
using System;
using System.Configuration;
using System.Web.Configuration;
|
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridviewData();
}
}
//Bind Data to Repeater Control
protected void BindGridviewData()
{
string connectionString = "Data Source=SureshDasari;Initial Catalog=MySampleDB;Integrated Security=true";
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd=new SqlCommand("select * from Repeater_Table Order By PostedDate desc",con))
{
con.Open();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
}
}
|
Imports System.Data
Imports System.Data.SqlClient
Partial Class Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGridviewData()
End If
End Sub
'Bind Data to Repeater Control
Protected Sub BindGridviewData()
Dim connectionString As String = "Data Source=SureshDasari;Initial Catalog=MySampleDB;Integrated Security=true"
Using con As New SqlConnection(connectionString)
Using cmd As New SqlCommand("select * from Repeater_Table Order By PostedDate desc", con)
con.Open()
Dim dt As New DataTable()
Dim da As New SqlDataAdapter(cmd)
da.Fill(dt)
gvDetails.DataSource = dt
gvDetails.DataBind()
End Using
End Using
End Sub
End Class
|
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
Subscribe by RSS
Subscribe by Email
|
|||
|
|
Subscribe by RSS
Subscribe by Email
6 comments :
One of the basic for which I had a doubt.
Thank u...
I have one doubt
Difference between using statement and using(namespace in csharp).
I have read all the articles are nice.
We will expect more articles from MVC concepts.
hi this tutorial is very nice.
i have one question.
how to develop cms server for a website.can you tell me the details about cms and which cms is the best ?
advance thanks to you
hi this tutorial is very nice.
i have one question.
how to develop cms server for a website.can you tell me the details about cms and which cms is the best ?
advance thanks to you
There is a floating box on the left side of this page which blocks part of the text. How do I remove it?
Thanks this was useful in general