Question

I have a 4 text boxes on a win form. [First Name, Last Name, Job, Description.]

I have one table.

I have the dataset and a data table configured and I can navigate the records.

I want to know how can I search based on first name, obtain the data from dataset/table and display the info based on what is in the text box.

how do I do this such as, obtain the row, "inc" where the

            txtFirstName = ds.Tables[0].Column[1]

Then I can:

            DataRow row = ds.Tables[0].Rows[inc];

            txtFirstName.Text = row[1];
            txtSurname.Text = row[2];
            txtJobTitle.Text = row[3];
            txtDepartment.Text = row[4];

sorry, found the solution.

First I created a search method which returned the row...

private int first_name_search(string fname) {

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if ((string)ds.Tables[0].Rows[i]["first_name"] == fname)
                {
                    send = i;
                    //result stuff
                    break;
                }
            }
           // return result;
            return send;
        }

I used this method in the Search button click method and displayed the data...

Was it helpful?

Solution

In a function that return a string[]:

        string[number_of_infos] infos = null;
        connection = new SqlConnection(connectionString);
        connection.Open();
        cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "SELECT * FROM your_table WHERE first_name = " + your_first_name;
        cmd.Connection = connection;
        rdr = cmd.ExecuteReader();
        if(rdr.Read())
        {
            infos[0] = rdr["Surname"].ToString();
            infos[1] = rdr["JobTitle"].ToString();
            infos[2] = rdr["Department"].ToString();
        }
        cmd.Dispose();
        connection.Close();
        return infos;

Direct in your form:

        connection = new SqlConnection(connectionString);
        connection.Open();
        cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "SELECT * FROM your_table WHERE first_name = " + your_first_name;
        cmd.Connection = connection;
        rdr = cmd.ExecuteReader();
        if(rdr.Read())
        {
            txtSurname.Text = rdr["Surname"].ToString();
            txtJobTitle.Text = rdr["JobTitle"].ToString();
            txtDepartment.Text = rdr["Department"].ToString();
        }
        cmd.Dispose();
        connection.Close();
        return infos;

Else if you want to get the row infos:

foreach(DataGridViewRow row in your_DGV.Rows)
    if(row["FirstName"].Value.ToString() == txtFirstName.Text)
    {
        txtSurname.Text = row["Surname"].Value.ToString();
        txtJobTitle.Text = row["JobTitle"].Value.ToString();
        txtDepartment.Text = row["Department"].Value.ToString();
        break;
    }

OTHER TIPS

FYI, you can use LINQ extensions to do this:

var tbl = ds.Tables[0].AsEnumerable();
return tbl.Where(p => p["first_name"] == fname).FirstOrDefault();

Edit: To select all matching rows from the DataTable:

var results = from row in tbl.AsEnumerable()
              where row["first_name"] == fname
              select row;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top