473,387 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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(sender As Object, e As EventArgs)
if me.isvalid then
Dim connectionString As String = "server='netsdk';
trusted_connection=true; database='db'"
Dim dbConnection As System.Data.IDbConnection = New
System.Data.SqlClient.SqlConnection(connectionStri ng)
Dim dbCommand As System.Data.IDbCommand = New
System.Data.SqlClient.SqlCommand
dbCommand.Connection = dbConnection
Dim uname as string
Dim password as string
Dim em as string
Dim ip as string
Dim rightnow as date
ip = Request.UserHostAddress()
rightnow = now
uname = username.text
password = password1.text
em = email.text

dbCommand.CommandType = System.Data.CommandType.StoredProcedure
dbCommand.CommandText = "sp_register"
Dim dbParam_username As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_username.ParameterName = "@username"
dbParam_username.Value = uname
dbParam_username.DbType = System.Data.DbType.StringFixedLength
dbCommand.Parameters.Add(dbParam_username)
Dim dbParam_password As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_password.ParameterName = "@password"
dbParam_password.Value = password
dbParam_password.DbType = System.Data.DbType.StringFixedLength
dbCommand.Parameters.Add(dbParam_password)
Dim dbParam_email As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_email.ParameterName = "@email"
dbParam_email.Value = em
dbParam_email.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_email)
Dim dbParam_ip As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_ip.ParameterName = "@ip"
dbParam_ip.Value = ip
dbParam_ip.DbType = System.Data.DbType.StringFixedLength
dbCommand.Parameters.Add(dbParam_ip)
Dim dbParam_last_ip As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_last_ip.ParameterName = "@last_ip"
dbParam_last_ip.Value = ip
dbParam_last_ip.DbType = System.Data.DbType.StringFixedLength
dbCommand.Parameters.Add(dbParam_last_ip)
Dim dbParam_created As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_created.ParameterName = "@created"
dbParam_created.Value = rightnow
dbParam_created.DbType = System.Data.DbType.DateTime
dbCommand.Parameters.Add(dbParam_created)
Dim dbParam_last As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_last.ParameterName = "@last"
dbParam_last.Value = rightnow
dbParam_last.DbType = System.Data.DbType.DateTime
dbCommand.Parameters.Add(dbParam_last)

Dim rowsAffected As Integer = 0
dbConnection.Open
Try
rowsAffected = dbCommand.ExecuteNonQuery
Finally
dbConnection.Close
End Try
'Server.Transfer("index.aspx")
end if
End Sub

Sub CustomValidator_Username( s As Object, e As ServerValidateEventArgs
)
Dim connectionString As String = "server='netsdk';
trusted_connection=true; database='db'"
Dim dbConnection As System.Data.IDbConnection = New
System.Data.SqlClient.SqlConnection(connectionStri ng)
Dim dbCommand As System.Data.IDbCommand = New
System.Data.SqlClient.SqlCommand
dbCommand.Connection = dbConnection

dbCommand.CommandType = System.Data.CommandType.StoredProcedure
dbCommand.CommandText = "sp_username"

Dim dbParam_username As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_username.ParameterName = "@username"
dbParam_username.Value = trim(e.value)
dbParam_username.DbType = System.Data.DbType.StringFixedLength
dbCommand.Parameters.Add(dbParam_username)

dbConnection.Open
Dim cnt as integer
cnt = dbCommand.ExecuteScalar
If cnt = 0 Then
e.IsValid = True
Else
e.IsValid = False
End If
End Sub

Sub CustomValidator_Email( s As Object, e As ServerValidateEventArgs )
Dim connectionString As String = "server='netsdk';
trusted_connection=true; database='db'"
Dim dbConnection As System.Data.IDbConnection = New
System.Data.SqlClient.SqlConnection(connectionStri ng)
Dim dbCommand As System.Data.IDbCommand = New
System.Data.SqlClient.SqlCommand
dbCommand.Connection = dbConnection

dbCommand.CommandType = System.Data.CommandType.StoredProcedure
dbCommand.CommandText = "sp_email"

Dim dbParam_email As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_email.ParameterName = "@email"
dbParam_email.Value = trim(email.text)
dbParam_email.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_email)

dbConnection.Open
Dim cnt as integer
cnt = dbCommand.ExecuteScalar
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 1373
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.spam> 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(sender As Object, e As EventArgs)
if me.isvalid then
Dim connectionString As String = "server='netsdk';
trusted_connection=true; database='db'"
Dim dbConnection As System.Data.IDbConnection = New
System.Data.SqlClient.SqlConnection(connectionStri ng)
Dim dbCommand As System.Data.IDbCommand = New
System.Data.SqlClient.SqlCommand
dbCommand.Connection = dbConnection
Dim uname as string
Dim password as string
Dim em as string
Dim ip as string
Dim rightnow as date
ip = Request.UserHostAddress()
rightnow = now
uname = username.text
password = password1.text
em = email.text

dbCommand.CommandType = System.Data.CommandType.StoredProcedure
dbCommand.CommandText = "sp_register"
Dim dbParam_username As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_username.ParameterName = "@username"
dbParam_username.Value = uname
dbParam_username.DbType = System.Data.DbType.StringFixedLength
dbCommand.Parameters.Add(dbParam_username)
Dim dbParam_password As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_password.ParameterName = "@password"
dbParam_password.Value = password
dbParam_password.DbType = System.Data.DbType.StringFixedLength
dbCommand.Parameters.Add(dbParam_password)
Dim dbParam_email As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_email.ParameterName = "@email"
dbParam_email.Value = em
dbParam_email.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_email)
Dim dbParam_ip As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_ip.ParameterName = "@ip"
dbParam_ip.Value = ip
dbParam_ip.DbType = System.Data.DbType.StringFixedLength
dbCommand.Parameters.Add(dbParam_ip)
Dim dbParam_last_ip As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_last_ip.ParameterName = "@last_ip"
dbParam_last_ip.Value = ip
dbParam_last_ip.DbType = System.Data.DbType.StringFixedLength
dbCommand.Parameters.Add(dbParam_last_ip)
Dim dbParam_created As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_created.ParameterName = "@created"
dbParam_created.Value = rightnow
dbParam_created.DbType = System.Data.DbType.DateTime
dbCommand.Parameters.Add(dbParam_created)
Dim dbParam_last As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_last.ParameterName = "@last"
dbParam_last.Value = rightnow
dbParam_last.DbType = System.Data.DbType.DateTime
dbCommand.Parameters.Add(dbParam_last)

Dim rowsAffected As Integer = 0
dbConnection.Open
Try
rowsAffected = dbCommand.ExecuteNonQuery
Finally
dbConnection.Close
End Try
'Server.Transfer("index.aspx")
end if
End Sub

Sub CustomValidator_Username( s As Object, e As ServerValidateEventArgs
)
Dim connectionString As String = "server='netsdk';
trusted_connection=true; database='db'"
Dim dbConnection As System.Data.IDbConnection = New
System.Data.SqlClient.SqlConnection(connectionStri ng)
Dim dbCommand As System.Data.IDbCommand = New
System.Data.SqlClient.SqlCommand
dbCommand.Connection = dbConnection

dbCommand.CommandType = System.Data.CommandType.StoredProcedure
dbCommand.CommandText = "sp_username"

Dim dbParam_username As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_username.ParameterName = "@username"
dbParam_username.Value = trim(e.value)
dbParam_username.DbType = System.Data.DbType.StringFixedLength
dbCommand.Parameters.Add(dbParam_username)

dbConnection.Open
Dim cnt as integer
cnt = dbCommand.ExecuteScalar
If cnt = 0 Then
e.IsValid = True
Else
e.IsValid = False
End If
End Sub

Sub CustomValidator_Email( s As Object, e As ServerValidateEventArgs )
Dim connectionString As String = "server='netsdk';
trusted_connection=true; database='db'"
Dim dbConnection As System.Data.IDbConnection = New
System.Data.SqlClient.SqlConnection(connectionStri ng)
Dim dbCommand As System.Data.IDbCommand = New
System.Data.SqlClient.SqlCommand
dbCommand.Connection = dbConnection

dbCommand.CommandType = System.Data.CommandType.StoredProcedure
dbCommand.CommandText = "sp_email"

Dim dbParam_email As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_email.ParameterName = "@email"
dbParam_email.Value = trim(email.text)
dbParam_email.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_email)

dbConnection.Open
Dim cnt as integer
cnt = dbCommand.ExecuteScalar
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
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...
6
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...
4
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...
9
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...
4
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...
2
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...
6
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...
7
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...
4
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.