Enter button

Daz66

Active member
Joined
May 17, 2020
Messages
40
Programming Experience
Beginner
Hello again.

Any chance one of you guys could point me in the right direction here please. I have an SQL database and returning results via search buttons to dataGridView, There are customer 'id' and 'name' search options on my form.

The id search button works fine with the keydown enter button event, but the customer search button only returns one result then stops, unless i add an if statement with a message box to loop the code. The only differences are int/string variables.

So it works, but i'm assuming i'm getting around a code error with the second message box on the if statement on the customer search?

I've researched this for a week or so, any help appreciated. Looking for a nudge not code.

I'm approx 8 weeks into my learning now.



C#:
//!=======================
//!SEARCH BY CUSTOMER NAME
//!=======================
private void btnSearch_Click(object sender, EventArgs e)
{
    if (String.IsNullOrWhiteSpace(txtName.Text))
    {
        MessageBox.Show("Please enter a customer name");

    }
    else
    {
        string conString;
        conString = Properties.Settings.Default.jobsConnectionString;


        string customerName = (txtName.Text);

        SqlConnection con = new SqlConnection(conString);

        con.Open();

        SqlCommand cmd = new SqlCommand("GetCustomerByName", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@Getcustomer_name", customerName));

        SqlDataAdapter adaptor = new SqlDataAdapter(cmd);

        DataTable dataTable = new DataTable();

        adaptor.Fill(dataTable);

        dataGridView1.DataSource = binder;

        binder.DataSource = dataTable;

        con.Close();


        //return;
        MessageBox.Show("Here are your results");
        //btnSearch.Enabled = true;
    }
}

//!=====================================
//!ENTER BUTTON FOR CUSTOMER NAME SEARCH
//!=====================================
private void txtName_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        btnSearch_Click(this, new EventArgs());

        txtName.Clear();
    }
}

//!===================
//!SEARCH BY COLUMN ID
//!===================
private void btnIdSearch_Click(object sender, EventArgs e)
{
    if (string.IsNullOrWhiteSpace(txtId.Text))
    {
        MessageBox.Show("Please enter id");
    }
    else

    {
        string conString;
        conString = Properties.Settings.Default.jobsConnectionString;

        int customeridValue = int.Parse(txtId.Text);


        SqlConnection con = new SqlConnection(conString);

        con.Open();

        SqlCommand cmd = new SqlCommand("GetCustomerById", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@Getid", customeridValue));

        SqlDataAdapter adaptor = new SqlDataAdapter(cmd);

        DataTable dataTable = new DataTable();

        adaptor.Fill(dataTable);

        dataGridView1.DataSource = binder;

        binder.DataSource = dataTable;

        con.Close();
    }
}

//!===================================
//!ENTER BUTTON FOR CUSTOMER ID SEARCH
//!===================================
private void txtId_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        btnIdSearch_Click(this, new EventArgs());
        e.SuppressKeyPress = true;//#Stops system ping sound on execution.
        txtId.Clear();
    }
}
 
Firstly, never do this:
C#:
btnSearch_Click(this, new EventArgs());
You don't call event handlers directly. If you want a Button to raise an Click event, call its PerformClick method. Better still, put your code in its own method and then call it from the Button's Click event handler and anywhere else you need to execute it.
 
Firstly, never do this:
C#:
btnSearch_Click(this, new EventArgs());
You don't call event handlers directly. If you want a Button to raise an Click event, call its PerformClick method. Better still, put your code in its own method and then call it from the Button's Click event handler and anywhere else you need to execute it.

Thank you for the reply, I shall go and try to learn what you have suggested....thanks again.
 
Back
Top Bottom