﻿<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>StrataFrame Forum » .NET Forums » General .NET Discussion  » Executing Scalar-Valued Function from VB.NET</title><generator>InstantForum 2017-1 Final</generator><description>StrataFrame Forum</description><link>http://forum.strataframe.net/</link><webMaster>StrataFrame Forum</webMaster><lastBuildDate>Mon, 08 Jun 2026 21:29:20 GMT</lastBuildDate><ttl>20</ttl><item><title>Executing Scalar-Valued Function from VB.NET</title><link>http://forum.strataframe.net/FindPost10459.aspx</link><description>How do I execute a sql server scalar-valued function from vb.net? Is it just like a stored procedure?</description><pubDate>Fri, 20 Jul 2007 19:57:30 GMT</pubDate><dc:creator>StarkMike</dc:creator></item><item><title>RE: Executing Scalar-Valued Function from VB.NET</title><link>http://forum.strataframe.net/FindPost10499.aspx</link><description>[quote][b]Ben Chase (07/20/2007)[/b][hr]Greg's solution also works with more than just scalar values... you can use it with full record sets as well.[/quote]&lt;br&gt;
&lt;br&gt;
I'm not following you Ben.  How would you use ExecuteScalar with a "full record sets"?  Doesn't it just return a single value?</description><pubDate>Fri, 20 Jul 2007 19:57:30 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: Executing Scalar-Valued Function from VB.NET</title><link>http://forum.strataframe.net/FindPost10498.aspx</link><description>[quote][b]StarkMike (07/20/2007)[/b][hr]Thats awesome! Thanks Greg... that worked. ;-)[/quote]&lt;br&gt;
&lt;br&gt;
:D Glad you got it working!</description><pubDate>Fri, 20 Jul 2007 19:55:54 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: Executing Scalar-Valued Function from VB.NET</title><link>http://forum.strataframe.net/FindPost10490.aspx</link><description>Greg's solution also works with more than just scalar values... you can use it with full record sets as well.</description><pubDate>Fri, 20 Jul 2007 08:48:13 GMT</pubDate><dc:creator>StrataFrame Team</dc:creator></item><item><title>RE: Executing Scalar-Valued Function from VB.NET</title><link>http://forum.strataframe.net/FindPost10489.aspx</link><description>Yep, Greg's solution works (just remember to either set the Connection property on the command or call ExecuteScalar(cmd) through a business object) ;)&amp;nbsp; However, if you ever need to return a value from a stored procedure (or multiple values), you can also use output parameters.&amp;nbsp; After you call ExecuteScalar() or ExecuteNonQuery(), the parameters labeled as Direction = Output will have the values assigned by the server.&amp;nbsp; Just FYI.</description><pubDate>Fri, 20 Jul 2007 08:47:34 GMT</pubDate><dc:creator>StrataFrame Team</dc:creator></item><item><title>RE: Executing Scalar-Valued Function from VB.NET</title><link>http://forum.strataframe.net/FindPost10486.aspx</link><description>Thats awesome! Thanks Greg... that worked. ;-)</description><pubDate>Fri, 20 Jul 2007 07:22:45 GMT</pubDate><dc:creator>StarkMike</dc:creator></item><item><title>RE: Executing Scalar-Valued Function from VB.NET</title><link>http://forum.strataframe.net/FindPost10466.aspx</link><description>Ah...well I hope my previous example helps someone else then.&lt;br&gt;
&lt;br&gt;
That is a very good question. I've never tried that before, calling a function from client code. My guess would be that you'd just use the above code, but for the CommandText use something like:&lt;br&gt;
&lt;br&gt;
[codesnippet]cmd.CommandText = "Select fn_GetCustomersByCounty(@county)"[/codesnippet]&lt;br&gt;
&lt;br&gt;
where fn_GetCustomersByCounty is your scalar SQL function.  That should work.</description><pubDate>Thu, 19 Jul 2007 14:49:59 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: Executing Scalar-Valued Function from VB.NET</title><link>http://forum.strataframe.net/FindPost10463.aspx</link><description>Thanks Greg. What I mean is if I create a scalar valued function in SQL Server... how would I execute that function and have its result returned to me in VB? :cool:</description><pubDate>Thu, 19 Jul 2007 14:18:52 GMT</pubDate><dc:creator>StarkMike</dc:creator></item><item><title>RE: Executing Scalar-Valued Function from VB.NET</title><link>http://forum.strataframe.net/FindPost10460.aspx</link><description>[quote]How do I execute a sql server scalar-valued function from vb.net? Is it just like a stored procedure?[/quote]&lt;br&gt;
&lt;br&gt;
If I understand you correctly, you want to do something like pass in a PK for a table and get the value of one column or get the count of rows that match some criterion.  I.e. get a single value back from SQL Server.  You'd do this in the appropriate BO. Here is an example&lt;br&gt;
&lt;br&gt;
[codesnippet]Public Sub GetCustomerCountByCounty(county As String) As Integer&lt;br&gt;
  Dim count As Integer&lt;br&gt;
  Using cmd As New SqlCommand()&lt;br&gt;
    cmd.CommandText = "Select Count(*) From Customers Where County = @county"&lt;br&gt;
    cmd.Parameters.Add("@county",varchar,50)   &lt;br&gt;
    cmd.Parameters("@county").Value = county&lt;br&gt;
    Dim retObj As Object = cmd.ExecuteScalar(cmd)&lt;br&gt;
    If retObj IsNot Nothing AndAlso retObj IsNot DBNull.Value Then&lt;br&gt;
      count = CType(retObj,Integer)&lt;br&gt;
    End If&lt;br&gt;
  End Using&lt;br&gt;
  Return count&lt;br&gt;
End Sub[/codesnippet]&lt;br&gt;
&lt;br&gt;
Take note that I return the value from SQL to an Object, not an integer.  In this case, it would likely be safe-ish to return to an integer, as count() return an integer.   But if this had been a column, then the return could be Nothing (null is C#) if no record was found, or it could be DBNull.Value if the record was found, but the column was NULL.&lt;br&gt;
&lt;br&gt;
I just typed this in, so there may be some errors. I use intellisense a lot :D  I know the enum for the Parameters.Add is wrong, but you should get the idea.</description><pubDate>Thu, 19 Jul 2007 12:50:22 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item></channel></rss>