473,775 Members | 2,523 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Do I Have to Open a Connection 3 Times?

I'm just starting to learn asp.net. What I have is a registration form
for a message board system. Basically, the user enters in their info
and it's validated. So I have a custom validator check to make sure the
username is unique, I have another custom validator that makes sure the
email address is unique and then I save the info to the database. That
means I open the connection to the sqlserver 3 times.

Seems inefficient to me. Is there a better way?

Sub Button1_Click(s ender As Object, e As EventArgs)
if me.isvalid then
Dim connectionStrin g As String = "server='netsdk ';
trusted_connect ion=true; database='db'"
Dim dbConnection As System.Data.IDb Connection = New
System.Data.Sql Client.SqlConne ction(connectio nString)
Dim dbCommand As System.Data.IDb Command = New
System.Data.Sql Client.SqlComma nd
dbCommand.Conne ction = dbConnection
Dim uname as string
Dim password as string
Dim em as string
Dim ip as string
Dim rightnow as date
ip = Request.UserHos tAddress()
rightnow = now
uname = username.text
password = password1.text
em = email.text

dbCommand.Comma ndType = System.Data.Com mandType.Stored Procedure
dbCommand.Comma ndText = "sp_registe r"
Dim dbParam_usernam e As System.Data.IDa taParameter = New
System.Data.Sql Client.SqlParam eter
dbParam_usernam e.ParameterName = "@username"
dbParam_usernam e.Value = uname
dbParam_usernam e.DbType = System.Data.DbT ype.StringFixed Length
dbCommand.Param eters.Add(dbPar am_username)
Dim dbParam_passwor d As System.Data.IDa taParameter = New
System.Data.Sql Client.SqlParam eter
dbParam_passwor d.ParameterName = "@password"
dbParam_passwor d.Value = password
dbParam_passwor d.DbType = System.Data.DbT ype.StringFixed Length
dbCommand.Param eters.Add(dbPar am_password)
Dim dbParam_email As System.Data.IDa taParameter = New
System.Data.Sql Client.SqlParam eter
dbParam_email.P arameterName = "@email"
dbParam_email.V alue = em
dbParam_email.D bType = System.Data.DbT ype.String
dbCommand.Param eters.Add(dbPar am_email)
Dim dbParam_ip As System.Data.IDa taParameter = New
System.Data.Sql Client.SqlParam eter
dbParam_ip.Para meterName = "@ip"
dbParam_ip.Valu e = ip
dbParam_ip.DbTy pe = System.Data.DbT ype.StringFixed Length
dbCommand.Param eters.Add(dbPar am_ip)
Dim dbParam_last_ip As System.Data.IDa taParameter = New
System.Data.Sql Client.SqlParam eter
dbParam_last_ip .ParameterName = "@last_ip"
dbParam_last_ip .Value = ip
dbParam_last_ip .DbType = System.Data.DbT ype.StringFixed Length
dbCommand.Param eters.Add(dbPar am_last_ip)
Dim dbParam_created As System.Data.IDa taParameter = New
System.Data.Sql Client.SqlParam eter
dbParam_created .ParameterName = "@created"
dbParam_created .Value = rightnow
dbParam_created .DbType = System.Data.DbT ype.DateTime
dbCommand.Param eters.Add(dbPar am_created)
Dim dbParam_last As System.Data.IDa taParameter = New
System.Data.Sql Client.SqlParam eter
dbParam_last.Pa rameterName = "@last"
dbParam_last.Va lue = rightnow
dbParam_last.Db Type = System.Data.DbT ype.DateTime
dbCommand.Param eters.Add(dbPar am_last)

Dim rowsAffected As Integer = 0
dbConnection.Op en
Try
rowsAffected = dbCommand.Execu teNonQuery
Finally
dbConnection.Cl ose
End Try
'Server.Transfe r("index.aspx ")
end if
End Sub

Sub CustomValidator _Username( s As Object, e As ServerValidateE ventArgs
)
Dim connectionStrin g As String = "server='netsdk ';
trusted_connect ion=true; database='db'"
Dim dbConnection As System.Data.IDb Connection = New
System.Data.Sql Client.SqlConne ction(connectio nString)
Dim dbCommand As System.Data.IDb Command = New
System.Data.Sql Client.SqlComma nd
dbCommand.Conne ction = dbConnection

dbCommand.Comma ndType = System.Data.Com mandType.Stored Procedure
dbCommand.Comma ndText = "sp_usernam e"

Dim dbParam_usernam e As System.Data.IDa taParameter = New
System.Data.Sql Client.SqlParam eter
dbParam_usernam e.ParameterName = "@username"
dbParam_usernam e.Value = trim(e.value)
dbParam_usernam e.DbType = System.Data.DbT ype.StringFixed Length
dbCommand.Param eters.Add(dbPar am_username)

dbConnection.Op en
Dim cnt as integer
cnt = dbCommand.Execu teScalar
If cnt = 0 Then
e.IsValid = True
Else
e.IsValid = False
End If
End Sub

Sub CustomValidator _Email( s As Object, e As ServerValidateE ventArgs )
Dim connectionStrin g As String = "server='netsdk ';
trusted_connect ion=true; database='db'"
Dim dbConnection As System.Data.IDb Connection = New
System.Data.Sql Client.SqlConne ction(connectio nString)
Dim dbCommand As System.Data.IDb Command = New
System.Data.Sql Client.SqlComma nd
dbCommand.Conne ction = dbConnection

dbCommand.Comma ndType = System.Data.Com mandType.Stored Procedure
dbCommand.Comma ndText = "sp_email"

Dim dbParam_email As System.Data.IDa taParameter = New
System.Data.Sql Client.SqlParam eter
dbParam_email.P arameterName = "@email"
dbParam_email.V alue = trim(email.text )
dbParam_email.D bType = System.Data.DbT ype.String
dbCommand.Param eters.Add(dbPar am_email)

dbConnection.Op en
Dim cnt as integer
cnt = dbCommand.Execu teScalar
If cnt = 0 Then
e.IsValid = True
Else
e.IsValid = False
End If
End Sub
--
..
http://sf-f.org, weblog and search engine for fans and writers of
speculative fiction.
..
Nov 20 '05 #1
3 1387
Why not use client side validation to do the preliminary stuff and if they
have all of the fields filled in, then send all three of them to the db and
validate once? You are right, sending three seperate commands to the db is
terribly inefficient.

--
W.G. Ryan MVP Windows - Embedded

http://forums.devbuzz.com
http://www.knowdotnet.com/dataaccess.html
http://www.msmvps.com/williamryan/
"jimb" <sp**@spamity.s pam> wrote in message
news:Xn******** *************** *****@68.1.17.6 ...
I'm just starting to learn asp.net. What I have is a registration form
for a message board system. Basically, the user enters in their info
and it's validated. So I have a custom validator check to make sure the
username is unique, I have another custom validator that makes sure the
email address is unique and then I save the info to the database. That
means I open the connection to the sqlserver 3 times.

Seems inefficient to me. Is there a better way?

Sub Button1_Click(s ender As Object, e As EventArgs)
if me.isvalid then
Dim connectionStrin g As String = "server='netsdk ';
trusted_connect ion=true; database='db'"
Dim dbConnection As System.Data.IDb Connection = New
System.Data.Sql Client.SqlConne ction(connectio nString)
Dim dbCommand As System.Data.IDb Command = New
System.Data.Sql Client.SqlComma nd
dbCommand.Conne ction = dbConnection
Dim uname as string
Dim password as string
Dim em as string
Dim ip as string
Dim rightnow as date
ip = Request.UserHos tAddress()
rightnow = now
uname = username.text
password = password1.text
em = email.text

dbCommand.Comma ndType = System.Data.Com mandType.Stored Procedure
dbCommand.Comma ndText = "sp_registe r"
Dim dbParam_usernam e As System.Data.IDa taParameter = New
System.Data.Sql Client.SqlParam eter
dbParam_usernam e.ParameterName = "@username"
dbParam_usernam e.Value = uname
dbParam_usernam e.DbType = System.Data.DbT ype.StringFixed Length
dbCommand.Param eters.Add(dbPar am_username)
Dim dbParam_passwor d As System.Data.IDa taParameter = New
System.Data.Sql Client.SqlParam eter
dbParam_passwor d.ParameterName = "@password"
dbParam_passwor d.Value = password
dbParam_passwor d.DbType = System.Data.DbT ype.StringFixed Length
dbCommand.Param eters.Add(dbPar am_password)
Dim dbParam_email As System.Data.IDa taParameter = New
System.Data.Sql Client.SqlParam eter
dbParam_email.P arameterName = "@email"
dbParam_email.V alue = em
dbParam_email.D bType = System.Data.DbT ype.String
dbCommand.Param eters.Add(dbPar am_email)
Dim dbParam_ip As System.Data.IDa taParameter = New
System.Data.Sql Client.SqlParam eter
dbParam_ip.Para meterName = "@ip"
dbParam_ip.Valu e = ip
dbParam_ip.DbTy pe = System.Data.DbT ype.StringFixed Length
dbCommand.Param eters.Add(dbPar am_ip)
Dim dbParam_last_ip As System.Data.IDa taParameter = New
System.Data.Sql Client.SqlParam eter
dbParam_last_ip .ParameterName = "@last_ip"
dbParam_last_ip .Value = ip
dbParam_last_ip .DbType = System.Data.DbT ype.StringFixed Length
dbCommand.Param eters.Add(dbPar am_last_ip)
Dim dbParam_created As System.Data.IDa taParameter = New
System.Data.Sql Client.SqlParam eter
dbParam_created .ParameterName = "@created"
dbParam_created .Value = rightnow
dbParam_created .DbType = System.Data.DbT ype.DateTime
dbCommand.Param eters.Add(dbPar am_created)
Dim dbParam_last As System.Data.IDa taParameter = New
System.Data.Sql Client.SqlParam eter
dbParam_last.Pa rameterName = "@last"
dbParam_last.Va lue = rightnow
dbParam_last.Db Type = System.Data.DbT ype.DateTime
dbCommand.Param eters.Add(dbPar am_last)

Dim rowsAffected As Integer = 0
dbConnection.Op en
Try
rowsAffected = dbCommand.Execu teNonQuery
Finally
dbConnection.Cl ose
End Try
'Server.Transfe r("index.aspx ")
end if
End Sub

Sub CustomValidator _Username( s As Object, e As ServerValidateE ventArgs
)
Dim connectionStrin g As String = "server='netsdk ';
trusted_connect ion=true; database='db'"
Dim dbConnection As System.Data.IDb Connection = New
System.Data.Sql Client.SqlConne ction(connectio nString)
Dim dbCommand As System.Data.IDb Command = New
System.Data.Sql Client.SqlComma nd
dbCommand.Conne ction = dbConnection

dbCommand.Comma ndType = System.Data.Com mandType.Stored Procedure
dbCommand.Comma ndText = "sp_usernam e"

Dim dbParam_usernam e As System.Data.IDa taParameter = New
System.Data.Sql Client.SqlParam eter
dbParam_usernam e.ParameterName = "@username"
dbParam_usernam e.Value = trim(e.value)
dbParam_usernam e.DbType = System.Data.DbT ype.StringFixed Length
dbCommand.Param eters.Add(dbPar am_username)

dbConnection.Op en
Dim cnt as integer
cnt = dbCommand.Execu teScalar
If cnt = 0 Then
e.IsValid = True
Else
e.IsValid = False
End If
End Sub

Sub CustomValidator _Email( s As Object, e As ServerValidateE ventArgs )
Dim connectionStrin g As String = "server='netsdk ';
trusted_connect ion=true; database='db'"
Dim dbConnection As System.Data.IDb Connection = New
System.Data.Sql Client.SqlConne ction(connectio nString)
Dim dbCommand As System.Data.IDb Command = New
System.Data.Sql Client.SqlComma nd
dbCommand.Conne ction = dbConnection

dbCommand.Comma ndType = System.Data.Com mandType.Stored Procedure
dbCommand.Comma ndText = "sp_email"

Dim dbParam_email As System.Data.IDa taParameter = New
System.Data.Sql Client.SqlParam eter
dbParam_email.P arameterName = "@email"
dbParam_email.V alue = trim(email.text )
dbParam_email.D bType = System.Data.DbT ype.String
dbCommand.Param eters.Add(dbPar am_email)

dbConnection.Op en
Dim cnt as integer
cnt = dbCommand.Execu teScalar
If cnt = 0 Then
e.IsValid = True
Else
e.IsValid = False
End If
End Sub
--
.
http://sf-f.org, weblog and search engine for fans and writers of
speculative fiction.
.

Nov 20 '05 #2
Hi Jimb,

In addition to Bill, in the preliminary part you can do not only if it is
filled in, however as well:
checking if the passwords is correct to the requirements.
checking if the email address is correct

For that you can find a lot of Javascript on the Net.

Cor
Nov 20 '05 #3
Hi Jimb,

In addition to Bill, in the preliminary part you can do not only if it is
filled in, however as well:
checking if the passwords is correct to the requirements.
checking if the email address is correct

For that you can find a lot of Javascript on the Net.

Cor
Nov 20 '05 #4

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

Similar topics

1
2567
by: C Sharp beginner | last post by:
I'm sorry about this verbose posting. This is a follow-up to my yesterday's posting. Thanks William for your reply. I understand it is a good practice to open connections as late as possible and close them as early as possible. My requirement is as follows: I'm developing a class library that will be instantiated by a COM component. My class library contains functions that perform lot of mathematical calculations and read a lot of data...
6
2911
by: Keith Smith | last post by:
I read in a Visual Studio .NET book that with C# it is now recommended to open database connections whenever you need to query a database as opposed to the traditional method of opening a database at the start of your app and closing it at the end. Their reasoning was that "opening a database connection is no longer an expensive operation". Do you guys agree?
4
2472
by: Macca | last post by:
Hi, I have an windows forms application that accesses a SQL database I have a few questions as to connecting to the database. This application will run 24 hours a day. It is a monitoring application and will store events that happen in the database (These events happen randomly without pattern, between 10-50 a day) . There are a number of situations where the database is accessed.
9
288
by: Paul | last post by:
Hi just wondering what could cause a left open SQL connection as this has only occured 2 times in the last week. The error it creates is when a web app tries to open a connection an error message states, it is already open. The process was then killed to close the connection. The usage is pretty low around 15 workstations. Looking at the code it appears that all connections get closed, so thinking it is not a problem with the code. Thanks...
4
23187
by: Guoqi Zheng | last post by:
Dear sir, I keep getting the following errors on one of my sites after clicking for many times. Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached. Below is my code. Any help will be appreciated.
2
2398
by: Nils Magnus Englund | last post by:
Hi, I've made a HttpModule which deals with user authentication. On the first request in a users session, it fetches data from a SQL Server using the following code: using (SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings)) {
6
4997
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing for long running reports. When the processing is complete it uses crystal reports to load a template file, populate it, and then export it to a PDF. It works fine so far....
7
13586
by: fniles | last post by:
I am using VB.Net 2003 and MS Access (connecting using OleDBConnection). I read using DataAdapter and DataSet, not DataReader. When many people try to access the database at the same time, I get the error "ExecuteReader requires an open and available Connection. The connection's current state is Open, Executing." I do not use ExecuteReader, why the error says ExecuteReader. What does it mean ? When I get this error, is there a way for me...
4
2154
by: UlrikSL | last post by:
My question is performance wize, is it better to open and close each time I make a request to the database, in small periods I make like 5 queryes a second, other times only every 10 seconds. I can understand that close and open do not close the underlaying connection, so would the correct way be to open and close with each request, and give a better chance of avoiding faulty connection? Or would it cause worse performance? Thanks.. Ulrik
0
9621
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8939
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7463
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5358
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4014
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 we have to send another system
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2852
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.