473,385 Members | 1,392 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,385 software developers and data experts.

Executescalar, the right approach

On a web-form the user enters basic data (company name, phone,
areacode etc).

Many we have already registrered and some not. For those we have we
wish to make the enter of data easier by looking them up.

This looks up 1 value:

Sub LookUpVVS()

Dim sqlCon As New SqlConnection
sqlCon.ConnectionString = conString

Try
sSQL = "SELECT Firmanavn FROM tblVVS WHERE
Telefon=@TelefonNr"
sqlCon.Open()
Dim cmd As New SqlCommand(sSQL, sqlCon)

Dim p1 As New SqlParameter("@TelefonNr",
Data.SqlDbType.Int)
p1.Value = Me.txtTelefon.Text
cmd.Parameters.Add(p1)

Me.txtFirma.Text = cmd.ExecuteScalar().ToString

sqlCon.Close()
sqlCon.Dispose()

Catch ex As Exception
Response.Write(ex.Message)
Exit Sub
End Try

End Sub
In total I have 10 fields I wish to look up and put into the webform.
Is Executescalar the wrong approach? I ask cause I don't know if
there's a simpler way to use Excecutescalar.

I'm thinking that I should use a dataset instaed that holds the entire
record.

Regards /Snedker
Oct 23 '06 #1
4 3650
Yes, it is a wrong approach. If you need 10 values, you don't need to call
ExecuteScalar 10 times. You should get them all in one single select. You
don't have to use a dataset. A datareader is another option.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
"Morten Snedker" <morten_spammenot_ATdbconsult.dkwrote in message
news:64********************************@4ax.com...
On a web-form the user enters basic data (company name, phone,
areacode etc).

Many we have already registrered and some not. For those we have we
wish to make the enter of data easier by looking them up.

This looks up 1 value:

Sub LookUpVVS()

Dim sqlCon As New SqlConnection
sqlCon.ConnectionString = conString

Try
sSQL = "SELECT Firmanavn FROM tblVVS WHERE
Telefon=@TelefonNr"
sqlCon.Open()
Dim cmd As New SqlCommand(sSQL, sqlCon)

Dim p1 As New SqlParameter("@TelefonNr",
Data.SqlDbType.Int)
p1.Value = Me.txtTelefon.Text
cmd.Parameters.Add(p1)

Me.txtFirma.Text = cmd.ExecuteScalar().ToString

sqlCon.Close()
sqlCon.Dispose()

Catch ex As Exception
Response.Write(ex.Message)
Exit Sub
End Try

End Sub
In total I have 10 fields I wish to look up and put into the webform.
Is Executescalar the wrong approach? I ask cause I don't know if
there's a simpler way to use Excecutescalar.

I'm thinking that I should use a dataset instaed that holds the entire
record.

Regards /Snedker

Oct 23 '06 #2
In the situation you are describing, executeScalar won't scale very well.
Today you have 10 fields, tomorrow you might have 100 - that'll be a
problem!

If it's only a single row of data, but multiple columns, ExecuteReader and
DataReaders are likely the most efficient and scalable solution...DataSets
would work too, but are heavier (they can be cached though, which may or
may not be applicable in your case...)

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Morten Snedker" <morten_spammenot_ATdbconsult.dkwrote in message
news:64********************************@4ax.com...
On a web-form the user enters basic data (company name, phone,
areacode etc).

Many we have already registrered and some not. For those we have we
wish to make the enter of data easier by looking them up.

This looks up 1 value:

Sub LookUpVVS()

Dim sqlCon As New SqlConnection
sqlCon.ConnectionString = conString

Try
sSQL = "SELECT Firmanavn FROM tblVVS WHERE
Telefon=@TelefonNr"
sqlCon.Open()
Dim cmd As New SqlCommand(sSQL, sqlCon)

Dim p1 As New SqlParameter("@TelefonNr",
Data.SqlDbType.Int)
p1.Value = Me.txtTelefon.Text
cmd.Parameters.Add(p1)

Me.txtFirma.Text = cmd.ExecuteScalar().ToString

sqlCon.Close()
sqlCon.Dispose()

Catch ex As Exception
Response.Write(ex.Message)
Exit Sub
End Try

End Sub
In total I have 10 fields I wish to look up and put into the webform.
Is Executescalar the wrong approach? I ask cause I don't know if
there's a simpler way to use Excecutescalar.

I'm thinking that I should use a dataset instaed that holds the entire
record.

Regards /Snedker

Oct 23 '06 #3
executescaler returns the first column value of the first row returned.
itsproably not what you want.

-- bruce (sqlwork.com)

"Morten Snedker" <morten_spammenot_ATdbconsult.dkwrote in message
news:64********************************@4ax.com...
On a web-form the user enters basic data (company name, phone,
areacode etc).

Many we have already registrered and some not. For those we have we
wish to make the enter of data easier by looking them up.

This looks up 1 value:

Sub LookUpVVS()

Dim sqlCon As New SqlConnection
sqlCon.ConnectionString = conString

Try
sSQL = "SELECT Firmanavn FROM tblVVS WHERE
Telefon=@TelefonNr"
sqlCon.Open()
Dim cmd As New SqlCommand(sSQL, sqlCon)

Dim p1 As New SqlParameter("@TelefonNr",
Data.SqlDbType.Int)
p1.Value = Me.txtTelefon.Text
cmd.Parameters.Add(p1)

Me.txtFirma.Text = cmd.ExecuteScalar().ToString

sqlCon.Close()
sqlCon.Dispose()

Catch ex As Exception
Response.Write(ex.Message)
Exit Sub
End Try

End Sub
In total I have 10 fields I wish to look up and put into the webform.
Is Executescalar the wrong approach? I ask cause I don't know if
there's a simpler way to use Excecutescalar.

I'm thinking that I should use a dataset instaed that holds the entire
record.

Regards /Snedker

Oct 23 '06 #4
If you have the option, the best way to handle a single row as opposed
to a single datum is to create a stored procedure with the fields
returned as OUTPUT parameters.

You can then use ExecuteNonQuery and read the values out of the
Parameters collection of your command.

Generally, I use this heuristic

1 value : ExecuteScalar
1 row : ExecuteNonQuery with output params
N rows : ExecuteReader

Morten Snedker wrote:
On a web-form the user enters basic data (company name, phone,
areacode etc).

Many we have already registrered and some not. For those we have we
wish to make the enter of data easier by looking them up.

This looks up 1 value:

Sub LookUpVVS()

Dim sqlCon As New SqlConnection
sqlCon.ConnectionString = conString

Try
sSQL = "SELECT Firmanavn FROM tblVVS WHERE
Telefon=@TelefonNr"
sqlCon.Open()
Dim cmd As New SqlCommand(sSQL, sqlCon)

Dim p1 As New SqlParameter("@TelefonNr",
Data.SqlDbType.Int)
p1.Value = Me.txtTelefon.Text
cmd.Parameters.Add(p1)

Me.txtFirma.Text = cmd.ExecuteScalar().ToString

sqlCon.Close()
sqlCon.Dispose()

Catch ex As Exception
Response.Write(ex.Message)
Exit Sub
End Try

End Sub
In total I have 10 fields I wish to look up and put into the webform.
Is Executescalar the wrong approach? I ask cause I don't know if
there's a simpler way to use Excecutescalar.

I'm thinking that I should use a dataset instaed that holds the entire
record.

Regards /Snedker
Oct 23 '06 #5

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

Similar topics

3
by: cmo63126 | last post by:
Why am I not able to retrieve the value of ID (which does exist) using ExecuteScalar()? Do i simply have to use ExecuteReader() instead? My problem snippet: string mySQL = "SELECT ID FROM...
2
by: WeiminZhang | last post by:
When I use the OleDb to connect a Oracle db, and use the ExecutScalar() method to get the count of a table, the return value can't be cast to a data type, say int, while this works fine for a SQL...
2
by: Dinesh | last post by:
Hi, I have one stored procedure in SQL server in which i have written one insert statement. Now in my cs file i pass the parameters require to execute that stored procedure and finaly by mistaken...
6
by: Max | last post by:
Anyone know why I'm always getting 0 returned? My stored procedure returns -1. Dim iErrorCode As Int32 iErrorCode = Convert.ToInt32(SqlHelper.ExecuteScalar(AppVars.strConn, _ "gpUpdateMember",...
7
by: Neven Klofutar | last post by:
Hi, I have a problem with SqlHelper.ExecuteScalar ... When I try to execute SqlHelper.ExecuteScalar I get this message: "System.InvalidCastException: Object must implement IConvertible.". ...
5
by: bienwell | last post by:
Hi all, I have a problem with using myCommand.ExecuteScalar(). My question is : If the Web setup is incorrect, does it make command ExecuteScalar() work improperly ?? In my program, I was...
3
by: charliewest | last post by:
Hi - I need to detect when the ExecuteScalar() method of the cmd object returns NULL. I have tried the below code, however, it always returns false (this is to say, that ExecuteScalar never...
2
by: Julio Allegue | last post by:
I am getting the wrong Count(*) on vb.net using the ExecuteScalar . It returns all the rows. It doesn't seem to look at the WHERE clause. At the same time, I am getting the correct count on "SQL...
2
by: Manikandan | last post by:
Hi, I have a table with following data Tablename:details No(varchar) Name(varchar) Updated(Datetime) 1 mm 10/10/2006 2 nn 02/12/2005 3 kk NULL I'm using executescalar to get the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.