connection string for reports

megatronixs

Member
Joined
Jan 15, 2018
Messages
11
Programming Experience
Beginner
Hi all,

I'm trying to look for info on how to create C# windows forms with reports. there are plenty of info on how to create a report from scratch, but I could only find the ones that you define the data source from some drop down boxes and you need to set the connection etc.
I come from Ms Access databases and there creating a report is in a way more easy as the report you create is based on the same database. Initially when I started with adding datagridview to the windows forms, I had the same thing that needed to add the dataconnection (selecting database and then add it to the datagridview)
I managed to find how to create a data connection via code and it easier to manipulate. Is this possible to do this with the windows forms reports?
like here in this code example:

C#:
        //-- new connection will be created
        private OleDbConnection connection = new OleDbConnection();
        public DataForm()
        {
            InitializeComponent();
            //-- database location
            connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;
            Data Source=\\FeGrand\Shareddata\Tools\Feedback\Feedback.accdb";

private void btn_search_Click(object sender, EventArgs e)
        {
            try
            {
                connection.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                string temp = PrepairWhereToQuery();

                string query = "SELECT * FROM tbl_main_data WHERE " + temp;
              
                command.CommandText = query;
                OleDbDataAdapter da = new OleDbDataAdapter(command);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dgr_data.DataSource = dt;
                //-- rename headers
                dgr_data.Columns[0].HeaderText = "Record Nr";
                dgr_data.Columns[1].HeaderText = "Area";
                dgr_data.Columns[2].HeaderText = "TL/Manager/Head";
                dgr_data.Columns[3].HeaderText = "Coach";
                dgr_data.Columns[4].HeaderText = "Type of meeting";
                dgr_data.Columns[5].HeaderText = "Date";
                dgr_data.Columns[6].HeaderText = "Advantages";
                dgr_data.Columns[7].HeaderText = "Disadvantages";
                dgr_data.Columns[8].HeaderText = "Other Comments";
                //-- hide headers if necesary
                //this.dgr_data.Columns["record_id"].Visible = false;
                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex);
                connection.Close();
            }
        }

I'm looking for a starting point to be able to develop my skills further :)

Greetings
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,646
Location
Sydney, Australia
Programming Experience
10+
If you're talking about using SQL Server Reporting Services and creating a client report then yes, you can use a data adapter or data reader to populate a DataTable and then use that as the data source for your report. The following question seems to be about a web application but the use of a ReportViewer is basically the same in Windows Forms and Web Forms, so you should be able to do pretty much the same thing.

https://stackoverflow.com/questions/14203414/fill-report-from-datatable
 

megatronixs

Member
Joined
Jan 15, 2018
Messages
11
Programming Experience
Beginner
hi,
I can't get it to work :-(
If I only could get an example on how to do this would be really helpful, but I can't find nothing and this is frustrating that I can't even pass a value from a text box to the query of the report......

Any idea how to solve this?

Greetings.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,646
Location
Sydney, Australia
Programming Experience
10+
The idea is that you try what you think is right and, if it doesn't work, you post it here, showing us what you did and telling us exactly what happened when you did it.
 

megatronixs

Member
Joined
Jan 15, 2018
Messages
11
Programming Experience
Beginner
hi, I will close this thread as for now I will not use the reports this way.
I will use the current reports there are in the access database and fire them up via C#

Greetings.
 
Top Bottom