Click here to Skip to main content
15,072,422 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.

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()

        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();
                ReportDocument rptc = new ReportDocument();
                crystalReportViewer1.ReportSource = rptc;

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

Updated 26-Sep-11 2:17am

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()+"'";
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.
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

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

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