What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 37676 |  Welcome, Guest!   Register  Login
 Home > Forums > ASP.NET > how to use a using block in this code? ...
Balajikcp8

how to use a using block in this code?

Replies: 1 | Posted by: Balajikcp8 on 8/18/2012 | Category: ASP.NET Forums | Views: 236 | Status: [Member] | Points: 10  


public class Connection
{
private SqlConnection conn;

public Connection()
{
conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
}

public SqlConnection Openconn()
{
if(conn.State == ConnectionState.Closed || conn.State == ConnectionState.Broken)
{
conn.Open();
}
return conn;
}
public void ExecuteQuery(string txtQuery)
{

SqlCommand cmd = new SqlCommand();
cmd.Connection = Openconn();
cmd.CommandText = txtQuery;
cmd.CommandTimeout = timeout;
cmd.ExecuteNonQuery();
}
public SqlConnection Close()
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
return conn;
}
}


Reply | Reply with attachment | Alert Moderator

 Responses below this adGet hundreds of .NET Tips and Tricks videos

 Replies

Vasanthmvp
Vasanthmvp  
Posted on: 8/19/2012 7:23:22 AM
Level: Starter | Status: [Member] | Points: 25


Hi, I believe this is what you are looking for..??
If you are trying to use the same with "Using" block, its a best practice to use( as far as i know).

In class:
string connstr = ConfigurationManager.ConnectionString["Conn"].ConnectionString; or .ToString(); //"Conn" is the name of the connectionstrings that we include in the web.config file.

In method Page_Load or any other:

using (sqlconnection conn = new sqlconnection(connstr))
{

if(conn.State == ConnectionState.Closed || conn.State == ConnectionState.Broken)
{
conn.Open(); // its better to open your connection before executing your command.
}

string sql = "write your sql query here"; // if you have a stored procedure, this is not necessary.

using (sqlcommand cmd = new sqlcommand( sql, conn))
{
// if there are any parameters, declare them and add the parameter.
// cmd.Parameters.Add(prm); if no paramerts then no need to add them.

// If you have to fill the data use some data adapter.


conn.Open();
returnvalue = cmd.ExecuteNonQuery();
conn.End();
}
}



Regards,

Awesome Coding !! :)

Balajikcp8, if this helps please login to Mark As Answer. | Reply | Alert Moderator 

Reply - Please login to reply


Click here to login & reply

Found interesting? Add this to:


 Latest Posts

Write New Post | More ...

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/22/2013 9:35:59 AM