good practise question

C

Chris

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
 
T

Tony M

The link in article is broken


sloan said:
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 said:
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
 
S

sloan

Doohhh.

It's fixed.



Tony M said:
The link in article is broken


sloan said:
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 said:
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
 
C

Chris

Thanks to all

sloan said:
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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top