I'm having difficulty to implement a select query for my database using datagridview to bind to and textbox for searching a particular value in my table.
I have this stored procedure
CREATE PROCEDURE usp_searchvisitor
@keyword nvarchar(20)
AS
SELECT * FROM tblVisitor WHERE lastName = @keyword OR firstName = @keyword OR personalID = @keyword OR vehicleNo = @keyword OR company = @keyword OR represent = @keyword OR "address" = @keyword
It believed it works fine, while this is my code for textbox keypress event
private void searchtb_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsLetter(e.KeyChar) || char.IsNumber(e.KeyChar) || char.IsControl(e.KeyChar) || char.IsWhiteSpace(e.KeyChar))
{
cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "usp_searchvisitor";
cmd.Connection = con;
cmd.Parameters.AddWithValue("@keyword", searchtb.Text);
DataSet ds = new DataSet();
da.SelectCommand = cmd;
da.Fill(ds, "tblVisitor");
registrydg.DataSource = ds;
registrydg.DataMember = "tblVisitor";
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
It works whenever I type a certain value on the textbox for the first time, but second time and so on it doesn't! It behaves so weird that I can't even understand. I have a column that contains 'mark' as the 'firstname', and when I input mark there would be no value, but if I type markk it works. It's really confusing..