2015-10-20 129 views
0

我試圖在Crystal Reports中創建報告,但是當我將參數分配給報告時,它不顯示選擇查詢的結果。它顯示錶格的所有列,儘管選擇查詢的結果是正確的。這是我的代碼:Crystal Reports,Visual Basic 2010和mysql報告

Dim ulogueado, consulta As String  
    consulta = "select nombre from usuario where usuario = @user" 
    Dim lector1 As MySqlDataReader 
    Dim comando As New MySqlCommand(consulta, conector) 
    comando.Parameters.AddWithValue("@user", usuario_conectado) 
    Try 
     lector1 = comando.ExecuteReader() 
     If lector1.Read Then 
      ulogueado = lector1.GetString(0) 
     End If 
     Dim reporte1 As New CrystalReport3 
     reporte1.SetDataSource(lector1.GetString(0)) 
     reportes1.CrystalReportViewer1.ReportSource = reporte1 
     reportes1.CrystalReportViewer1.RefreshReport() 
    Catch ex As Exception 
    End Try 
    reportes1.Show() 

我幾乎可以肯定問題可能在SetDataSource行,但我不知道我還能做什麼。我很感謝你對這個問題的關注和合作。

+0

你有沒有檢查http://stackoverflow.com/questions/8676448/my-crystal-report-is-not-getting-refreshed-while-passing-parameter – haraman

回答

0

由於documentation說,你需要一個記錄集或數據集傳送到ReportEngine,而你是通過其返回一個字符串

reporte1.SetDataSource(lector1.GetString(0)) 

更改語句使用DataReader應該解決問題的DataReader的場如

reporte1.SetDataSource(lector1) 

我還沒有使用MySqlDataReader作爲數據源,但已經使用DataSet來生成效果很好的報告。

您需要檢查的另一件事是上面我的comment中提到的保存的數據。如果您在您的報告中使用的參數再有其他的方式來值傳遞給這些參數,請參閱how-to-pass-values-to-two-different-parameters

編輯:使用DataSet中

Dim ulogueado, consulta As String  
consulta = "select nombre from usuario where usuario = @user" 
Dim comando As New MySqlCommand(consulta, conector) 
comando.Parameters.AddWithValue("@user", usuario_conectado) 
Dim da As MySqlDataAdapter = New MySqlDataAdapter() 
Dim ds As DataSet = New DataSet() 
da.SelectCommand = comando 
Try 
    da.Fill(ds, "usuario") 
    Dim reporte1 As New CrystalReport3 
    reporte1.SetDataSource(ds) 
    reportes1.CrystalReportViewer1.ReportSource = reporte1 
    reportes1.CrystalReportViewer1.RefreshReport() 
Catch ex As Exception 
    'Check for errors here 
    'MsgBox(ex.Message) 
End Try 
reportes1.Show() 

有關DataAdapters更多信息,DataSet中你可以看一下這SO post connecting-to-a-mysql-db-with-c-sharp-need-some-with-datasets

+0

嗨,謝謝你的答案。我已經嘗試過 reporte1.SetDataSource(lector1) 但出現此錯誤: 錯誤重載解析,因爲沒有訪問'SetDataSource'函數更具體的這些參數 請問我可以如何使用DataSet或哪裏可以找到這個。非常感謝你。 –

+0

檢查DataSet的更新 – haraman

相關問題