Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

Help with passing value from textbox in ASPX page to a MS report

Hello,

I need to modify the color of a textbox in my report depending on the values entered in a textbox on my form. How do i achieve this?

For example I would like to set the Background color of my  report's Textbox to red if the user
enter BEL in the APSX's form's textbox.

Thanks,

Victor
Avatar of MikeMCSD
MikeMCSD
Flag of United States of America image

if (txt1.Text == "BEL")
            {
                txt1.BackColor = System.Drawing.Color.Red;
            }
Avatar of Victor  Charles

ASKER

I'm afraid this code would not work. I need to pass the value entered in textbox1.text of my asps form to my report and modify the background color of textbox2.text in my report.
Hi,

For example I need to modify the code below for it to work with the report.

Textbox1 on in the aspx form
Textbox2 is on the MS Report

If Textbox2.text = Textbox1.text then
Textbox2.backcolor = "red"
endif
Avatar of Kumaraswamy R
rptMyReport report = new rptMyReport();
TextObject to = (TextObject)report.ReportDefinition.Sections["Section2"].ReportObjects["textboxname"];
to.Text = newvalue;
Hi,

Can you please explain the code to me. I can't figure out where you are taking the value entered in Textbox1.Text (from the aspx form).
Also how are you changing the background color of Textbox2.text (from RDLC) when Textbox1.Text = Texbox2.Text?

Thanks.
Hello Victor,

Your approach should be as follows:

1. Take the Color mentioned by the user in ASPX form.
2. Pass it as a parameter to your Report
3. In your report, use this parameter to set background colors to the required textboxes/cells.

Please read below link to see how parameters are passed to Report from ASPX:
http://www.codeproject.com/Articles/15469/Integrating-Reporting-Services-2005-Into-a-Web-App

By the way, were you able to create parameters for colors and use them for cells inside report?

Thanks,
Harish
Hi,

I am still confused, will look at  the link and get back to you.

For example when you say

1. Take the Color mentioned by the user in ASPX form.

I am not sure what you mean because the color with depend on the text in Report's texbox,  for example if:

Textbox1 on in the aspx form
Textbox2 is on the MS Report

If Textbox2.text = Textbox1.text then
Textbox2.backcolor = "red"
endif

Victor
Victor,

I thought you wanted to pass the color to the report. Ok, read the points as below:

1. Take the value in TextBox1 in ASPX form.
2. Pass it as a parameter to your Report
3. In your report, use this parameter to compare values in the cells and set background color to the required textboxes/cells.


Thanks,
Harish
Got it, will look at the links to figure out how to do steps 2 and 3, if can't still can't figure it out, I will ask for more help.
Hi,

Below is the code in C# to pass the value of the Textbox as a parameter to the report, but I still can figure out how to use it to set the value of the textbox that is in the report, can you please help me with the propoer syntax in VB.NET


Button_Click event:

Dim textcolor as string
textcolor = Textbox1.Text

RptParameters[0] =
    new Microsoft.Reporting.WebForms.ReportParameter("TextboxColor", textcolor);
this.ReportViewer1.ServerReport.SetParameters(RptParameters);
this.ReportViewer1.ServerReport.Refresh();
Victor,

You are again confusing me!
Dim textcolor as string
textcolor = Textbox1.Text

That means you want to pass the name of the color to the report.
Anyway, let me see if I can make a sample report and aspx code for you.

Thanks,
Harish
Hi,

Textcolor was not a propoer name to use, I am trying to pass for example the country and see if it matches the value of the textbox in the report, for example if user Enters BEL, change the color in the report if textbox in report = BEL

Dim CountryText as string
CountryText = Textbox1.Text

RptParameters[0] =
    new Microsoft.Reporting.WebForms.ReportParameter("TextboxColor", CountryText);
this.ReportViewer1.ServerReport.SetParameters(RptParameters);
this.ReportViewer1.ServerReport.Refresh();

Thanks,

V.
Hi again,

I need to resolve the single entry first, but in some cases if the user enters BEL,CAN,USA, I need to somehow change the color for all the entries, for example, If textbox in report IN ( Textbox1.Text)  where Texbox.text = BEL,CAN,USA, change the color in the report if textbox in report = BEL or CAN, or USA. Thanks for working on a small report.
Hello Victor,

Have you deployed your report on a server? In that case, here is the code to pass a parameter to the report and retrieve the report.

1. I created ASPX file with a Report Viewer.
2. Below is the code I used for the button to retrieve the report:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim textcolor As String = TextBox1.Text
        Dim RptParameters As Microsoft.Reporting.WebForms.ReportParameter() = New Microsoft.Reporting.WebForms.ReportParameter(1) {}

        'Initialize report
        ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote
        ReportViewer1.ServerReport.ReportServerUrl = New Uri("http://harishv-l057/ReportServer_SQL2008R2") ' Report Server URL 
        ReportViewer1.ServerReport.ReportPath = "/TextColor Example" ' Report Name 
        ReportViewer1.ShowParameterPrompts = False
        ReportViewer1.ShowPrintButton = True

        'Assign values to parameters
        RptParameters(0) = New Microsoft.Reporting.WebForms.ReportParameter("TextboxColor1", textcolor)
        RptParameters(1) = New Microsoft.Reporting.WebForms.ReportParameter("TextboxColor2", "LightGreen")

        'Retrieve report
        Me.ReportViewer1.ServerReport.SetParameters(RptParameters)
        Me.ReportViewer1.ServerReport.Refresh()

    End Sub

Open in new window


I have attached the report file and the Default.aspx file I used.

Here is a useful link that shows how to call SSRS report from ASPX. Please read it, it is very simple.
http://forums.asp.net/t/1281716.aspx/1

And here is another link to convert c# code to VB.Net easily:
http://www.developerfusion.com/tools/convert/csharp-to-vb/

Thanks,
Harish
Default.aspx
Default.aspx.vb
Default.aspx.designer.vb
TextColor-Example.rdl
Harish,

I modified your code for the Windows version, but I'm not certain how you are passing the value to the report, the name of the textbox of the report is TxtReceiver. Where do I include it in the code?


What does the following code do?

      RptParameters(0) = New Microsoft.Reporting.WinForms.ReportParameter("TextboxColor1", textcolor)
        RptParameters(1) = New Microsoft.Reporting.WinForms.ReportParameter("TextboxColor2", "LightGreen")


Complete code:



    Dim dt As New DataTable()
        dt.TableName = "Links"
        dt.Columns.Add("Link_ID")
        dt.Columns.Add("Receiver")
        dt.Columns.Add("Donor")
    Dim linker As XElement = XElement.Load(Application.StartupPath + "\Link.xml")
    Dim receiver As XElement = XElement.Load(Application.StartupPath + "\Receiver.xml")
    Dim Donor As XElement = XElement.Load(Application.StartupPath + "\Donor.xml")
        For Each item As XElement In linker.Elements("Row")

    Dim linkID As String = item.Element("Link_ID").Value
    Dim receiverId As String = item.Element("Receiver_ID").Value
    Dim DonorID As String = item.Element("Donor_ID").Value 'Add (3)
    Dim receiverVal As String = String.Empty
    Dim xe As XElement = receiver.Elements("Row").Cast(Of XElement)().Where(Function(n) n.Element("Receiver_ID").Value = receiverId).FirstOrDefault()
            If xe IsNot Nothing Then
                receiverVal = xe.Element("Receiver").Value
            End If
    Dim DonorVal As String = String.Empty
            xe = Donor.Elements("Row").Cast(Of XElement)().Where(Function(n) n.Element("Donor_ID").Value = DonorID).FirstOrDefault()
            If xe IsNot Nothing Then
                DonorVal = xe.Element("Donor").Value
            End If
    Dim dr As DataRow = dt.NewRow()
            dr("Link_ID") = linkID
            dr("Receiver") = receiverVal
            dr("Donor") = DonorVal 'Add (5)
            dt.Rows.Add(dr)
        Next
    Dim bs As New BindingSource()
        bs.DataSource = dt

        'View Report

        Dim textcolor As String = TextBox1.Text
        Dim RptParameters As Microsoft.Reporting.WinForms.ReportParameter() = New Microsoft.Reporting.WinForms.ReportParameter(1) {}

        Dim reportDataSource As New Microsoft.Reporting.WinForms.ReportDataSource()
        reportDataSource.Name = "MyDataSet_Links"
        reportDataSource.Value = dt

        RptParameters(0) = New Microsoft.Reporting.WinForms.ReportParameter("TextboxColor1", textcolor)
        RptParameters(1) = New Microsoft.Reporting.WinForms.ReportParameter("TextboxColor2", "LightGreen")



        ReportViewer1.LocalReport.DataSources.Add(reportDataSource)
        ReportViewer1.LocalReport.SetParameters(RptParameters)
        ReportViewer1.LocalReport.Refresh()
        ReportViewer1.RefreshReport()


Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Harish Varghese
Harish Varghese
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Got it. Thank You very much.