Page 1 of 2 12 LastLast
Results 1 to 40 of 45

Thread: How to display the last inserted records in crystal report

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    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.

    Please guide me that how to this?

  2. #2
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    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 ...

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    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

  4. #4
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: How to display the last inserted records in crystal report

    Then use Sale_ID as the RecordSelectionFormula
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    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?

  6. #6
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    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 ...

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to display the last inserted records in crystal report

    Hi jggtz. These are my table fields, in which Sale_ID is primary key and Prod_ID is Foreigh Key. Here nothing is common. Please guide me for this.

    Sale_ID, Prod_ID, Sale_Prod_Qty, Sale_Prod_PriceOfOne, Sale_Prod_PriceOfAll, Sale_Date

  8. #8
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    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 ...

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    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??

    Give your opinion Please???

  10. #10
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: How to display the last inserted records in crystal report

    If that's the way you want to follow, I would try hour & minutes only
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    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?


    Secondly you say that
    If that's the way you want to follow
    What other way you suggest?

  12. #12
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    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 ...

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to display the last inserted records in crystal report

    To me this is a single sale and must be identified by an ID (the same ID for the 2 rows)
    how to have same ID for 2 rows? Please explain.

  14. #14
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: How to display the last inserted records in crystal report

    Quote Originally Posted by jggtz View Post
    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 ...

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    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?

  16. #16
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    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 ...

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to display the last inserted records in crystal report

    @ jggtz, what you mean by
    all the header information

    I'm not understanding this master and detail tables scenario that how to do this?

  18. #18
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: How to display the last inserted records in crystal report

    Let's say this is a sale operation:


    This goes in Master table
    Sale ID : 123
    Date : 23 / 03 / 2013
    Customer : 321 Mr. Henry Flowers
    Total Sale : $7.50

    this goes in Detail table
    Sale ID:123 Qty.: 2 Item: 1922837 Description: Can of coke Price: $1.00 Total: $2.00
    Sale ID:123 Qty.: 3 Item: 2567885 Description: Piece of xxx Price: $1.00 Total: $3.00
    Sale ID:123 Qty.: 5 Item: 1925332 Description: Candies abc Price: $0.50 Total: $2.50

    Common field is Sale ID
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to display the last inserted records in crystal report

    Oh, for this purpose, i need to change a massive modification in my design as well as Coding. Right. even my Analysis as well.

  20. #20
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    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 ...

  21. #21

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    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?

  22. #22
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: How to display the last inserted records in crystal report

    Not 100% right
    What will happen if the user save 1 record at 14:59:59 and the next at 15:00:35

    You can try storing the last saleIDs in an array and pass to CR as a filter
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  23. #23

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to display the last inserted records in crystal report

    jggtz

    What will happen if the user save 1 record at 14:59:59 and the next at 15:00:35
    What will wrong happen with these 2 times?

  24. #24
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: How to display the last inserted records in crystal report

    If you are sure that the field "time" will have the same hour-min-sec for all the last saved records then go ahead...
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  25. #25

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to display the last inserted records in crystal report

    Yes jggtz. I inserted 6 records with one click, so it has the following image result. Please check it and find that is it fine or not?
    Attached Images Attached Images  

  26. #26
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: How to display the last inserted records in crystal report

    It's ok
    You could use the date/time field to select the records
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  27. #27

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    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.

  28. #28
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: How to display the last inserted records in crystal report

    Try formating the date/time in textbox as yyyymmdd hh:mm:ss
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  29. #29

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to display the last inserted records in crystal report

    it doesn't change, because i tried and the bind data can't be changed.

  30. #30
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: How to display the last inserted records in crystal report

    We don't want to change it, just to format... try it... or do not use binding
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  31. #31

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    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?

  32. #32
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: How to display the last inserted records in crystal report

    yyyymmdd hh:mm:ss
    date without /
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  33. #33

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    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.

    Did you get my jggtz?

  34. #34
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    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 ...

  35. #35

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to display the last inserted records in crystal report

    I'm using sql server 2005 standard.

    Secondly, if you are with me, so i'll add new field in it. Just guide me through.

  36. #36
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: How to display the last inserted records in crystal report

    Ok
    I'll try to help you to select using that datetime field
    Gimme some time and I'll be back with some answer
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  37. #37
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: How to display the last inserted records in crystal report

    Just test in a SQL Server 2005 and I can Select usinf datetime fields using the format as I posted in posts 28 & 32

    Please post your actual code where you are selecting the last inserted records using datetime field
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  38. #38

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    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)

  39. #39
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: How to display the last inserted records in crystal report

    Both are ok
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  40. #40

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to display the last inserted records in crystal report

    But I am worry about your post # 22? Please guide me that what is the expected error the user can get?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width