组合框所选索引已更改 [英] Combobox Selected Index changed

查看:76
本文介绍了组合框所选索引已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从数据库表检索数据到Crystal Report Viewer.通过组合框选择的项目.

How to retrieve data from database tables to Crystal Report Viewer. through combobox selected item.

public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=CMS;Data Source=KOUSHIK");
        SqlDataAdapter da;
        SqlCommand cmd;
        DateTime dt = System.DateTime.Now;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
                cmd = new SqlCommand();
                da = new SqlDataAdapter();
                cmd.CommandText = "select Accessory_No,Book_Title,Book_Price,Book_Author,Book_Publisher,Date_of_Purchase,Available,Issued,Remaining from BookDetails1";
                da.SelectCommand = cmd;
                cmd.Connection = con;
                da.SelectCommand = cmd;
                DataSet ds = new DataSet();
                da.Fill(ds);
                ReportDocument rptc = new ReportDocument();
                rptc.Load("D:\\CMS\\BookDetailReport\\BookDetailReport\\BookDetailedReport2.rpt");
                rptc.SetDataSource(ds);
                crystalReportViewer1.ReportSource = rptc;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

推荐答案

尝试此查询
Try this query
cmd.CommandText = "select Accessory_No,Book_Title,Book_Price,Book_Author,Book_Publisher,Date_of_Purchase,Available,Issued,Remaining from BookDetails1 where Column=''"+comboBox1.selectedValue.toString()+"''";


感谢您的回复.
和之后的足迹
对象引用未设置为对象的实例.

我再次遇到此错误.
Thank u for u r reply.
and after trail
Object reference not set to an instance of an object.

I got this error again.


原因可能是执行查询时在组合框中没有选择任何值,请在cmd.CommandText之前添加以下内容,看看是否帮助

The reason could be there is no value selected in the combo box at the time you execute the query, add the following before the cmd.CommandText and see whether it helps

if (comboBox1.SelectedValue==null)
             {
                 MessageBox.Show("Please select a value");
                 comboBox1.Focus();
                 return;
             }


这篇关于组合框所选索引已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆