How to set crystal report's textbox value at run time? - asp.net

How to set crystal report's textbox value at run time. I have a textbox in section2 (page header) in crystal report, now I have to set Text property of this textbox at run time. Basically I have to pass user name in this textbox.

You can change textbox text in runtime. You can use this:
using CrystalDecisions.CrystalReports.Engine;
rptMyReport report = new rptMyReport();
TextObject to = (TextObject)report.ReportDefinition.Sections["Section2"].ReportObjects["textboxname"];
to.Text = newvalue;
The another way is to use parameters.

If you have the user name before the report opens you can add a parameter field (string) to the report and then put that field on the report where you want it to appear at runtime. You will just need to pass it into the report as a parameter just like you would any other parameter.
Dim UserName As String = "BukHix"
crDOC.SetParameterValue("UserName", UserName)

try this
((TextObject)rpt.Section2.ReportObjects["Textbox"]).Text = "yourvalue";

Related

Save the value from Multiline Textbox into SQL Server with line brea

I got one multiline textbox call txtDesc.
The user can key in multiline value such as :
Hai.
I'm robot.
Please feed me.
I want to save into database with the line break and will display it back later as user key in.
if i save the value as below code, the line break will not save.
dim strDesc as string = txtDesc.text
how to make this happen?
Thank you.
Love the sample text :)
How are you saving and loading this to/from SQL ? If I have a multiline textbox, the .Text property includes the line break character -
"Hai.\n\I'm robot.\n\Please feed me."
I save this into an NVARCHAR(MAX) field in a table in SQL, and when I load it back into the textbox, I get the line breaks exactly as it was typed in.
Can you post your your load/save code ?
i got the answer :
To save from multiline textbox to database :
Dim strDesc As String
strDesc = ReplaceNewLines(txtDesc.Text, True)
To show back from database to multiline textbox :
strDesc = productDetails.ProductDesc.Replace("<br/>", vbCrLf)
txtDesc.Text = strDesc

Programmatically change dropdownlist selected value

I want to create an application that is able to change the selected value of the dropdownlist in the web page.
I tried to use the code below to change the selected value of the dropdownlist:
Dim TypeSelection As HtmlElement = WebBrowser1.Document.GetElementById("dropdownlist1")
Dim TypeSelectionOption As HtmlElement = TypeSelection.GetElementsByTagName("option").Cast(Of HtmlElement).First(Function(el) el.GetAttribute("value") = "ABC")
TypeSelectionOption.SetAttribute("selected", "true")
TypeSelection.InvokeMember("onchange")
The selected value of the dropdownlist had been changed, but it can't post back to the server, it just hangs at there. I tried to create a testing page to test my code, the code is working & able to post back to the server.
Is there any other method to change the selected value of the dropdownlist, that is able to post back to the server?

Crystal Report direct saving as PDF, instead of viewing

I want to make a report from ASP.Net, in Crystal Report. I want, when user click on print, it should simply show a browser dialog of Save,Open,Save as, and PDF should be saved, or Crystal Report print preview should appear, I don't want to display report first in Viewer then click on button to get print or PDF, I want simply from clicking on asp button, I have all the idea of parameters and know how to make report, my question is just to not to show viewer and take report from asp button in a form of PDF or print preview dialog to print. I have used the Export method of .Net for Crystal Report, but it does not work.
You can generate a PDF by Using a Crystal Report and piece of code....
First: Generate a Crystal Report as per your requirements.
Second: Use the below code to generate the PDF:
Place the following name spaces at the top of the code page
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Variable Declaration
Dim CrReport As New CrystalReport1() // Report Name
Dim CrExportOptions As ExportOptions
Dim CrDiskFileDestinationOptions As New DiskFileDestinationOptions()
Dim CrFormatTypeOptions as New PdfRtfWordFormatOptions()
Set the destination path and file name
CrDiskFileDestinationOptions.DiskFileName = "c:\RichText.pdf"
Specify a page range (optional)
crFormatTypeOptions.FirstPageNumber = 1 // Start Page in the Report
crFormatTypeOptions.LastPageNumber = 3 // End Page in the Report
crFormatTypeOptions.UsePageRange = True
Set export options
CrExportOptions = crReport.ExportOptions
With CrExportOptions
// Set the destination to a disk file
.ExportDestinationType = ExportDestinationType.DiskFile
// Set the format to PDF
.ExportFormatType = ExportFormatType.PortableDocFormat
// Set the destination options to DiskFileDestinationOptions object
.DestinationOptions = CrDiskFileDestinationOptions
.FormatOptions = crFormatTypeOptions
End With
Trap any errors that occur on export
Try
// Export the report
CrReport.Export()
Catch err As Exception
MessageBox.Show(err.ToString())
End Try
Thats it.... Now you are ready to create a PDF of the Report.
Here is the solution you are looking for:
http://www.c-sharpcorner.com/UploadFile/mahesh/ExportCRtoPDF10062006161918PM/ExportCRtoPDF.aspx
Here is the quote from the site:
The following steps will guide you to achieve the same:
Add crystal report (.cr) file to your ASP.NET application.
Add a report instance on the page level.
Dim report As MyReport = New MyReport
Populate reports data on Page_Init
Dim ds As DataSet = GetData()
report.SetDataSource(ds)
Export Report
report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, False, "ExportedReport")
If you wish to format report to other formats, just change the ExportFormatType enumeration value to > your desired format.
If you wish to download the report, then you simply change the third parameter of >ExportToHttpResponse method in Step 4 to True.

How to reference dynamic textboxes created at run time?

I have an ASP project which references a WCF service. Does exactly half of what I need.
A button on the page calls a function from the WCF, which returns a list of objects (variable names). When returned, the vb code dynamically adds textboxes to a panel on the page. Like this:
For Each LetterVariables In LetterVarList
tb = New TextBox
lb = New Label
lb.Text = LetterVariables._key & " "
tb.ID = LetterVariables._key
pnlVars.Controls.Add(lb)
pnlVars.Controls.Add(tb)
Dim LineBreak As LiteralControl = New LiteralControl("<br />")
pnlVars.Controls.Add(LineBreak)
Next
Now the problem is, after this is finished the user will enter some values into those texboxes. I (somehow) need to reference those texboxes to snag the values when a user clicks another button.
How can I do this?
Thanks,
Jason
You can give the TextBox an ID which you could use FindControl to retrieve.
tb.ID = "txt" + LetterVariables._key.ToString();
Then when you want to reference it.
TextBox txtBox = (TextBox)FindControl("txt" + someKey);
Something like that might work for you.
Don't forget to recreate the controls on post back BEFORE the controls are loaded with the posted values.

accessing HtmlTable inside a placeHolder

I'm working with a website written in aspx.net over vb.
I have a placeHolder, and I create a table of names inside this PlaceHolder, each name has an HtmlInputCheckBox next to it.
Im doing this in the aspx.vb file, when the page is uploading.
Then, when the user wants to send mail, he presses a button and than I need to access the checkboxes, and I'm having problems with this, the Sub doesn't know the checkBox object.
I would love for some help,
Thank you!
I understand that you're creating those checkboxes dynamically?
In such case, store them as global member of the class, most simple way is to have List of them:
List<HtmlInputCheckBox> arrCheckboxes = new List<HtmlInputCheckBox>();
...
...
HtmlInputCheckBox myCheckbox = new HtmlInputCheckBox();
arrCheckboxes.Add(myCheckbox);
...
This is C# but should be easy to translate to VB - anyhow having this, you can access the List and it should work.
Worst case as "last resort" you can simply iterate the whole Request.Form collection and look for Keys with name matching to the checkbox name.
Put this in the procedure...
Dim chkValue1 As New CheckBox
Dim chkValue2 As New CheckBox
'Find the Checkbox Controls in the PlaceHolder and cast them to the checkboxes we just made.
chkValue1 = CType(YourPlaceHolder.FindControl("Checkbox1ControlId"), CheckBox)
chkValue2 = CType(YourPlaceHolder.FindControl("Checkbox2ControlId"), CheckBox)
'Now you can do this...
Dim bolIsValue1Checked As Boolean = chkValue1.Checked

Resources