Display ONLY the first record of datagrid from textbox on click

Israel

Active member
Joined
Jan 10, 2020
Messages
26
Programming Experience
Beginner
Hi,

I wrote these codes to display the LAST row ONLY. Then how can I display ONLY the first row from textbox one click.
C#:
private void button_Click(object sender, EventArgs e)

        {
             string commandString = "select top 1 * from Test where name='" + txtNumber.Text ";  
            OleDbCommand sqlCmd = new OleDbCommand(commandString, sqlCon);
            OleDbDataReader read = sqlCmd.ExecuteReader();

            if (read.HasRows)
            {
                while (read.Read())
                {
                    txtNumber.Text = read["number"].ToString();
                }
            }
            else
            {
                MessageBox.Show("Name " + txtName.Text + " not found");
            }
            read.Close();
            sqlCon.Close();
 
Last edited:
I wrote these codes to display the LAST row ONLY. Then how can I display ONLY the first row from textbox one click.
Huh? You are already doing what you are asking for. In your code, you used SELECT TOP 1. That selects the FIRST row.

If you want the LAST row, then change your query to sort in reverse order.
 
Back
Top Bottom