'**************************************************************
'
' Staff Functions
'
'**************************************************************
Public Function Staff(param As String) As String Implements CentralService.Staff
SQLStr = "SELECT Name_First, Name_Last, email_MSU, PhotoFileName FROM tbl_people WHERE Name_First LIKE '% @partial %' OR Name_Last LIKE '% @partial %'"
Dim jss As JavascriptSerializer
Dim Employees As New List(Of Employee)
Dim StaffList As String = ""
Dim FName As String = ""
Dim LName As String = ""
Dim Email As String = ""
Dim PhotoURL As String = ""
jss = New JavascriptSerializer()
Using SQLConn As New SqlConnection(ConnString)
Using SQLCmd As New SqlCommand(SQLStr, SQLConn)
SQLCmd.Parameters.AddWithValue("@partial", param)
SQLConn.Open()
Dim SQLdr As SqlDataReader = SQLCmd.ExecuteReader()
Try
While SQLdr.Read()
If IsDBNull(SQLdr.Item("Name_First")) Then
FName = ""
Else
FName = SQLdr.Item("Name_First")
End If
If IsDBNull(SQLdr.Item("Name_Last")) Then
LName = ""
Else
LName = SQLdr.Item("Name_Last")
End If
If IsDBNull(SQLdr.Item("email_MSU")) Then
Email = ""
Else
Email = SQLdr.Item("email_MSU")
End If
If IsDBNull(SQLdr.Item("PhotoFileName")) Then
PhotoURL = ""
Else
PhotoURL = SQLdr.Item("PhotoFileName")
End If
Employees.Add(New Employee() With {.firstName = FName, .lastName = LName, .email = Email, .photo = PhotoURL})
End While
Catch ex As Exception
Return ex.ToString
End Try
End Using
End Using
Return jss.Serialize(Employees)
End Function
52 Replies - 2236 Views - Last Post: 10 November 2012 - 08:10 AM
#31
Re: RestFul service & SQL Query
Posted 24 October 2012 - 07:47 AM
#32
Re: RestFul service & SQL Query
Posted 24 October 2012 - 07:55 AM
rgfirefly24, on 24 October 2012 - 08:25 AM, said:
In doing some testing, I'm not sure where the \s are coming from actually. When I look at my output of .Serialize it doesn't have the escape character. What I'll do now is try and build your environment as closely as possible and see if i can't reproduce it. I do have one question though. The JSON string you supplied. Where did you get it from?
That came from me running http://localhost:577...ice.svc/Staff/d
#33
Re: RestFul service & SQL Query
Posted 24 October 2012 - 07:58 AM
SQLStr = "SELECT Name_First, Name_Last, email_MSU, PhotoFileName FROM tbl_people WHERE Name_First LIKE @partial OR Name_Last LIKE @partial"
...
SQLCmd.Parameters.AddWithValue("@partial", "'%"+param+"%'")
I suspect because you have a space between the % and @partial it is causing your issue
maddtechwf, on 24 October 2012 - 09:55 AM, said:
since it is running as localhost I can't look at it, but the issue might be in several places. Because this is a method that returns to another place it can be anywhere. I would suggest setting a breakpoint and following the logical execution of the code and finding at which point the backslashes are added in.
#34
Re: RestFul service & SQL Query
Posted 24 October 2012 - 12:43 PM
#35
Re: RestFul service & SQL Query
Posted 29 October 2012 - 07:33 AM
#36
Re: RestFul service & SQL Query
Posted 29 October 2012 - 10:39 AM
#37
Re: RestFul service & SQL Query
Posted 29 October 2012 - 10:53 AM
<WebGet(UriTemplate:="/Staff/{param}", ResponseFormat:=WebMessageFormat.Json)>
<OperationContract()>
Function Staff(param As String) As String
and my Service1.svc.vb has this
'**************************************************************
'
' Staff Functions
'
'**************************************************************
Public Function Staff(param As String) As String Implements CentralService.Staff
SQLStr = "SELECT Name_First, Name_Last, email, PhotoFileName FROM tbl_people WHERE Name_First LIKE @partial OR Name_Last LIKE @partial"
Dim jss As JavascriptSerializer
Dim Employees As New List(Of Employee)
Dim FName As String = ""
Dim LName As String = ""
Dim Email As String = ""
Dim PhotoURL As String = ""
jss = New JavascriptSerializer()
Using SQLConn As New SqlConnection(ConnString)
Using SQLCmd As New SqlCommand(SQLStr, SQLConn)
SQLCmd.Parameters.AddWithValue("@partial", "%" & param & "%")
SQLConn.Open()
Dim SQLdr As SqlDataReader = SQLCmd.ExecuteReader()
Try
While SQLdr.Read()
If IsDBNull(SQLdr.Item("Name_First")) Then
FName = ""
Else
FName = SQLdr.Item("Name_First")
End If
If IsDBNull(SQLdr.Item("Name_Last")) Then
LName = ""
Else
LName = SQLdr.Item("Name_Last")
End If
If IsDBNull(SQLdr.Item("email")) Then
Email = ""
Else
Email = SQLdr.Item("email")
End If
If IsDBNull(SQLdr.Item("PhotoFileName")) Then
PhotoURL = ""
Else
PhotoURL = SQLdr.Item("PhotoFileName")
End If
Employees.Add(New Employee() With {.firstName = FName, .lastName = LName, .email = Email, .photo = PhotoURL})
End While
Catch ex As Exception
Return ex.ToString
End Try
End Using
End Using
Return jss.Serialize(Employees)
End Function
I was just reading and I didn't know if since I declared my Response format to be JSON and then I was also serializing the data before sending it out, if that was doing it.
This post has been edited by maddtechwf: 29 October 2012 - 10:54 AM
#38
Re: RestFul service & SQL Query
Posted 30 October 2012 - 02:04 PM
Employee_RSS.vb
Public Class Employee_RSS
Public Property ID As Integer
Public Property Name As New List(Of FullNameTitle)
Public Property Email As String
Public Property Dept As String
Public Property Building As String
Public Property Room As String
Public Property Title As String
Public Property Photo As String
Public Property Phone As String
Public Property Fax As String
End Class
FullNameTitle.vb
Public Class FullNameTitle
Public Property Title As String
Public Property First As String
Public Property Middle As String
Public Property Last As String
Public Property Suffix As String
End Class
Service1.svc.vb
Public Function StaffRSS() As String Implements CentralService.StaffRSS
SQLStr = "SELECT tbl_people.ID, tbl_people.Name_Title, tbl_people.Name_First, tbl_people.Name_Last, tbl_people.Name_Middle, tbl_people.Name_Suffix, tbl_people.PhotoFileName, tbl_people.email_ FROM tbl_people"
'Declare Variables
Dim jss As JavascriptSerializer = New JavascriptSerializer
Dim RSSEmployee As New List(Of Employee_RSS)
Dim id As String = ""
Dim TName As String = ""
Dim FName As String = ""
Dim MName As String = ""
Dim LName As String = ""
Dim SName As String = ""
Dim email As String = ""
Dim dept As String = ""
Dim building As String = ""
Dim room_numb As String = ""
Dim title As String = ""
Dim photo As String = ""
Dim phone As String = ""
Dim fax As String = ""
Using SQLConn As New SqlConnection(ConnString)
Using SQLCmd As New SqlCommand(SQLStr, SQLConn)
SQLConn.Open()
Dim SQLdr As SqlDataReader = SQLCmd.ExecuteReader()
Try
While SQLdr.Read()
If IsDBNull(SQLdr.Item("ID")) Then
id = "0"
Else
id = SQLdr.Item("ID").ToString
End If
If IsDBNull(SQLdr.Item("Name_Title")) Then
TName = ""
Else
TName = SQLdr.Item("Name_Title")
End If
If IsDBNull(SQLdr.Item("Name_First")) Then
FName = ""
Else
FName = SQLdr.Item("Name_First")
End If
If IsDBNull(SQLdr.Item("Name_Middle")) Then
MName = ""
Else
MName = SQLdr.Item("Name_Middle")
End If
If IsDBNull(SQLdr.Item("Name_Last")) Then
LName = ""
Else
LName = SQLdr.Item("Name_Last")
End If
If IsDBNull(SQLdr.Item("Name_Suffix")) Then
SName = ""
Else
SName = SQLdr.Item("Name_Suffix")
End If
If IsDBNull(SQLdr.Item("email_")) Then
email = ""
Else
email = SQLdr.Item("email_")
End If
If IsDBNull(SQLdr.Item("PhotoFileName")) Then
photo = ""
Else
photo = SQLdr.Item("PhotoFileName")
End If
RSSEmployee.Add(New Employee_RSS() With {.Email = email, .Name = New List(Of FullNameTitle) With {.Title = TName, .First = FName, .Middle = MName, .Last = LName, .Suffix = SName}, .ID = id})
End While
Catch ex As Exception
Return ex.ToString
End Try
End Using
End Using
Return jss.Serialize(RSSEmployee)
End Function
This post has been edited by maddtechwf: 30 October 2012 - 02:19 PM
#39
Re: RestFul service & SQL Query
Posted 31 October 2012 - 05:13 AM
Since you're assigning a class to the Name value, you'll have to create that FullNameTitle class and assign the values inside the loop. Just dim an instance of it in your loop (or do it from outside the loop, and then redim it in the loop - I'm no expert on VB.NET), and then after all of your assignments in the If/Else block, try
myFullNameTitle.Title = Tname myFullNameTitle.First = Fname myFullNameTitle.Middle = Mname myFullNameTitle.Last = Lname myFullNameTitle.Suffix = Sname
I'll assume that you've changed the Employee's Name field from a List of FullNameTitle to a single instance of FullNameTitle now, so then adding it to the list would look like this:
RSSEmployee.Add(New Employee_RSS() With {.Email = email, .Name = myFullNameTitle, .ID = id})
I didn't actually check any of this in VS, because I don't have any active VB.NET projects to check against, but the basic idea should be right even if you have to tweak the syntax.
#40
Re: RestFul service & SQL Query
Posted 31 October 2012 - 06:42 AM
#41
Re: RestFul service & SQL Query
Posted 31 October 2012 - 07:22 AM
#42
Re: RestFul service & SQL Query
Posted 31 October 2012 - 12:00 PM
#43
Re: RestFul service & SQL Query
Posted 31 October 2012 - 12:05 PM
#44
Re: RestFul service & SQL Query
Posted 01 November 2012 - 07:09 AM
With that said, I have a .dbml that has four tables in it and their relationship set. I want to travers from on table down to another one to find what I need. I just don't know how to write that in my EmployeesController.cs.
EmployeeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Http.WebHost;
using System.Web.Http.WebHost.Routing;
namespace hello_services.Controllers {
// Inheriting from the APIController class will designate this as a WebAPI endpoint
public class EmployeesController : ApiController {
// the linq to sql context that provides the data access layer
private Data.ContactManagerContextDataContext _context = new Data.ContactManagerContextDataContext();
// WebAPI will respond to an HTTP GET with this method
public List<Models.Employee> Get() {
// get all of the records from the employees table in the
// northwind database. return them in a collection of user
// defined model objects for easy serialization.
var employees = from e in _context.tbl_peoples
select new Models.Employee
{
Id = e.ID,
First = e.Name_First,
Middle = e.Name_Middle,
Last = e.Name_Last,
Title = e.Name_Title,
Suffix = e.Name_Suffix,
email = e.email_1,
photo = e.PhotoFileName
};
// returns the employees as a list, which is converted
// to an array during serialization to JSON
return employees.ToList();
}
}
}
My source that I'm basing this off of is here.
This post has been edited by maddtechwf: 01 November 2012 - 07:11 AM
#45
Re: RestFul service & SQL Query
Posted 06 November 2012 - 01:15 PM
|
|

New Topic/Question
Reply




MultiQuote





|