Is there a way to export data that has been populated into a VB.net forms application split by Tabs? I have a large program that connects to a SQL database and runs many queries to populate Labels within the application.
I would like to export the data into an Excel Sheet over different tabs per Tabular Page in the Form application. If this is not possible then a generic .csv export will be fine.
I am running my SQL query and importing it into Labels like this -
Dim myCmd23 As New SqlCommand("MySQL Query", myconn)
Dim myDataA23 As New SqlDataAdapter(myCmd23)
Dim myDataT23 As New DataTable()
Dim myDataS23 As New DataSet()
myDataA23.Fill(myDataT23)
DataGridView1.DataSource = myDataT23
Dim result23(2) As String
result23(1) = DataGridView1.Rows(0).Cells(0).Value.ToString
result23(2) = DataGridView1.Rows(0).Cells(1).Value.ToString
Label1.Text = result23(1)
Label2.Text = result23(2)
There are many of these SqlCommands carried out. The Form application looks like such -
The seperate tabs are shown above and i would like to try and seperate this data in the Export?
The Dates and Filters are taken into account within each seperate SQL query so they are no bother.
If anybody has a possible solution that would be great.
Thanks
Greg
There are several blogs about exporting datatables to Excel in VB.NET here
Related
I have read every single SO question and online article regarding this, and I get confused in a couple different instances.
As my project sits, I have tried to create a Report (Report2.rdlc) manually and drag different fields from DataSources onto the report, and use that report as the data source for the ReportViewer. This did not work. I need to use the DataTable I created from a SqlDataAdapterbecause it decrypts specific rows. I tried creating a DataSet and filling it with the DataTable, however I was unable to perform that, also.
I don't understand: if I have a ReportViewer control on a
WinForm, what all I need in my code to bind a data source to it.
Is the code even creating a report and utilizing the report for
the ReportViewer?
The DataTable name is dt and the ReportViewer control is rv1.
Here's the code I have been toying around with, however I am not sure what to put as the Report Path
Dim RDS1 As ReportDataSource = New ReportDataSource("Test Report", dt)
rv1.LocalReport.DataSources.Clear()
rv1.ProcessingMode = ProcessingMode.Local
rv1.LocalReport.EnableExternalImages = True
rv1.LocalReport.ReportEmbeddedResource = "Your Report Path"
rv1.LocalReport.DataSources.Add(RDS1)`.
The worst part is, the ReportViewer just shows up blank. There are no errors or any indicators as to what could be going wrong.
The information within the DataTable dt is all correct (verified by viewing it in a DGV). I am just trying to use that data in a Report / ReportViewer.
Does anyone have any advice? I cannot seem to catch a break on this issue.
Note: Exporting to Excel is not an option. There are encrypted values that require decryption. The report needs to be printable.
Edit: Here is how I populate the DataTable:
Dim cmd As New SqlCommand
cmd.CommandText = "Select * FROM Participant " & _
"WHERE FIRST_NM_TXT = #searchFirst " & _
"OR LAST_NM_TXT = #searchLast"
cmd.Parameters.AddWithValue("#searchFirst", SearchFirstTxt.Text)
cmd.Parameters.AddWithValue("#searchLast", SearchLastTxt.Text)
Dim adapter As New SqlDataAdapter(cmd)
adapter.Fill(dt)
So, here we have the DataTable with the correct data in it. I have then gone to the Project Name, Added a new Report (Report1.rdlc), and from here I am unsure of the next steps. If I create a DataSet dynamically, should I see it in this window?
Since this seems to be a popular thread, I'll explain how I was able to do this in quick/easy steps.
Right click on your porject -> Add New Item -> Selection Report.rdlc
Top left (Data Source) -> New -> Dataset (select Database [Next ->] Dataset [Next ->] Your DB connection.
"Choose your database objects" screen -> Select Tables
"Choose the dataset" screen -> This will be reset at run time. Make sure you remember the name of this Dataset, because it will be used in the code.
Add ReportViewer control (under Reporting) to the form. Select reportviewer control and go to properties (bottom right pane in VS). Select Local Report's property and set ReportEmbeddedResource to point to the report path we just created. ** ProjectName.ReportName.rdlc**
Peform the usual:
Public DataSet FillDS()
//Try Catch block & other code omitted
Dim cmd As New SqlCommand
cmd.CommandText = "Select * FROM Participant " & _
"WHERE FIRST_NM_TXT = #searchFirst " & _
"OR LAST_NM_TXT = #searchLast"
cmd.Parameters.AddWithValue("#searchFirst", SearchFirstTxt.Text)
cmd.Parameters.AddWithValue("#searchLast", SearchLastTxt.Text)
Dim adapter As New SqlDataAdapter(cmd)
adapter.Fill(dt)
Then, we bind the datasource at run time.
DataSet yourDS = FillDS();
ReportDataSource rds = New ReportDataSource("YourDataSetNameFromStep4", yourDS.Tables[0]);
yourReportViewerName.localreport.datasources.clear();
yourReportViewerName.localreport.datasources.add(rds);
yourReportViewerName.refreshreport();
If there is something I could post in here to help anyone else, let me know.
Some things to check:
"Test Report" must be the name used in the report designer. It's case sensitive.
The dt must match the name used in the report designer too
"Your report Path" should be of the form "namespace.reportname.rdlc" . It's case sensitive.
EDIT:
The approach I normally use utilises an extra layer, in the form of a BindingSource as follows:
Fill a strongly-typed dataset using the ad-hoc query and a SqlDataAdapter.
Dim a Bindingsource and set its DataSource to the dataset and its DataMember to the table name.
Dim a ReportDataSource and set its name to the name used in the report designer, and its value to the BindingSource
eg
Using cn As New SqlConnection(gcs)
cn.Open()
Dim sa As New SqlDataAdapter(sql, cn)
sa.SelectCommand.CommandTimeout = cGlobals.ReportTimeout
sa.Fill(ds, "Foos")
End Using
bs.DataSource = ds
bs.DataMember = "Foos"
Dim rds As New ReportDataSource
rds.Name = "dsFooList_Foos"
rds.Value = bs
rv1.LocalReport.DataSources.Add(rds)
Me.Show()
rv1.RefreshReport()
Edit 2: At the point in your screenshot, drop down the datasources and pick the one you want. It will then appear in the Report Data window, which may be hiding in the 'View' menu (it's only there when you're working with a report) Then you can add a Table to the report from the toolbox, and then drag the fields from the Report Data window to the table cells.
something similar I have made for my "report loader", please check this link, it is written in C# but it will give you an idea how to do it.
How to bind dynamically datasource to reportviewer on windows forms c#
I'm relatively new to form programming in Visual Basic and need some help.
I've successful got what I want from my database into a datagrid but need this in a report viewer.
dgvReport.DataSource = dsReport
Dim ds As DataTable = New DataTable("GenerateReport")
Dim ReportDataSource = New ReportDataSource("GenerateReport", ds)
rvReport.LocalReport.DataSources.Clear()
rvReport.LocalReport.DataSources.Add(ReportDataSource)
rvReport.LocalReport.ReportEmbeddedResource = "Test.Report1.rdlc"
rvReport.RefreshReport()
Where 'dsReport' is the name of the dataset I'm using.
'Generate Report' is the table name within the dataset.
"Test.report1.rdlc" is the path of the report file.
I've searched and found this code, but the error I get says that the the program cannot find the file. I was unaware that there's a file saved which you then copy the data from into the report viewer.
I don't know if this is right, what I'm doing. Any help is much appreciated.
I am using VB .NET and the Visual Studio Crystal Reports Tool. I am generating a report that uses an SQL query, fills the datatable, and adds a datacolumn based on the field values in the datatable. The datatable is populated, but doesn't display on the report. I am using an XSD file as the datasource for the report which contains the SQL query fields as well as the datacolumn field.
Dim sqlAdapter As New SqlDataAdapter()
Dim dt As New DataTable
sqlAdapter.SelectCommand = New SqlCommand(SELECT * FROM [database], validConnectionString)
sqlAdapter.Fill(dt)
Dim addedColumn As New DataColumn ("addedColumn", Type.GetType("System.String"))
dt.Columns.Add(addedColumn)
'Logic to fill the column (tested and used without fail throughout other parts of project)
Dim bsv As New CRReportViewer 'class type that works for all other reports
Dim rpt As New CompletedReport 'class type that follow pattern that works elsewhere
bsv.DataContext = Me
rpt.SetDataSource(dt)
bsv.ReportViewer.ViewerCore.ReportSource = rpt
bsv.Show()
If I insert something like 'MsgBox(dt.Rows(0).Item("addedColumn)) I get a valid value. This value doesn't appear in the report however. Everything within the SelectCommand appears without problem.
The above code does work. The problem was a setting within the field of the .rpt file. There was a problem with its length and default submission.
As an aside, it seems that once dt contains valid entries--everything else falls in the Crystal reports editor and the .rpt file.
I am new to reportviewer and struggling with the concepts.
I realise that I am probably being very stupid here. I have tried reading up on the reportviewer but not found any tutorials except those that drag and drop datasets. which is not what I want to do.
I want to create a report from a single datatable at first just as a learning exercise.
I have created a dataset added the table to it and have tried this code but I get an error: 'The report definition for report "C:\Users\Mike\BM\Reports\" has not been specified. Object reference not set to an instance of an object'
I don't really understand what the 'The report definition' bit means?
I would appreciate some guidance please.
Dim MyTestDS As New DataSet
Dim myTestTable As New DataTable
myTestTable = Data.Accounts.Table.Copy
MyTestDS.Tables.Add(myTestTable)
Dim DSReport As New ReportDataSource()
DSReport.Name = "MyTestDS"
DSReport.Value = MyTestDS.Tables(0)
Dim PathReport As String = "C:\Users\Mike\BM\Reports\"
ReportViewer1.LocalReport.ReportEmbeddedResource = PathReport
ReportViewer1.LocalReport.DataSources.Clear()
ReportViewer1.LocalReport.DataSources.Add(DSReport)
ReportViewer1.LocalReport.Refresh()
ReportViewer1.RefreshReport()
As a learning exercise, I think is better not using embedded resource so you can simply specify the complete report path of your rdlc file; for example:
ReportViewer1.LocalReport.ReportPath = "C:\Users\Mike\BM\Reports\YourReportFile.rdlc"
If you want to use an embedded report, I think you have to get it like this:
Get Embedded Resource
Copy YourReportFile.rdl to YourReportFile.rdlc
I have read every single SO question and online article regarding this, and I get confused in a couple different instances.
As my project sits, I have tried to create a Report (Report2.rdlc) manually and drag different fields from DataSources onto the report, and use that report as the data source for the ReportViewer. This did not work. I need to use the DataTable I created from a SqlDataAdapterbecause it decrypts specific rows. I tried creating a DataSet and filling it with the DataTable, however I was unable to perform that, also.
I don't understand: if I have a ReportViewer control on a
WinForm, what all I need in my code to bind a data source to it.
Is the code even creating a report and utilizing the report for
the ReportViewer?
The DataTable name is dt and the ReportViewer control is rv1.
Here's the code I have been toying around with, however I am not sure what to put as the Report Path
Dim RDS1 As ReportDataSource = New ReportDataSource("Test Report", dt)
rv1.LocalReport.DataSources.Clear()
rv1.ProcessingMode = ProcessingMode.Local
rv1.LocalReport.EnableExternalImages = True
rv1.LocalReport.ReportEmbeddedResource = "Your Report Path"
rv1.LocalReport.DataSources.Add(RDS1)`.
The worst part is, the ReportViewer just shows up blank. There are no errors or any indicators as to what could be going wrong.
The information within the DataTable dt is all correct (verified by viewing it in a DGV). I am just trying to use that data in a Report / ReportViewer.
Does anyone have any advice? I cannot seem to catch a break on this issue.
Note: Exporting to Excel is not an option. There are encrypted values that require decryption. The report needs to be printable.
Edit: Here is how I populate the DataTable:
Dim cmd As New SqlCommand
cmd.CommandText = "Select * FROM Participant " & _
"WHERE FIRST_NM_TXT = #searchFirst " & _
"OR LAST_NM_TXT = #searchLast"
cmd.Parameters.AddWithValue("#searchFirst", SearchFirstTxt.Text)
cmd.Parameters.AddWithValue("#searchLast", SearchLastTxt.Text)
Dim adapter As New SqlDataAdapter(cmd)
adapter.Fill(dt)
So, here we have the DataTable with the correct data in it. I have then gone to the Project Name, Added a new Report (Report1.rdlc), and from here I am unsure of the next steps. If I create a DataSet dynamically, should I see it in this window?
Since this seems to be a popular thread, I'll explain how I was able to do this in quick/easy steps.
Right click on your porject -> Add New Item -> Selection Report.rdlc
Top left (Data Source) -> New -> Dataset (select Database [Next ->] Dataset [Next ->] Your DB connection.
"Choose your database objects" screen -> Select Tables
"Choose the dataset" screen -> This will be reset at run time. Make sure you remember the name of this Dataset, because it will be used in the code.
Add ReportViewer control (under Reporting) to the form. Select reportviewer control and go to properties (bottom right pane in VS). Select Local Report's property and set ReportEmbeddedResource to point to the report path we just created. ** ProjectName.ReportName.rdlc**
Peform the usual:
Public DataSet FillDS()
//Try Catch block & other code omitted
Dim cmd As New SqlCommand
cmd.CommandText = "Select * FROM Participant " & _
"WHERE FIRST_NM_TXT = #searchFirst " & _
"OR LAST_NM_TXT = #searchLast"
cmd.Parameters.AddWithValue("#searchFirst", SearchFirstTxt.Text)
cmd.Parameters.AddWithValue("#searchLast", SearchLastTxt.Text)
Dim adapter As New SqlDataAdapter(cmd)
adapter.Fill(dt)
Then, we bind the datasource at run time.
DataSet yourDS = FillDS();
ReportDataSource rds = New ReportDataSource("YourDataSetNameFromStep4", yourDS.Tables[0]);
yourReportViewerName.localreport.datasources.clear();
yourReportViewerName.localreport.datasources.add(rds);
yourReportViewerName.refreshreport();
If there is something I could post in here to help anyone else, let me know.
Some things to check:
"Test Report" must be the name used in the report designer. It's case sensitive.
The dt must match the name used in the report designer too
"Your report Path" should be of the form "namespace.reportname.rdlc" . It's case sensitive.
EDIT:
The approach I normally use utilises an extra layer, in the form of a BindingSource as follows:
Fill a strongly-typed dataset using the ad-hoc query and a SqlDataAdapter.
Dim a Bindingsource and set its DataSource to the dataset and its DataMember to the table name.
Dim a ReportDataSource and set its name to the name used in the report designer, and its value to the BindingSource
eg
Using cn As New SqlConnection(gcs)
cn.Open()
Dim sa As New SqlDataAdapter(sql, cn)
sa.SelectCommand.CommandTimeout = cGlobals.ReportTimeout
sa.Fill(ds, "Foos")
End Using
bs.DataSource = ds
bs.DataMember = "Foos"
Dim rds As New ReportDataSource
rds.Name = "dsFooList_Foos"
rds.Value = bs
rv1.LocalReport.DataSources.Add(rds)
Me.Show()
rv1.RefreshReport()
Edit 2: At the point in your screenshot, drop down the datasources and pick the one you want. It will then appear in the Report Data window, which may be hiding in the 'View' menu (it's only there when you're working with a report) Then you can add a Table to the report from the toolbox, and then drag the fields from the Report Data window to the table cells.
something similar I have made for my "report loader", please check this link, it is written in C# but it will give you an idea how to do it.
How to bind dynamically datasource to reportviewer on windows forms c#