How to display the last inserted records in crystal report
Hi. I have a Sales form. I want that when the user sales 1 or more than 1 product to the customer and after pressing the submit button, the record saves in DataBase and Crystal report of those last inserted records will be created. In simple words i want to create a simple Crystal Report, on which i want to show the last inserted records from DataBase.
Re: How to display the last inserted records in crystal report
Normally I use the RecordSelectionFormula when call the report
In your case it could be to use the invoice number
Also, to get a better help it would be better if you provide CR version & language
JG
... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...
Re: How to display the last inserted records in crystal report
Thank you jggtz.
I'm using Visual Studio 2005 and sql server 2005 standard. CR is the builtin, available in VS.
Secondly, there is no such Invoice number, just Primary Key, which is Sale_ID
Re: How to display the last inserted records in crystal report
But How i will determine that what number of records i need to display? this is my question? Lets say i iinsert 5 records in last attempt, so how only last 5 records will be displayed jggtz?
Re: How to display the last inserted records in crystal report
Following your example... If the last 5 saved records belong to the same Sale operation (same customer) then in your table those records must be identified with something in common (Invoice number or Customer ID or Sale Id or whatever).
If not then it will be not easy
JG
... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...
Re: How to display the last inserted records in crystal report
You have a case of Master-Detail tables
Master -> Sale_Id, Date, Customer, Total, etc
Detail -> Sale_Id, Prod_ID, Qty, Price, LineTotal
Of course you need also the Customers and Products tables
In my opinion the Sale_ID field must be the same for all the same sale operation.
I think You are trying each row as a sale operation
JG
... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...
Re: How to display the last inserted records in crystal report
Hi. I read post # 6 and now i got it. You mean that if i have something common for records then it would be very easy to get the desired output right. So in my table Sale_Date has a uniqueness.
Lets say if the user inserts 2 records, so according to my coding, all these 2 records will be inserted with single insert statement right, so for all these 2 records, the time second will be the same and thus i would search in database that: retrieve all those records which have the latest second of time, so by this way it will display the last inserted record in crystal report??
Re: How to display the last inserted records in crystal report
using second is just because lets suppose if in a minute a user deals 4 customers each for having 4 4 products, so for dealing 4 customers in a single minute, will be problematic. Therefore, i used seconds. What you say?
Re: How to display the last inserted records in crystal report
Sorry for my english...
Let's be clear
As I understand this is a single operation:
-Customer arrives
-Buy 1 coke and 1 bread
To me this is a single sale and must be identified by an ID (the same ID for the 2 rows)
in this way it will be easy to identify and report (among other things)
JG
... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...
Re: How to display the last inserted records in crystal report
Originally Posted by jggtz
You have a case of Master-Detail tables
Master -> Sale_Id, Date, Customer, Total, etc
Detail -> Sale_Id, Prod_ID, Qty, Price, LineTotal
Of course you need also the Customers and Products tables
In my opinion the Sale_ID field must be the same for all the same sale operation.
I think You are trying each row as a sale operation
You may search for Master-Detail tables design
JG
... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...
Re: How to display the last inserted records in crystal report
@ Jggtz, do you refer Master-Detail table, to a Table with solid fields, won't be changed, but its fields will be used in other tables? No changes will be made in Master table, and data will be fetched from that master table to the child tables? Am i right with this?
Re: How to display the last inserted records in crystal report
No
Master is one table and Detail is another table
In Master table you will save : Sale_Id, Date, Customer, Total, etc. all the header information
In Detail table you will save : Sale_Id, Prod_ID, Qty, Price, LineTotal, etc. all the detail information of each row of sale
You see that the Sale_Id field is common both tables and is incremented by one each time a sale is realized
JG
... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...
Re: How to display the last inserted records in crystal report
We have 5 phases in Program development
1. PROBLEM ANALYSIS PHASE
2. PROGRAM DESIGN PHASE
3. CODING PHASE
4. TESTING AND DEBUGGING PHASE
5. DOCUMENTATION PHASE
This problem must have been resolved in phase 2 and I'm not saying that my way is the only way
It's very dificult to resolve design failures in coding phase and the result is a poor program
So could be a right time to check
JG
... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...
Re: How to display the last inserted records in crystal report
Very Well. This time i'll be going with Post # 9, which validly solves my problem and its going all right with my application, so far. I haven't deployed it on client machine, but on my development PC its going all right along.
But before, this I want your opinion once, that whether, they way i adapted in post # 9 is right in your opinion?
Re: How to display the last inserted records in crystal report
This query is not working jggtz.
Look this is my form crystal report from load event code:
Code:
dsSql2.Clear()
Dim selSale As String = "select top 1 Sale_Date from SaleInfo order by Sale_ID Desc"
Dim cmdSql As New SqlCommand(selSale, cnSql)
cmdSql.CommandType = CommandType.Text
daSql.SelectCommand = cmdSql
daSql.Fill(dsSql2, "SaleInfo")
TextBox1.DataBindings.Add("text", dsSql2, "SaleInfo.Sale_Date")
this will load and bind the last record from DB, to textbox1. Okay.
When textbox wil be loaded with last value, then i will press the button and the following code will be execute.
Code:
cnSql.Open()
Dim sel As String = "select * from SaleInfo where Sale_Date = '" & TextBox1.Text & "'"
Dim cmdSql As New SqlCommand(sel, cnSql)
cmdSql.CommandType = CommandType.Text
daSql.SelectCommand = cmdSql
daSql.Fill(dsSql2, "SaleInfo")
Dim objRpt As New CrystalReport1
objRpt.SetDataSource(dsSql2.Tables("SaleInfo"))
CrystalReportViewer2.ReportSource = objRpt
CrystalReportViewer2.Refresh()
cnSql.Close()
But when i press the button so it does not load anything? Please tell me that what is wrong wit this.
Re: How to display the last inserted records in crystal report
I set the format to Short and Custom Format to yyyy/mm/dd hh:mm:ss, but its still not working. For cross checking. I added a DataGridView on my form and the new code is:
Code:
Dim sel As String = "select * from SaleInfo where Sale_Date = @Sale_Date"
Dim cmdSql As New SqlCommand(sel, cnSql)
cmdSql.CommandType = CommandType.Text
daSql.SelectCommand = cmdSql
With cmdSql
.Parameters.Add("@Sale_Date", SqlDbType.DateTime).Value = DateTimePicker1.Text
End With
daSql.Fill(dsSql2, "SaleInfo")
DataGridView1.DataSource = dsSql2
DataGridView1.DataMember = "SaleInfo"
Dim objRpt As New CrystalReport1
objRpt.SetDataSource(dsSql2.Tables("SaleInfo"))
CrystalReportViewer2.ReportSource = objRpt
CrystalReportViewer2.Refresh()
cnSql.Close()
Now this code displaying the record in DGV, but not in Crystal Report? Whats wrong now?
Re: How to display the last inserted records in crystal report
Not working either. Neither by design, nor by coding.
Okay let me ask you something (just to make this problem solve).
In my main form called SaleInfo. On this form, the customer sales multiple products with single Insert statement right. Let's say he sells 5, so all 5 records will be inserted into DataGridView and then to DB. right. After this i want that automatically report of those saled products is generated, as a customer Receipt (Likewise, we receive after shopiing from Uitlity Store). What my problem is, that How i will determine on Crystal Report that how many number of records were inserted in last attempt? This is the whole problem, for which i actually need crystal Report.
Re: How to display the last inserted records in crystal report
I got it since first post
But you are walking the hardest path
You need a common field but you don't want to add a new field
You say with the datetime field and had problems... try it harder
It's important to consider the db... if it's SQL or Access or Oracle... then the datetime format will be different in the sql query
JG
... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...
Re: How to display the last inserted records in crystal report
Hi.
I change the query last night and its working now. Here is my new code:
This is my form load event code:
Code:
Dim selSale As String = "SELECT * FROM SaleInfo WHERE Sale_Date = (SELECT Top 1 Sale_Date FROM SaleInfo ORDER BY Sale_Date DESC)"
Dim cmdSql As New SqlCommand(selSale, cnSql)
cmdSql.CommandType = CommandType.Text
daSql.SelectCommand = cmdSql
daSql.Fill(dsSql2, "SaleInfo")
DateTimePicker1.DataBindings.Add("text", dsSql2, "SaleInfo.Sale_Date")
And this is my button click event code:
Code:
cnSql.Open()
Dim sel As String = "select * from SaleInfo where Sale_Date = @Sale_Date"
Dim cmdSql As New SqlCommand(sel, cnSql)
cmdSql.CommandType = CommandType.Text
daSql.SelectCommand = cmdSql
With cmdSql
.Parameters.Add("@Sale_Date", SqlDbType.DateTime).Value = DateTimePicker1.Text
End With
daSql.Fill(dsSql2, "SaleInfo")
DataGridView1.DataSource = dsSql2.Tables("SaleInfo")
Dim objRpt As New CrystalReport1
objRpt.SetDataSource(dsSql2.Tables("SaleInfo"))
CrystalReportViewer2.ReportSource = objRpt
CrystalReportViewer2.Refresh()
cnSql.Close()
I also have a query; Its also showing my desired output, but I'm not sure that which one is better now, the below one or the above one?
Code:
SELECT * FROM SaleInfo WHERE Sale_Date = ( SELECT MAX(Sale_Date) FROM SaleInfo)