Need to be able to load different reports into the same report viewer, based on the selection of a combobox value How do i do this?

26 Views Asked by At

Actually, what I need to do is select the views in my sql database one by one from the combobox and transfer them to reportviewer, or can it also be done by writing a sql query from the dataset in visual studio rather than from the views in the database.

 private void LoadViewsIntoComboBox()
 {
     using (SqlConnection connection = new SqlConnection(connectionString))
     {
         try
         {
             connection.Open();
             string query = "SELECT name FROM sys.views";
             SqlCommand command = new SqlCommand(query, connection);
             SqlDataReader reader = command.ExecuteReader();

             while (reader.Read())
             {
                 comboBox1.Items.Add(reader["name"].ToString());
             }

             reader.Close();
         }
         catch (Exception ex)
         {
             MessageBox.Show("Error while loading views: " + ex.Message);
         }
         finally
         {
             connection.Close();
         }
     }
 }
 private void LoadReport(string viewName)
 {
     using (SqlConnection connection = new SqlConnection(connectionString))
     {
         try
         {
             connection.Open();
             string query = $"SELECT * FROM {viewName}";
             SqlDataAdapter da = new SqlDataAdapter(query, connection);
             System.Data.DataTable dt = new System.Data.DataTable();
             da.Fill(dt);

             reportViewer1.LocalReport.DataSources.Clear();

             ReportDataSource reportDataSource = new ReportDataSource("aracTakipDataSet", dt);
             reportViewer1.LocalReport.DataSources.Add(reportDataSource);

             reportViewer1.LocalReport.ReportPath = @"\aracTakip.Report1.rdlc";
             reportViewer1.RefreshReport();
         }
         catch (Exception ex)
         {
             MessageBox.Show("Error while loading report: " + ex.Message);
         }
         finally
         {
             connection.Close();
         }
     }
 }
private void raporlama_Load(object sender, EventArgs e)
{
    this.dataTable1TableAdapter.Fill(this.dataSet1.DataTable1);
    LoadViewsIntoComboBox();
    this.reportViewer1.RefreshReport();
}

I tried both adding the view to the dataset and adding the related tables, and I tried setting up a query between it and the tableadapter, but I couldn't Decipher it.

0

There are 0 best solutions below