所有人。我有一個簡單的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;
}
當您運行此操作時,報告查看器是否顯示報告或某些錯誤文本? – SimonPJ
你不解釋你有什麼問題,但你的代碼有問題。您正在使用字符串連接來創建一個不包含任何參數的SQL字符串。如果文本框包含任何奇怪的東西,你的查詢將失敗。你的'WHERE'子句應該是'where id_doc = @ id_doc'。 –
根據您對連接字符串下方的答案的評論錯誤或您的sql服務器配置錯誤。 –