Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to retrieve data from database tables to Crystal Report Viewer. through combobox selected item.

C#
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)
        {

        }
Posted
Updated 26-Sep-11 2:17am
v2

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()+"'";
 
Share this answer
 
Comments
Mehdi Gholam 26-Sep-11 8:23am    
My 5! but Column must be specified and ToString() can be omitted.
P.Salini 26-Sep-11 8:30am    
My 5!
kris444 26-Sep-11 8:48am    
what if selectedValue is object? You can simply says comboBox1.SelectedText right?
Thank u for u r reply.
and after trail
Object reference not set to an instance of an object.

I got this error again.
 
Share this answer
 
Comments
Syed Salman Raza Zaidi 26-Sep-11 9:10am    
Try this con.Open()
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

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900