Solved

ASP .net JSON webservice - Problems with stored procedure.

Posted on 2014-02-24
6
157 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

749 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