Click here to Skip to main content
16,019,263 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I DESIGN A small application as follows;

Project ID Combobox1
Project Name Textbox1
Rate Textbox2
Unit Textbox3
Amt Textbox4

all the datas are stored in the database.

I have one textbox and one button(button name Project Wise Id)in textbox i type the letter R Only the project id start with R want to display the output as follows;

Project ID Project Name Rate Unit Amt
R001 Embedded 30 16 480
R002 Embedded 40 16 640
1120

Project ID Project Name Rate Unit Amt
P001 JAva 15 5 75
P002 .Net 20 10 200
R001 Embedded 30 16 480
R002 Embedded 40 16 640
Total 1395

C#
private void button3_Click(object sender, EventArgs e)
        {
            con = new SqlConnection("Server=(local);initial catalog=master;Trusted_Connection=True");
            con.Open();
            string str = "select * from Project where PojectID like = '" + cb_id.SelectedItem + "'";
            SqlCommand com = new SqlCommand(str, con);
            SqlDataAdapter sqlda = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            sqlda.Fill(ds, "Project");
            con.Close();

            ReportDocument r1 = new ReportDocument();
            //r1.Load("Full Path of Your Report\Employee.rpt");
            //string path = Server.MapPath("Reports/Service Bill.rpt");
            r1.Load("Reports/ProjectReport.rpt");
            r1.SetDataSource(ds);
            crystalReportViewer1.ReportSource = r1;
            crystalReportViewer1.RefreshReport();
 }

Here this query as follows: "select * from Project where PojectID like = '" + cb_id.SelectedItem + "'";

From the above please correct the query i have some doubt.

[Edit]Code block added[/Edit]
Posted
Updated 21-Oct-12 3:54am
v2

Sql LIKE requires wildcard characters to be effective. '%' is the Sql equivalent of '*' in a normal search, meaning "match any number of any character"
Try:
C#
string str = "select * from Project where PojectID like = '" + cb_id.SelectedItem + "%'";
to match "anything starting with" the value in the ComboBox.
 
Share this answer
 
C#
private void button3_Click(object sender, EventArgs e)
        {
            con = new SqlConnection("Server=(local);initial catalog=master;Trusted_Connection=True");
            con.Open();
            string str = "select * from Project where PojectID like = '" + cb_id.SelectedItem + "%'";
            SqlCommand com = new SqlCommand(str, con);
            SqlDataAdapter sqlda = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            sqlda.Fill(ds, "Project");
            con.Close();
 
            ReportDocument r1 = new ReportDocument();
            //r1.Load("Full Path of Your Report\Employee.rpt");
            //string path = Server.MapPath("Reports/Service Bill.rpt");
            r1.Load("Reports/ProjectReport.rpt");
            r1.SetDataSource(ds);
            crystalReportViewer1.ReportSource = r1;
            crystalReportViewer1.RefreshReport();
 }
 
Share this answer
 
Please use either LIKE or = operator in your SQL statement.
Try with:
C#
string str = "select * from Project where PojectID like '" + cb_id.SelectedItem + "%'";

Happy coding!
 
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