I am making a search bar for my windows form application. I have datagridview, textbox and a combobox...
I have this code in my class :
Classes.dbConnect _db = new Classes.dbConnect();
public void SearchPosition()
{
if (_search.Equals("POSITION"))
{
_db._conn.Open();
string com = "SELECT * FROM tbl_Candidates" + " WHERE ( Position LIKE @pos )";
_db._com = new SqlCommand(com, _db._conn);
_db._com.Parameters.AddWithValue("@pos", "%" + _position + "%");
try
{
_db._da = new SqlDataAdapter();
_db._da.SelectCommand = _db._com;
DataTable _dt = new DataTable();
_db._da.Fill(_dt);
BindingSource bs = new BindingSource();
bs.DataSource = _dt;
_dg.DataSource = bs;
_db._da.Update(_dt);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
_db._conn.Close();
}
}
}
Then I have my textbox = TxtSearch.Text
private void txtSearch_TextChanged(object sender, EventArgs e)
{
Classes.Admin.Candidates.position = txtSearch.Text;
cand.SearchPosition();
}
And i have my combobox = cboCategory; Item inside is "Position"
private void cboCategory_SelectedIndexChanged(object sender, EventArgs e)
{
Classes.Admin.Candidates.search = cboCategory.Text;
}
When I start typing in my TxtSearch.Text. Every letter searches the database with the same value as the TxtSearch.Text.
The DataGridView only shows the value same as the value in the textbox. But when the datagridview is empty already, i keep getting the error.
"index was out of range. Must be non-negative and less than the size of the collection. Parameter name: Index"
Aucun commentaire:
Enregistrer un commentaire