Solved

ASP .net JSON webservice - Problems with stored procedure.

Posted on 2014-02-24
6
153 Views
Last Modified: 2014-07-29
Hello, I am having troubles with modifying this code to use a Stored Procedure insted of an SQL request, here is my code where it uses a normal SQL request:
Imports System.IO
Imports System.Runtime.Serialization
Imports System.ServiceModel
Imports System.ServiceModel.Activation
Imports System.Data.SqlClient
Imports System.Runtime.Serialization.Json

<ServiceContract([Namespace]:="")> _
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _
Public Class GetFriends
    Inherits System.Web.UI.Page
    <OperationContract()> _
    Public Function GetFriendLocation(friendID As Integer) As String
        Dim friendinfo As New FriendInfo()
        Dim connectionString As String = "server=localhost;uid=sa;pwd=thiru;database=AdventureWorks;"
        Using connection As New SqlConnection(connectionString)
            Dim sql As String = "Select FullName, FriendLocation from Friend.Place " & " Where FriendID = " & friendID.ToString()
            connection.Open()
            Dim command As New SqlCommand(sql, connection)
            Dim reader As SqlDataReader = command.ExecuteReader()
            While reader.Read()
                friendinfo.FriendName = reader("FriendName").ToString()
                friendinfo.FriendLocation = reader("FriendLocation").ToString()
                friendinfo.FriendID = friendID
            End While
        End Using
        Dim stream As New MemoryStream()
        Dim serializer As New DataContractJsonSerializer(GetType(FriendInfo))
        serializer.WriteObject(stream, friendinfo)
        stream.Position = 0
        Dim streamReader As New StreamReader(stream)
        Return streamReader.ReadToEnd()
    End Function
End Class
<DataContract()> _
Public Class FriendInfo
    <DataMember()> _
    Public FriendID As Integer
    <DataMember()> _
    Public FriendName As String
    <DataMember()> _
    Public FriendLocation As String
End Class

Open in new window

I need to modify it so it will connect to an stored procedure but I cannot get it to work, any help would be appreciated.
0
Comment
Question by:JoachimPetersen
  • 3
  • 2
6 Comments
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 39884261
What have you tried and what error(s) (if any) are you getting?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39884646
You need to set SqlCommand.CommandType to StoredProcedure:

Dim command As New SqlCommand(sql, connection)
command.CommandType = CommandType.StoredProcedure
0
 

Author Comment

by:JoachimPetersen
ID: 39927142
Okay tried that, but when I have multiple entries it only returns the first row from the select statement, why is that?
It should select all the rows and write them out in json
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 21

Expert Comment

by:Craig Wagner
ID: 39927212
You are creating one FriendInfo object and then replacing the values you put into that object on each iteration of the loop. I'm a little surprised that it's returning the first row, based on looking at the code it should be returning the last row as JSON.

This question has nothing to do with how you're retrieving the data and everything to do with the fact you're not creating a list of objects to return, you created one instance and then you keep overwriting the data on that one instance as you loop through the rows. Then you return whatever was left in that one instance when you exit the loop.

Walking through this code in the debugger would probably have shown you that. The debugger is a wonderful tool.
0
 

Author Comment

by:JoachimPetersen
ID: 39927294
How can I make the object(FriendInfo) able to be read and written to like an array, like friendinfo[countanumberhere].FriendName?
Or what changes do you suggest? is there a better way then what I am thinking?
0
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 500 total points
ID: 39927595
Because you don't know how many you need in advance you should probably make it a list so you can add to it dynamically. Read up on the generic List type, give it a shot, and come back with any specific questions you may have.

http://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now