473,467 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

good practise question

Hi,

We have an application where users must fill the same form several times,
creating each time a record in the database.
When checking the checkbox in this form means the last record.

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim connection As SqlConnection
etc ....
connectionstr = "Data Source=LAPTOP\SQLEXPRESS;Initial
Catalog=mydb;Integrated Security=True"
etc ....
comd.CommandText = "insert into [dbo].[mytable] (field1, field2, etc ...)
values(......);"
connection.Open()
comd.ExecuteNonQuery()
....
If CheckBox1.Checked Then
Server.Transfer("other.aspx")
end if
connection.Close()
end sub

My question is: do i have to close the connection like this or leaving it
open till the last record is inserted?
With other words: where to place the "connection.Close()"? Inside the "If
CheckBox1.Checked Then" or outside it?

Thanks for your advice.
Chris
Jul 11 '07 #1
1 1091

In a web environment, you should always open (as late as possible) and close
( as soon as possible ) your code.

You should at the very least, write some kind of wrapper class or
static(shared in vb.net) method to encapsulate the database call.

Your code below is ...... clanky at best.
Here is an example of a well layered application:

http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!139.entry

"Chris" <kn*@shdv.sdwrote in message
news:uv**************@TK2MSFTNGP03.phx.gbl...
Hi,

We have an application where users must fill the same form several times,
creating each time a record in the database.
When checking the checkbox in this form means the last record.

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim connection As SqlConnection
etc ....
connectionstr = "Data Source=LAPTOP\SQLEXPRESS;Initial
Catalog=mydb;Integrated Security=True"
etc ....
comd.CommandText = "insert into [dbo].[mytable] (field1, field2, etc
....)
values(......);"
connection.Open()
comd.ExecuteNonQuery()
...
If CheckBox1.Checked Then
Server.Transfer("other.aspx")
end if
connection.Close()
end sub

My question is: do i have to close the connection like this or leaving it
open till the last record is inserted?
With other words: where to place the "connection.Close()"? Inside the "If
CheckBox1.Checked Then" or outside it?

Thanks for your advice.
Chris


Jul 11 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Randall Smith | last post by:
I've been programming in Python for about 2 years. I think it offers the best combination of simplicity and power of any language I have explored. As I write more and larger and complex programs,...
1
by: ===Steve L.=== | last post by:
I'm new to vb studio .net and sql2k, and wondered if any one know any good articles on the subject? such as the best practices and configuration for the database connection and security model....
12
by: alex | last post by:
Hi, I was just wandering which one is correct, better, or better programming practice? //Case #1 public class Main { ClassA a = new ClassA(); a.Value = "some value";
2
by: Martin Horn | last post by:
Hi group. I have implemented a class along these lines, Public Class CustomerDetails Private fRecordString As String Public Sub New(ByVal RecordString As String) fRecordString =...
5
by: bloukopkoggelmander | last post by:
Hi All Just a question about good Access design. I have a form which displays some information from different tables through comboboxes(which the user selects) and textboxes. There are then...
0
by: sri5739 | last post by:
Hi All, Can anyone help in getting me the .net MCAD practise exams link and practise question papers .. this will really help me .. thanks a lot in advance
4
by: =?Utf-8?B?VzFsZDBuZTc0?= | last post by:
When one architects a new project one of the first steps in the decision is to decide on the layers. (In my opinion anyway) One architecture that I have used before is to go solid OO and create...
3
by: Chris | last post by:
Hi again, I need another good practise advice. Everybody (100 persons) in our compagny manages one or more projects. Each projects needs two tables: table 'project' (general information),...
1
by: Chris | last post by:
Hi, We have an application where users must fill the same form several times, creating each time a record in the database. When checking the checkbox in this form means the last record. ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.