Home > Software design >  SQL query works in navicat but not work in vb.net
SQL query works in navicat but not work in vb.net

Time:06-17

I am trying to get data using GROUP BY statement:

SELECT nama_penerbit 
FROM DigitalBook.digitalbook.ebook 
ORDER BY nama_penerbit 

and store this value.

Using navicat

enter image description here

but did not get the value when I try to get in controller in VB.NET:

    <Route("api/Ebooks/GetAllPenerbit")>
    <HttpGet>
    Function GetAllPenerbit() As Results.JsonResult(Of List(Of ebook))
        Dim kategoriEbookRepository As New EbookReposity
        Dim d = kategoriEbookRepository.PenerbitEbookRepository()

        Return Json(d)
    End Function

This is my model class:

Public Function PenerbitEbookRepository() As List(Of ebook)
    Dim sql = "Select nama_penerbit From Digitalbook.digitalbook.ebook Group by nama_penerbit"
    Dim datas As New List(Of ebook)
    Try
        datas = db.Database.SqlQuery(Of ebook)(sql).ToList
    Catch ex As Exception

    End Try
    Return datas
End Function

enter image description here

This is my class

<Table("digitalbook.ebook")>
Partial Public Class ebook
    <Key>
    <StringLength(50)>
    Public Property id_ebook As String

    <StringLength(500)>
    Public Property judul As String

    Public Property id_kategori_ebook As Guid?

    <StringLength(255)>
    Public Property pengarang As String

    <StringLength(255)>
    Public Property nama_penerbit As String
End Class

CodePudding user response:

i created a new class with the same property, then i used it on controller and model, it works fine

This is my new class

Partial Public Class getebook
    Public Property id_ebook As String

    Public Property judul As String

    Public Property id_kategori_ebook As Guid?

    Public Property pengarang As String

    Public Property nama_penerbit As String
End Class

My Controller

        <Route("api/Ebooks/GetAllPenerbit")>
        <HttpGet>
        Function GetAllPenerbit() As Results.JsonResult(Of List(Of getebook))
            Dim kategoriEbookRepository As New EbookReposity
            Dim d = kategoriEbookRepository.PenerbitEbookRepository()

            Return Json(d)
        End Function

My Model

    Public Function PenerbitEbookRepository() As List(Of getebook)
        Dim sql = "Select Distinct nama_penerbit From Digitalbook.digitalbook.ebook"
        Dim datas As New List(Of getebook)
        Try
            datas = db.Database.SqlQuery(Of getebook)(sql).ToList
        Catch ex As Exception

        End Try
        Return datas
    End Function
  • Related