Displaying Stored Procedure results in a textbox

G

Guest

I have a stored procedure which returns a decimal field from a table and i
want my asp.net page to read in the result and then diplay it in a textbox or
a label.

What is the best way to do this?
 
K

Karl Seguin

if you are returning a single field, use ExecuteScalar() to get it and
assign the value to your textbox:

sub Page_Load
someTextBox.Text = GetProductAmount(3).ToString()
end sub

public shared function GetProductAmount(productId as integer) as decimal
dim connection as new sqlconnection(connection_String)
dim command as new sqlcommad("SELECT Amount from Price where Id =@id",
connection)
command.parameters.add("@Id", SqlDbType.Int).Value = productId

try
connection.open()
return cdec(command.ExecuteScalar())
finally
connection.dispose()
command.dispoe()
end try
end function


of course you might wanna check for null if that's possible:

dim o as object = command.ExecuteScalar()
if not o is nothing andalso not o is DbNull.Value then
return cdec(o)
end if
return 0.0

Karl



--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top