473,487 Members | 2,671 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 1008

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
10504
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
2175
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
1332
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
1125
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 =...
10
2737
by: Barry Morris | last post by:
Hi I am a php newbie although I have been a programmer for years, this can be dangerous because all the languages I know use = as equal comparison so it took me a long time to debug if ($STRING...
5
1307
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
1004
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
2034
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
1336
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
1093
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. ...
0
6967
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
7349
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...
1
4874
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
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1381
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 ...
1
600
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
267
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.