Solved

ASP .net JSON webservice - Problems with stored procedure.

Posted on 2014-02-24
6
154 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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

863 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

19 Experts available now in Live!

Get 1:1 Help Now