2015-04-22 191 views
0

所有人。我有一個簡單的WebApp,帶有一個報告查看器,可以顯示我的查詢結果。我寫了我的代碼,應該通過我的select查詢獲取數據。然後,我寫了一個方法,在報告查看器上顯示我的記錄。我究竟做錯了什麼?在ASP.NET中顯示報告查看器

protected void Button1_Click(object sender, EventArgs e) 
{ 
    showReport(); 
} 

protected void showReport() 
{ 
    rptViewer.Reset(); 
    DataTable dt = GetData(Convert.ToInt64(TextBox1.Text)); 
    ReportDataSource rds = new ReportDataSource("DataSet1", dt); 
    rptViewer.LocalReport.DataSources.Add(rds); 
    rptViewer.LocalReport.ReportPath = "Report1.rdlc"; 
    ReportParameter rptParam = new ReportParameter("id_doc", TextBox1.Text); 
    rptViewer.LocalReport.Refresh(); 
} 

private DataTable GetData(Int64 id_doc) 
{ 
    DataTable dt = new DataTable(); 
    string connStr = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["logindbConnectionString"].ConnectionString; 
    try 
    { 
     using (var conn = new SqlConnection(connStr)) 
     {   
      string sSQL = "select * from doc_details where id_doc="; 
      SqlCommand cmd = new SqlCommand(sSQL+ TextBox1.Text, conn); 
      cmd.CommandType = CommandType.Text; 
      cmd.Parameters.Add("@id_doc", SqlDbType.BigInt).Value = TextBox1.Text; 
      SqlDataAdapter adp = new SqlDataAdapter(cmd); 
      Console.WriteLine(dt); 
      adp.Fill(dt); 
     } 
    } 
    catch (Exception ex) 
    { 
     lblError.Text = ex.Message; 
    } 
    return dt; 
} 
+0

當您運行此操作時,報告查看器是否顯示報告或某些錯誤文本? – SimonPJ

+0

你不解釋你有什麼問題,但你的代碼有問題。您正在使用字符串連接來創建一個不包含任何參數的SQL字符串。如果文本框包含任何奇怪的東西,你的查詢將失敗。你的'WHERE'子句應該是'where id_doc = @ id_doc'。 –

+0

根據您對連接字符串下方的答案的評論錯誤或您的sql服務器配置錯誤。 –

回答

0

試試這個

查詢必須有一個參數指定字符串@id_doc

串sSQL = 「SELECT * FROM doc_details其中id_doc = @ id_doc」;

SqlCommand cmd = new SqlCommand(sSQL,conn);

command.Parameters.Add(new SqlParameter(「@ id_doc」,TextBox1.Text));

+0

@SimonPJ應用程序永遠不會到達showReport()方法,並在等待很長時間後顯示連接問題:命名管道提供程序,錯誤:40 - 無法打開到SQL Server的連接。我不知道如何解決它... –

+0

檢查你的連接字符串 – MYATALIT

+0

這裏是我的連接字符串:任何線索? –

相關問題