Solved

ASP .net JSON webservice - Problems with stored procedure.

Posted on 2014-02-24
6
156 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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 setup several different housekeeping processes for a SQL Server.
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.

828 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