本文介绍了将值传递给sql查询,该查询将选择数据并生成晶体报告以显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



在我的项目中,我有一个要求,我需要将选定的值从dropdownbox传递给sql查询,然后填充数据集,并将数据集分配给水晶报告查看器生成水晶报告。



这是我的代码:

hello,

In my project i have an requirement where in i need to pass selected value from dropdownbox to sql query, then fill dataset, and assign dataset to crystal report viewer to generate crystal report.

This is my code:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared; 


public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       // InitializeComponent();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection cnn;
        string connectionString = null;
        string sql = null;
       cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["IFBConnectionString"].ToString());
        cnn.Open();
     
        sql = "SELECT * FROM Master$ WHERE ([Name]=@value)";
    SqlCommand SelectCmd = New SqlCommand(sql,Cnn);
        cnn.Parameters.AddWithValue("@value", DropDownList1.Text);
        //cnn.Parameters.Add("@value", SqlDbType.VarChar).Value = DropDownList1.Text;
        SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
        DataSet1 ds = new DataSet1();
        dscmd.Fill(ds, "Master$");
        MessageBox.Show(ds.Tables[1].Rows.Count.ToString());
        cnn.Close();

        CrystalReport objRpt = new CrystalReport();
        objRpt.SetDataSource(ds.Tables[1]);
        crystalReportViewer1.ReportSource = objRpt;
        crystalReportViewer1.Refresh();
    }
}





但我收到以下错误:

错误1 'System.Data.SqlClient.SqlConnection'不包含'Parameters'的定义,也没有扩展方法'Parameters'接受类型'System.Data.SqlClient.SqlConnection'的第一个参数'(你是否缺少using指令)或汇编引用?)



错误2找不到类型或命名空间名称'DataSet1'(您是否缺少using指令或程序集引用?)



错误5找不到类型或命名空间名称'CrystalReport'(您是否缺少using指令或程序集引用?)



请帮帮我。谢谢。



But I am getting following error:
Error1'System.Data.SqlClient.SqlConnection' does not contain a definition for 'Parameters' and no extension method 'Parameters' accepting a first argument of type 'System.Data.SqlClient.SqlConnection' could be found (are you missing a using directive or an assembly reference?)

Error2The type or namespace name 'DataSet1' could not be found (are you missing a using directive or an assembly reference?)

Error5The type or namespace name 'CrystalReport' could not be found (are you missing a using directive or an assembly reference?)

Please help me out.Thanks in advance.

推荐答案






但我收到以下错误:

错误1 'System.Data.SqlClient.SqlConnection'不包含'Parameters'的定义,也没有扩展方法'Parameters'接受类型'System.Data.SqlClient.SqlConnection'的第一个参数'(你是否缺少using指令)或汇编引用?)



错误2找不到类型或命名空间名称'DataSet1'(您是否缺少using指令或程序集引用?)



错误5找不到类型或命名空间名称'CrystalReport'(您是否缺少using指令或程序集引用?)



请帮帮我。谢谢。



But I am getting following error:
Error1'System.Data.SqlClient.SqlConnection' does not contain a definition for 'Parameters' and no extension method 'Parameters' accepting a first argument of type 'System.Data.SqlClient.SqlConnection' could be found (are you missing a using directive or an assembly reference?)

Error2The type or namespace name 'DataSet1' could not be found (are you missing a using directive or an assembly reference?)

Error5The type or namespace name 'CrystalReport' could not be found (are you missing a using directive or an assembly reference?)

Please help me out.Thanks in advance.


SqlConnection cnn = new SqlConnection();
cnn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["IFBConnectionString"].ConnectionString;
cnn.Open();
     
sql = "SELECT * FROM Masters WHERE Name=@value";
SqlCommand SelectCmd = New SqlCommand(sql,Cnn);
SelectCmd.Parameters.AddWithValue("@value", DropDownList1.SelectedValue);
SqlDataAdapter dscmd = new SqlDataAdapter(SelectCmd);
DataSet ds = new DataSet();
dscmd.Fill(ds);
cnn.close();



对于CrystalReport的错误,添加所需的引用和使用语句。



问候..


For CrystalReport's error,add the required reference and using statements.

Regards..


这篇关于将值传递给sql查询,该查询将选择数据并生成晶体报告以显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 16:04