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..

Recommended Answers

All 4 Replies

Hi,
I guess you want to do the sql query on every character inserted, but...
You missed the point of the main issue you are having. Tell me, which characters do you want to be inserted into textBox (or which not)?
As far as I can see from your code there are:
- lettes
- numbers
- why do you need "IsControl" characters? which one do you expect?
- whitespaces

- What about backspace for erasing?

Mitja

Yes, I want to execute the sql query for every character inputted. I included IsControl because it contains backspace also. What's missing?

after reading a tutorial about textbox properties and events, i have fixed it! I placed the code in textchanged event and it works the way I wanted to!

What are you doing wrong is that you set for parameter the Text property of textBox.
The Text property in KeyPress event is "one step behind" so you will never have in text propety thow whole correct value.

You have to use a little trick, to help with using e.KeyChar.
Take a look at this code:

string myText;
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsLetter(e.KeyChar) || char.IsNumber(e.KeyChar) || char.IsControl(e.KeyChar) || char.IsWhiteSpace(e.KeyChar))
            {
                e.Handled = false;  //just to enable other controls to receive keyboard events!
             
                string charVal = e.KeyChar.ToString();
                if (e.KeyChar == '\b')
                    myText = myText.Substring(0, myText.Length - 1);
                else
                    myText += charVal.ToString();

                //example to show what we are doing:
                label1.Text = myText; //you can erase this line - it was my test!!

                //but you have to use "myText" value as your parameter! It will work now!
            }
        }

Hope it helps,
Mitja

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.