Hi All,

I am creating an application where in I want to display the rows in DataGridView based on the values entered by the user in the textbox.

For Eg.
If the User enters the BookName in the textbox all the details regarding that book should be displayed in a DataGridView.

I have used the following codings:

SqlConnection objSqlConnection = new SqlConnection();
    string connectionStringSettings = "Data Source =.; Initial Catalog = LibrarySystemManagement;Integrated Security = SSPI";
    private void btnSearch_Click(object sender, EventArgs e)
 try
            {
                objSqlConnection.ConnectionString = connectionStringSettings;
                objSqlConnection.Open();

                if ((txtBookName.Text != "") || (txtCategory.Text != ""))
                {

                    SqlDataAdapter objSqlDataAdapter = new SqlDataAdapter("select * from LIBRARYBOOKDETAILS where Title = '"+txtTitle.Text+"'", objSqlConnection);
                    SqlCommandBuilder objSqlCommandBuilder = new SqlCommandBuilder(objSqlDataAdapter);
                    DataTable objDataTable = new DataTable();
                    objSqlDataAdapter.Fill(objDataTable);
                    BindingSource objBindingSource = new BindingSource();
                    objBindingSource.DataSource = objDataTable;
                    dataGridView1.DataSource = objBindingSource;
                    objSqlDataAdapter.Update(objDataTable);
                    objSqlConnection.Close();
                }
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message + e1.Source);
            }
          }

But the above code displays all the rows entered in the table. I mean the rows are not retrieved based on the condition.

Can anyone help me out in finding the correct set of code snippet used to retrieve data based on the condition?

Please Help me out.

Thanks in advance.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Code tag please!!
Try that please

SqlConnection objSqlConnection = new SqlConnection();
string connectionStringSettings = "Data Source =.; Initial Catalog = LibrarySystemManagement;Integrated Security = SSPI";
private void btnSearch_Click(object sender, EventArgs e)
try
{
objSqlConnection.ConnectionString = connectionStringSettings;
objSqlConnection.Open();

if ((txtBookName.Text != "") || (txtCategory.Text != ""))
{

SqlCommand objSqlCommand = new SqlCommand("select * from LIBRARYBOOKDETAILS where Title = '"+txtTitle.Text+"'", objSqlConnection);

dataGridView1.DataSource = objSqlCommand.ExecuteReader();
//objSqlDataAdapter.Update(objDataTable);
dataGridView1.DataBind();
objSqlConnection.Close();
}
}
catch (Exception e1)
{
MessageBox.Show(e1.Message + e1.Source);
}
}

Little modification with Ramy's code:
It's window control.

....
  // dataGridView1.DataBind();
   ...

Little modification with Ramy's code:
It's window control.

....
  // dataGridView1.DataBind();
   ...

Thanks for your prompt reply.

But When I include the statement:
datagridview1.dataBind();

I am getting the following error:

'System.Windows.Forms.DataGridView' does not contain a definition for 'dataBind' and no extension method 'dataBind' accepting a first argument of type 'System.Windows.Forms.DataGridView' could be found (are you missing a using directive or an assembly reference?) D:\LibraryManagementSystem\LibraryManagementSystem\SearchOrIssueBooks.cs 72 39 LibraryManagementSystem

Can you tell me how should I rectify this error?

What should I do to overcome this error?

Please help me out.

Comment datagridview1.dataBind(); or remove it.

Thanks for your reply.

I have removed it but still I m not getting the desired output.

What should I do?

Can anyone help me out please?

Thanks in advance!!!

Is that syntax error? Please post your complete code

Thanks for your reply!

This is my complete code:

SqlConnection objSqlConnection = new SqlConnection();
    string connectionStringSettings = "Data Source =.; Initial Catalog = LibrarySystemManagement;Integrated Security = SSPI";
private void btnSearch_Click(object sender, EventArgs e)
    {
        if ((txtBookName.Text == "") && (txtCategory.Text == ""))
        {
            MessageBox.Show("Please Enter the Search Criteria", "Error Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }
        else
            try
            {
                objSqlConnection.ConnectionString = connectionStringSettings;
                objSqlConnection.Open();

                if ((txtBookName.Text != "") || (txtCategory.Text != ""))
                {
                   SqlDataAdapter objSqlDataAdapter = new SqlDataAdapter("select * from LIBRARYBOOKDETAILS", objSqlConnection);
                    SqlCommandBuilder objSqlCommandBuilder = new SqlCommandBuilder(objSqlDataAdapter);
                    DataTable objDataTable = new DataTable();
                    objSqlDataAdapter.Fill(objDataTable);
                    BindingSource objBindingSource = new BindingSource();
                    objBindingSource.DataSource = objDataTable;
                    dataGridView1.DataSource = objBindingSource;
                    objSqlDataAdapter.Update(objDataTable);                }
                    catch (Exception e1)
                    {
                        MessageBox.Show(e1.Message + e1.Source);
                    }
                    objSqlConnection.Close();
                }
            }

Please tell me what is the error?

How can I get the desired result?

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Wrap source using code tags - see # icon at toolbar.

private void btnSearch_Click(object sender, EventArgs e)
{
   SqlDataAdapter objSqlDataAdapter = new SqlDataAdapter("select *  from LIBRARYBOOKDETAILS", objSqlConnection);
   
    DataTable objDataTable = new DataTable();
    objSqlDataAdapter.Fill(objDataTable);
 
   dataGridView1.DataSource = objDataTable;
}

Wrap source using code tags - see # icon at toolbar.

private void btnSearch_Click(object sender, EventArgs e)
{
   SqlDataAdapter objSqlDataAdapter = new SqlDataAdapter("select *  from LIBRARYBOOKDETAILS", objSqlConnection);
   
    DataTable objDataTable = new DataTable();
    objSqlDataAdapter.Fill(objDataTable);
 
   dataGridView1.DataSource = objDataTable;
}

Thanks for your prompt reply.

I have just modified the code in this way and I am getting the desired output. Thanks for all your help..

I have just modified the code in this way:

SqlDataAdapter objSqlDataAdapter = new SqlDataAdapter("select * from LIBRARYBOOKDETAILS where Title like '" + txtBookName.Text +"'", objSqlConnection);
DataTable objDataTable = new DataTable();
objSqlDataAdapter.Fill(objDataTable);
dataGridView1.DataSource = objDataTable;

Thanks for your support.

This question has already been answered. Start a new discussion instead.