Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

In my 'Table1' SQL table I have 'ProjectCode' which is taken from Master Table. I am attaching 'Table1' to gridview. In that table I am displaying 'ProjectName' instead of 'ProjectCode'. But on Project Name I cannot search the records.
Posted
Updated 22-Sep-13 21:57pm
v2
Comments
Neema Derakhshan 23-Sep-13 3:34am    
hi,show some code please !

Maintain projectCode and projectName also in the table and just display the projectName in Grid and use the respective value of projectCode to search.


[Edit member="Tadit"]
Corrected formatting issues.
[/Edit]
 
Share this answer
 
v2
Try this. Hope it will help you.

txtName is the search TextBox and ddlSearch is DropDownList for filtering
C#
private void txtName_TextChanged(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(Connection.getConnectionString());
    if (ddlSearch.Text == "Enter Name")
    {
        con.Open();
        SqlDataAdapter adap = new SqlDataAdapter("select * from Item where Item_name like '" + txtName.Text + "%'", con);
        SqlCommandBuilder build = new SqlCommandBuilder(adap);
        DataSet ds = new DataSet();
        adap.Fill(ds, "Items");
        dataGridView1.DataSource = ds.Tables["Items"];
        con.Close();
    }
    if (ddlSearch.Text == "Enter Item Code")
    {
        con.Open();
        SqlDataAdapter adap = new SqlDataAdapter("select * from Item where Item_code like '" + txtName.Text + "%'", con);
        SqlCommandBuilder build = new SqlCommandBuilder(adap);
        DataSet ds = new DataSet();
        adap.Fill(ds, "Items");
        dataGridView1.DataSource = ds.Tables["Items"];
        con.Close();
    }
}



[Edit member="Tadit"]
Corrected formatting and grammatical issues.
[/Edit]
 
Share this answer
 
v2
Comments
Prateek gsharma 11-Apr-18 14:05pm    
sir,
if my query is like below then it is not giving any results

"select * from Item where Item_code like '" + txtName.Text + "%' and date between '"+todatetimepicker.value.tostring("dd-MM-yyyy")+"' and '"+fromdatetimepicker.value.tostring("dd-MM-yyyy")+"'";

finally datagridview is not giving any result.
what to do in this case?
Prateek gsharma 11-Apr-18 15:08pm    
Error Message : System.Data.OleDb.OleDbException: 'No value given for one or more required parameters.'

My Code :
private void textBox1_TextChanged(object sender, EventArgs e)
{
//Format(dateTimePicker1.Value, "mm/dd/yyyy");
if(comboBox1.Text == "ID")
{
OleDbDataAdapter da = new OleDbDataAdapter("select * from Spldetails where ID like '%" + textBox1.Text + "%' and Date >= ('"+(dateTimePicker1.Value.ToString("mm/dd/yyyy"))+ "') and Date <= ('" + (dateTimePicker2.Value.ToString("mm/dd/yyyy")) + "')", con);
OleDbCommandBuilder build = new OleDbCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds, "Spldetails");
dataGridView1.DataSource = ds.Tables["Spldetails"];
}
if (comboBox1.Text == "Empid")
{
OleDbDataAdapter da = new OleDbDataAdapter("select * from Spldetails where EmpId like '%" + textBox1.Text + "%' and Date >= ('" + (dateTimePicker1.Value.ToString("mm/dd/yyyy")) + "') and Date <= ('" + (dateTimePicker2.Value.ToString("mm/dd/yyyy")) + "'))", con);
OleDbCommandBuilder build = new OleDbCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds, "Spldetails");
dataGridView1.DataSource = ds.Tables["Spldetails"];
}
}

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