none
Date type parameter in Report Viewer RRS feed

  • Question

  • Hi all !

    Please help me ..urgent...In Visual Studio VB 2005

    I am holding a VB.Net form with a Report viewer dropped in it along with 2 TextBox es and a Button.

    Now i have linked a report tht works well in the query builder with FromDate and ToDate parameter.

    In the VB.Net Code when i tried to execute the form im always getting a error like either

    'Type 'FromDate' string cannot be converted to 'Date' type '  or

     

    Value of type 'Date' cannot be converted to 'Microsoft.Reporting.WinForms.ReportParameter'.

     

    please assist me how to achieve passing Date type parameter as REportParameter in the ReportViewer.

     

     

    Code is as follows:

    -------------------------------------

     

    Dim F_Date As Date = TextBox1.ToString

    Dim T_Date As Date = TextBox2.ToString

    Dim FDate As New ReportParameter(F_Date)

     Dim TDate As New ReportParameter(T_Date)

    Dim rp() As ReportParameter = {FDate, TDate}

     

    'TODO: This line of code loads data into the 'Sales.DataTable1' table. You can move, or remove it, as needed.

            Me.DataTable1TableAdapter.Fill(Me.Sales.DataTable1, "F_Date", "T_Date")

            Me.ReportViewer1.LocalReport.SetParameters(rp)

            Me.ReportViewer1.RefreshReport()

     

     

    ******

     

     

     

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Try

                Me.DataTable1TableAdapter.Fill(Me.Sales.DataTable1, TextBox1.ToString, TextBox2.ToString)

                Me.ReportViewer1.RefreshReport()

            Catch ex As Exception

                System.Windows.Forms.MessageBox.Show(ex.Message)

            End Try

        End Sub

     

    ----------------------------------------

     

    PLease any one help me....

    Thursday, July 22, 2010 6:51 AM

Answers

  • ;) We don't typically use this forum for general coding help but for you we'll let it slip this time.

    Like I mentioned in issue #3 above, you probably want to pass in something of type Date in both the 2nd and 3rd input, since that seems to be how your TableAdapter is set up. The suggested code

    Me.DataTable1TableAdapter.Fill(Me.Sales.DataTable1, F_Date, T_Date)

    passes in F_Date and T_Date, which both in your original code are variables of type Date. You should be able to use the Date.Parse() method to convert the string in your TextBox to return a Date object. For exampe:

    Me.DataTable1TableAdapter.Fill(Me.Sales.DataTable1, Date.Parse(TextBox1.Text), Date.Parse(TextBox2.Text))


    Cephas Lin This posting is provided "AS IS" with no warranties.
    • Marked as answer by Radhai Tuesday, July 27, 2010 10:42 AM
    Friday, July 23, 2010 2:40 PM
    Moderator
  • Exact same conversion error?

    I would check your dataset's design.cs file in your project to see how Fill() is defined when the TableAdapter is automatically generated in yor project, and it's generated based on the query you give it in the table designer wizard. If the second and third argument accept String, then you should use String as input, likely "12/5/2010". If it's Date (DateTime), then you'll need to convert it to a Date (DateTime) object first, likely Date.Parse("12/5/2010"). Apart from that, it's looking into the exception properties in the debugger, etc. Again, some of this really requires that you get proficient with the VS debugger, and also going through a good book on VB.NET.

    You can also post your project somewhere and I can take a quick look.


    Cephas Lin This posting is provided "AS IS" with no warranties.
    • Marked as answer by Radhai Tuesday, July 27, 2010 10:41 AM
    Monday, July 26, 2010 4:20 PM
    Moderator

All replies

  • General observation: It helps a lot to become proficient in using VS debugging to find out what properties and methods are actually returning rather than trying to assign something and hope for the best. :)

    There are three problems I see with this code.

    First:

    Dim F_Date As Date = TextBox1.ToString
    Dim T_Date As Date = TextBox2.ToString

    try to assign a String object to a Date variable, which is not allowed. Also, you should use debugging to see what's returned by ToString. It's not what you want any way. This problem also applies to the following line:

    Me.DataTable1TableAdapter.Fill(Me.Sales.DataTable1, TextBox1.ToString, TextBox2.ToString)

    Second:

    Dim FDate As New ReportParameter(F_Date)
    Dim TDate As New ReportParameter(T_Date)

    will result in ReportParameter objects with the actual date-time value as the parameter name, which I don't believe is what you want. Take a look at http://msdn.microsoft.com/en-US/library/microsoft.reporting.winforms.reportparameter.reportparameter(v=VS.80).aspx. I think the one you want to use is the constructor that takes a name and value pair. Here's an example:

    Dim FDate As New ReportParameter("FromDate", TextBox1.Text)

    This will work assuming that you already have a named parameter in your report called "FromDate".

    Third:

    Me.DataTable1TableAdapter.Fill(Me.Sales.DataTable1, "F_Date", "T_Date")

    Depending on how you setup your query, I believe what you actually want is the following code, to get the actual dates as filters for your query. The strings "F_Date" and "T_Date" won't do you any good.

    Me.DataTable1TableAdapter.Fill(Me.Sales.DataTable1, F_Date, T_Date)


    Cephas Lin This posting is provided "AS IS" with no warranties.
    Thursday, July 22, 2010 3:31 PM
    Moderator
  • Dear Cephalin,

    Thks for your kind help...

    Please find below the modified code as per your advice,

      Dim FDate As New ReportParameter("FDate", TextBox1.Text) 
      Dim TDate As New ReportParameter("TDate", TextBox2.Text) 
      Dim rp() As ReportParameter = {FDate, TDate}
      'TODO: This line of code loads data into the 'Sales.DataTable1' table. You can move, or remove it, as needed.
      Me.DataTable1TableAdapter.Fill(Me.Sales.DataTable1, "FDate", "TDate")
      Me.ReportViewer1.LocalReport.SetParameters(rp)
      Me.ReportViewer1.RefreshReport()
    

     

    But when debugging i am getting the following error pointing the underlined line, 

    "InvalidCastException was unhandled              

    Conversion from string "FDate" to type 'Date' is not valid."

     

    Kindly guide me is still there any mistakes. I am new to Coding.

     

    Many Thanks,

    Radhai.

     

     

     

    Friday, July 23, 2010 11:40 AM
  • ;) We don't typically use this forum for general coding help but for you we'll let it slip this time.

    Like I mentioned in issue #3 above, you probably want to pass in something of type Date in both the 2nd and 3rd input, since that seems to be how your TableAdapter is set up. The suggested code

    Me.DataTable1TableAdapter.Fill(Me.Sales.DataTable1, F_Date, T_Date)

    passes in F_Date and T_Date, which both in your original code are variables of type Date. You should be able to use the Date.Parse() method to convert the string in your TextBox to return a Date object. For exampe:

    Me.DataTable1TableAdapter.Fill(Me.Sales.DataTable1, Date.Parse(TextBox1.Text), Date.Parse(TextBox2.Text))


    Cephas Lin This posting is provided "AS IS" with no warranties.
    • Marked as answer by Radhai Tuesday, July 27, 2010 10:42 AM
    Friday, July 23, 2010 2:40 PM
    Moderator
  • Dear Cephalin, Thank you for ur extended help...

    But again conversion error. I guess may the date in the DB field may cause the problem.

    The FieldType is DateTime in the DB. When displaying the date it ll get displayed as '1/25/2010 11:15 AM' like that.. 

    ll such existence cause the issue?

     

    Thanking you,

    Radhai.

    Saturday, July 24, 2010 5:43 AM
  • Exact same conversion error?

    I would check your dataset's design.cs file in your project to see how Fill() is defined when the TableAdapter is automatically generated in yor project, and it's generated based on the query you give it in the table designer wizard. If the second and third argument accept String, then you should use String as input, likely "12/5/2010". If it's Date (DateTime), then you'll need to convert it to a Date (DateTime) object first, likely Date.Parse("12/5/2010"). Apart from that, it's looking into the exception properties in the debugger, etc. Again, some of this really requires that you get proficient with the VS debugger, and also going through a good book on VB.NET.

    You can also post your project somewhere and I can take a quick look.


    Cephas Lin This posting is provided "AS IS" with no warranties.
    • Marked as answer by Radhai Tuesday, July 27, 2010 10:41 AM
    Monday, July 26, 2010 4:20 PM
    Moderator
  • Thanks a LOT Cephalin !!! It worked for me !!
    Tuesday, July 27, 2010 10:41 AM