Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / September 2007

Tip: Looking for answers? Try searching our database.

Using MS SQL Function in VB.Net 2003

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alex - 11 Sep 2007 23:32 GMT
Hi...

I'm rather new to VB.Net, so please forgive the elementary question.  I'm
trying to use an MS SQL 2005 Function from within my VB.Net 2003 code, and
as simple as might be, I'm unable to find any snippet of code that shows me
how to do this.

The function is this 'RetrieveGroupID(@UserID)' and if for example someone
uses 'RetrieveGroupID(44)' it will return the GroupID for User Number 44.

This application is using web forms, and the UserID is stored in the
session, so I have this already.  My problem is I'm not sure how to actually
run the Function then store the output as a variable.

Can someone point me to examples or a how-to showing this?

Thanks --

Alex
Nick Chan - 12 Sep 2007 04:03 GMT
Hi alex, i do not know the 'expert' way
i call the function using "SELECT dbo.RetrieveGroupID(44)"
im sure u know the way to retrieve Selects

> Hi...
>
[quoted text clipped - 15 lines]
>
> Alex
Alex - 12 Sep 2007 14:22 GMT
Hi Nick,

Thanks for the reply, but my question was exactly what you assume I already
know...  I'm new to Visual Basic coding, and though I have a number of books
and resources at hand, often times a simple problem like this eludes me.

What I'm looking for is a snippet or example of code showing how to execute
a SQL command in a class, then store the output in a variable.  In this
instance I am using a function which returns a single integer, but you're
correct in staying the same snippet would work with any SQL statement which
only returned one value.  This is the missing piece I need... I don't know
how to do this, and any assistance from you or anyone else would be much
appriciated.

Thanks again, and take care --

Alex

> Hi alex, i do not know the 'expert' way
> i call the function using "SELECT dbo.RetrieveGroupID(44)"
[quoted text clipped - 23 lines]
>>
>> Alex
Mark Rae [MVP] - 12 Sep 2007 14:34 GMT
> Thanks for the reply, but my question was exactly what you assume I
> already know...  I'm new to Visual Basic coding, and though I have a
[quoted text clipped - 8 lines]
> need... I don't know how to do this, and any assistance from you or anyone
> else would be much appriciated.

How is your ASP.NET app currently configured to interface with the
database...?

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

Alex - 12 Sep 2007 15:30 GMT
>> Thanks for the reply, but my question was exactly what you assume I
>> already know...  I'm new to Visual Basic coding, and though I have a
[quoted text clipped - 11 lines]
> How is your ASP.NET app currently configured to interface with the
> database...?

Hi Mark,

The application is a monolithic program written in VB.Net 2003 by a coder
who's no longer here, and I'm coming in on his coat tails to maintain the
code, learning VB along the way.  To the best I can determine the
application creates a session with all the general information, like the
UserID, UserName, Groups, and so forth.  It then uses datasets, setup both
in the code and via the IDE, to retrieve information from the MS SQL 2005
database.

What I need now is to edit a class so it pulls in a value from an MS SQL
2005 function and stores this value in a variable to use with in the class.
Here's what I've pieced together thus far:

Private Function GetManagerID() As Integer
   Dim cn As New
SqlClient.SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("MyConnectionString"))
   Dim cm As New SqlClient.SqlCommand("select dbo.ManagerFunction(" +
Me.UserID + ")", cn)
  cm.ExecuteNonQuery()
End Function

But how do I get the value from dbo.ManagerFunction(#) into a variable to
use within my application after this function is called?  Also note,
MyConnectionString is already defined in the application and stores our
connection information.

Thanks ..

Alex
Mark Rae [MVP] - 12 Sep 2007 16:33 GMT
> The application is a monolithic program written in VB.Net 2003 by a coder
> who's no longer here, and I'm coming in on his coat tails to maintain the
[quoted text clipped - 3 lines]
> in the code and via the IDE, to retrieve information from the MS SQL 2005
> database.

I've been there, and you have my sympathy...

Change your Function as follows:

' cm.ExecuteNonQuery()
cm.Connection.Open()
Dim ReturnValue As Integer = cm.ExecuteScalar()

I should point out that the above may not be 100% correct as I never go
anywhere near VB.NET, but I think that's more or less it...

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.