?
Solved

ASP .net JSON webservice - Problems with stored procedure.

Posted on 2014-02-24
6
Medium Priority
?
159 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

762 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