I'm trying to create a report that will display my table that has joins from different tables that looks likes this,
select * from tblproduct As p inner join tblbrand As b On p.bid = b.brandid inner Join tblclassification As c On p.cid = c.classid inner Join tblformulation As f On p.fid = f.formid inner Join tblgeneric As g On p.gid = g.genericid inner Join tbltype As t On p.tid = t.typeid
My table consist of 8 columns and 4 of it is column id's for joins, I can succesfully use the query when im trying to display to datagridview but when I try this in reportviewer it doesn't work and gives me error: "Object reference not sent to an instance of an object". This is what I tried,
Public Class frmInventoryReport
Public Property strReport2 As String
Private Sub frmInventoryReport_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.tblproductTableAdapter.Fill(Me.groceryDataSet.tblproduct)
Me.ReportViewer1.RefreshReport()
Dim rptDataSource As New ReportDataSource
Try
With Me.ReportViewer1.LocalReport
.ReportPath = "D:\VisualBasic\EELPOS - Copy\EELPOS\Report3.rdlc"
.DataSources.Clear()
End With
Select Case strReport2
Case "Report3"
Dim i As Integer = 0
Dim sqlQuery As String = "select * from tblproduct As p inner join tblbrand As b On p.bid = b.brandid inner Join tblclassification As c On p.cid = c.classid inner Join tblformulation As f On p.fid = f.formid inner Join tblgeneric As g On p.gid = g.genericid inner Join tbltype As t On p.tid = t.typeid"
Dim da As New MySqlDataAdapter(sqlQuery, cn)
Dim dt As DataTable = New DataTable()
While dr.Read
dt.Rows.Add(i, dr.Item("barcode").ToString, dr.Item("generic").ToString, dr.Item("classification").ToString, dr.Item("type").ToString, dr.Item("formulation").ToString, dr.Item("price").ToString, dr.Item("qty").ToString)
End While
da.Fill(dt)
rptDataSource = New ReportDataSource("inventory", dt)
End Select
Me.ReportViewer1.LocalReport.DataSources.Add(rptDataSource)
Catch ex As Exception
MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Me.ReportViewer1.RefreshReport()
End Sub
End Class
What's the correct way to do it?
Related
I've got 2 datatables and am trying to summarize the data in them using a left outer join. The join works fine with this code
Dim Journal = From entries In dt.AsEnumerable()
Join inccodes In dtGL.AsEnumerable()
On entries.Field(Of String)("GLCode") Equals inccodes.Field(Of String)("GLCode")
Group By keys = New With {Key .IncomeCode = entries.Field(Of String)("GLCode"), Key .IncomeDesc = .inccodes.Field(Of String)("GLCodeDesc")}
Into ChargeSum = Group, sm = Sum(entries.Field(Of Decimal)("Amount"))
Where sm <> 0
Select New GL_Journal With {.IncomeCode = keys.IncomeCode, .IncomeDesc = keys.IncomeDesc, .LineAmount = sm}
`
However, since I really want a Left Outer Join I want to use Group Join instead of Join.
As soon as I change the Join to Group Join the code in the Group by at ".inccodes.field(Of String)("GLCodeDesc")" has ".inccodes" highlighted with the error "'inccodes' is not a member of 'anonymous type'"
I've reviewed much documentation on Group By and Group Join but there is scant information on them together.
Any ideas? Would I have more options/success with the method syntax?
If i try to reproduce your query using a left outer join, I will do something like this :
Dim dt As New DataTable
dt.Columns.Add("GLCode", GetType(String))
dt.Columns.Add("Amount", GetType(Decimal))
dt.Rows.Add("111", 3251.21)
dt.Rows.Add("222", 125.79)
dt.Rows.Add("999", 10000)
Dim dtGL As New DataTable
dtGL.Columns.Add("GLCode", GetType(String))
dtGL.Columns.Add("GLCodeDesc", GetType(String))
dtGL.Rows.Add("111", "a")
dtGL.Rows.Add("222", "b")
dtGL.Rows.Add("333", "c")
Dim Journal = From entries In dt.AsEnumerable()
Group Join inccodes In dtGL.AsEnumerable()
On entries.Field(Of String)("GLCode") Equals inccodes.Field(Of String)("GLCode")
Into Group
From lj In Group.DefaultIfEmpty()
Group By keys = New With {Key .IncomeCode = entries.Field(Of String)("GLCode"), Key .IncomeDesc = lj?.Field(Of String)("GLCodeDesc")}
Into ChargeSum = Group, sm = Sum(entries.Field(Of Decimal)("Amount"))
Select New With {.IncomeCode = keys.IncomeCode, .IncomeDesc = keys.IncomeDesc, .LineAmount = sm}
so I'm getting this error:
The main thing is that sometimes it might run smooth and get no errors some others i get the errors that what i don't get. Also, SQL wise the code is running there is no problem with that. I don't know what might be wrong any ideas would be helpful thanks in advance. Also the line I get the error is Adapter.Fill(SourceDataSet).
Try
'Δήλωση Μεταβλητών
strConnection = String.Format("Provider=SQLOLEDB;Data Source={0};Initial Catalog={1};User ID={2};Password={3};",
strServer, strDataBase, strUserName, strPassword)
' Create a connection object.
Dim Connection As New OleDbConnection(strConnection)
' Create a data adapter.
Dim Adapter As New OleDbDataAdapter("USE EMBONILO_B
SELECT M.CODE AS ΚΩΔΙΚΟΣ_ΕΙΔΟΥΣ , M.DESCRIPTION AS ΠΕΡΙΓΡΑΦΗ_ΕΙΔΟΥΣ , M.SUBCODE1 AS ΚΩΔΙΚΟΣ_ΚΑΤΑΣKEYAΣΤΗ , MF.DESCR AS BRAND , TS.DESCR AS ΣΑΙΖΟΝ, IG.DESCR AS ΟΜΑΔΑ_ΕΙΔΟΥΣ , TF.DESCR AS ΦΥΛΟ , IG2.DESCR AS ΤΥΠΟΣ ,
SC.DESCR AS SPORTSCODE , M.SUBCODE2 AS ΠΕΡΙΓΡ_ΧΡΩΜΑΤΟΣ , M.COMPOSITION AS ΣΥΝΘΕΣΗ , MS.DESCR AS ΜΟΝ_ΜΕΤΡ , VAT.VATDESCR AS ΦΠΑ , SL.DESCR AS ΜΕΓΕΘΟΛΟΓΙΟ , IC.COLORDESCR AS ΚΩΔ_ΧΡΩΜ ,
M.DESCR2 AS ΕΝΑΛ_ΠΕΡΙΓΡ , M.WHSPRICE AS ΧΟΝΔΡΙΚΗ , M.RTLPRICE AS ΛΙΑΝΙΚΗ , M.FLDSTRING1 AS ΠΧ_ΦΥΛΟ , M.FLDSTRING2 AS ΠΧ_ORIGIN_COUNTRY , M.FLDSTRING3 AS ΠΧ_ΣΑΙΖΟΝ , M.FLDSTRING5 AS ΠΧ_ΕΤΟΣ ,
M.FLDSTRING4 AS ΠΧ_ΚΑΤΗΓΟΡΙΑ , M.FLDSTRING6 AS ΠΧ_ΧΡΩΜΑ
FROM MATERIAL M
INNER JOIN MANUFACTURER MF ON MF.CODEID = M.MNFID
INNER JOIN ITEMGROUP IG ON IG.CODEID = M.IGPID
INNER JOIN IP_TBLFILLO TF ON TF.CODEID=M.IP_EPS_MODEL
INNER JOIN ITEMGROUP2 IG2 ON IG2.CODEID=M.IGSID
INNER JOIN INF_SPORTSCODE SC ON SC.CODEID=M.INF_SPORTSCODE
INNER JOIN MESUNIT MS ON MS.CODEID=M.MU1
INNER JOIN VATCATEGORY VAT ON VAT.CODEID=M.VTCID
INNER JOIN SIZELIST SL ON SL.CODEID=M.MAINSZLID
INNER JOIN ITECOLOR IC ON IC.ITEID=M.ID
INNER JOIN ITEMCATEGORY TS ON TS.CODEID=M.ICTID
WHERE M.CODE LIKE '" & txtSearch.Text & "%'", Connection)
' Create and fill a dataset.
Dim SourceDataSet As New DataSet()
Adapter.Fill(SourceDataSet)
' Specify the data source for the grid control.
grdMain.DataSource = SourceDataSet.Tables(0)
If GridView2.RowCount = 0 Then
MsgBox("Δεν υπάρχει τέτοιος κωδικός.", vbOKOnly)
grdMain.DataSource = Nothing
End If
Connection.Close()
Catch oleDbExceptionParameter As OleDbException
DevExpress.XtraEditors.XtraMessageBox.Show(oleDbExceptionParameter.StackTrace.ToString, Application.ProductName,
MessageBoxButtons.OK, MessageBoxIcon.Error)
Catch ex As Exception
DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, My.Application.Info.Title)
End Try
I am trying to convert this query:
select sales.TTL_Sales_Net, sales.TTL_Sales_Target, units.TTL_Units_Net
from epos_Anal_Branch_Sales_Day sales
join epos_PULL_PM_SKU_STATUS till on sales.BRANCH_ID = till.BRANCH_ID
join epos_Anal_Branch_Units_Day units on sales.BRANCH_ID = units.BRANCH_ID
where till.BRANCH_ID = 45 and sales.DATEKEY = 20140725 and units.DATEKEY = 20140725
from SQL into an Entity Framework query in VB.net. I am sending in the BRANCH_ID in my function and producing the datekey like so:
Dim MyDate As String = Date.Now.ToString("yyyyMMdd")
This is what I have but it just returns no results and I am at a complete loss as to why! This is my query thus far, if anyone can spot what I have done wrong then I would be grateful for a push in the right direction
Public Class clsStoreSales
Public Property DailyAmount As String
Public Property DailyUnits As String
Public Property SalesTarget As String
Public Property ErrorMessages As String
Public Shared Function GetStoreSales(ByVal BranchID As String) As List(Of clsStoreSales)
Dim live As New RMISEntitiesLive
Dim MyDate As String = Date.Now.ToString("yyyyMMdd")
Dim l As New List(Of clsStoreSales)
Try
Dim r = From till In live.epos_PULL_PM_SKU_STATUS
Join sales In live.epos_Anal_Branch_Sales_Day On till.BRANCH_ID Equals sales.BRANCH_ID
Join units In live.epos_Anal_Branch_Units_Day On till.BRANCH_ID Equals units.DATEKEY
Where till.BRANCH_ID = CDbl(BranchID) AndAlso sales.DATEKEY = CDbl(MyDate) AndAlso units.DATEKEY = CDbl(MyDate)
Select New With {.SalesAmount = sales.TTL_Sales_Net, .SalesTarget = sales.TTL_Sales_Target, .SaleUnits = units.TTL_Units_Net}
For Each t In r
Dim m As New clsStoreSales
m.DailyAmount = CStr(t.SalesAmount)
m.DailyUnits = CStr(t.SaleUnits)
m.SalesTarget = CStr(t.SalesTarget)
l.Add(m)
Next
Return l
Catch ex As Exception
Dim e As New clsStoreSales
e.ErrorMessages = ex.ToString
l.Add(e)
Return l
End Try
End Function
End Class
This is what sqlServer Profiler is showing as the query being run:
exec sp_executesql N'SELECT
[Extent1].[BRANCH_ID] AS [BRANCH_ID],
[Extent2].[TTL_Sales_Net] AS [TTL_Sales_Net],
[Extent2].[TTL_Sales_Target] AS [TTL_Sales_Target],
[Extent3].[TTL_Units_Net] AS [TTL_Units_Net]
FROM [dbo].[epos_PULL_PM_SKU_STATUS] AS [Extent1]
INNER JOIN [dbo].[epos_Anal_Branch_Sales_Day] AS [Extent2] ON [Extent1].[BRANCH_ID] = [Extent2].[BRANCH_ID]
INNER JOIN [dbo].[epos_Anal_Branch_Units_Day] AS [Extent3] ON [Extent1].[BRANCH_ID] = [Extent3].[DATEKEY]
WHERE ( CAST( [Extent1].[BRANCH_ID] AS float) = #p__linq__0) AND ( CAST( [Extent2].[DATEKEY] AS float) = #p__linq__1) AND ( CAST( [Extent3].[DATEKEY] AS float) = #p__linq__2)',N'#p__linq__0 float,#p__linq__1 float,#p__linq__2 float',#p__linq__0=45,#p__linq__1=20140725,#p__linq__2=20140725
This
Join units In live.epos_Anal_Branch_Units_Day On till.BRANCH_ID Equals units.DATEKEY
Should be this!
Join units In live.epos_Anal_Branch_Units_Day On till.BRANCH_ID Equals units.BRANCH_ID
I have created the Datatable below which feeds a GridView on Page_Load
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strQuery As String = "SELECT DISTINCT setsTbl.setName, trainingTbl.t_date, userAssessmentTbl.o_id FROM userAssessmentTbl LEFT JOIN trainingTbl ON userAssessmentTbl.tt_id = trainingTbl.tt_id LEFT JOIN setsTbl ON trainingTbl.s_id = setsTbl.s_id LEFT JOIN outcomesTbl ON userAssessmentTbl.o_id = outcomesTbl.o_id WHERE UserId = #UserId ORDER BY setName, t_date DESC "
Dim cmd As New SqlCommand(strQuery)
cmd.Parameters.AddWithValue("#UserId", userString)
Dim dt As DataTable = GetData(cmd)
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
At present it outputs values:
SetName t_date o_id
name1 12.12.12 1
name2 13.07.12 1
name2 15.07.12 2
name3 16.07.12 4
name3 17.07.12 3
I would like to get my DataTable below to output distinct rows like this:
SetName t_date o_id
name1 12.12.12 1
name2 13.07.12 1
name3 16.07.12 4
can anybody tell me hw this can be achieved?
It looks like you want the earliest date for each name. If so, you could use a Group By and the min function:
setsTbl.setName, Min(trainingTbl.t_date), Min(outcomesTbl.o_id)
FROM userAssessmentTbl LEFT JOIN trainingTbl ON userAssessmentTbl.tt_id = trainingTbl.tt_id
LEFT JOIN setsTbl ON trainingTbl.s_id = setsTbl.s_id LEFT JOIN outcomesTbl
ON userAssessmentTbl.o_id = outcomesTbl.o_id
WHERE UserId = #UserId Group By setsTbl.setName ORDER BY setsTbl.setName
I can't figure out that linq to entity query syntax. My problem is that if the value of the Calls table is null then noting comes up, I want to make something like a left join to get 'all' rows from the Calls table.
I tried to group it but I can't figure out the correct way to write it.
Dim TicketQuery As ObjectQuery = From c In EnData.Customer _
Join t In EnData.Calls On t.CustomerID Equals c.CustomerID _
Join Status In EnData.Lists On t.Status Equals Status.ListValue _
Join Project In EnData.Lists On t.Project Equals Project.ListValue _
Join Priorty In EnData.Lists On t.Priority Equals Priorty.ListValue _
Where c.Status > -1 And t.Status > -1 And Status.ListType = 1 And Project.ListType = 3 And Priorty.ListType = 2 _
Select New With {c.CustName, t.CallID, t.CallDate, t.CallTime, t.Description, Key .Status = Status.ListText, Key .Project = Project.ListText, t.DateModified, Key .Priority = Priorty.ListText}
How can I fix that?
Similar question: Linq to Sql: Multiple left outer joins
Microsoft Documentation: http://msdn.microsoft.com/en-us/library/bb918093.aspx#Y916
LINQ Examples from: http://msdn.microsoft.com/en-us/vbasic/bb737909
Left Outer Join
A so-called outer join can be expressed with a group join. A left outer joinis like a cross join, except that all the left hand side elements get included at least once, even if they don't match any right hand side elements. Note how Vegetables shows up in the output even though it has no matching products.
Public Sub Linq105()
Dim categories() = {"Beverages", "Condiments", "Vegetables", "Dairy Products", "Seafood"}
Dim productList = GetProductList()
Dim query = From c In categories _
Group Join p In productList On c Equals p.Category Into Group _
From p In Group.DefaultIfEmpty() _
Select Category = c, ProductName = If(p Is Nothing, "(No products)", p.ProductName)
For Each v In query
Console.WriteLine(v.ProductName + ": " + v.Category)
Next
End Sub
For left join in VB.net we can use Let
Dim q =
(From item In _userProfileRepository.Table
Let Country = (From p In _countryRepository.Table Where p.CountryId = item.CurrentLocationCountry Select p.Name).FirstOrDefault
Let State = (From p In _stateRepository.Table Where p.CountryId = item.CurrentLocationCountry Select p.Name).FirstOrDefault
Let City = (From p In _stateRepository.Table Where p.CountryId = item.CurrentLocationCountry Select p.Name).FirstOrDefault
Where item.UserId = item.ProfileId.ToString)
After the left join we can use group by
Dim q2 =
(From p In q Group p By p.Country, p.State, p.City Into Group
Select New With
{
.Country = Country,
.State = State,
.City = City,
.Count = Group.Count
}).ToList()