Asynchronous
Asynchronous

Reputation: 3977

Select from dataset where data in dataset matches user input

I am trying to query or match user input against a dataset using a DataTable:

I am populating the dataset from a stored procedure which selects only a single column from a single table: Example: UserID Column. **I am not selecting the entire content of the table.*

public static DataSet LoadProfile()
{
    SqlCommand cmdSQL = new SqlCommand("usp_LoadProfile", ConnectDatabase);
    cmdSQL.CommandType = CommandType.StoredProcedure;

    SqlDataAdapter daSQL = new SqlDataAdapter(cmdSQL);
    DataSet ds = new DataSet();
    daSQL.Fill(ds);

    try
    {
        ConnectDatabase.Open();
        cmdSQL.ExecuteNonQuery();
    }
    catch(Exception)
    {
        StatusMsg = ex.Message;
    }
    finally
    {
        ConnectDatabase.Close();
        cmdSQL.Parameters.Clear();
        cmdSQL.Dispose();
    }
    return ds;
}

I have the following method called in the form load event: I need to populate the dataset on from load.

public static DataTable LoadData()
{
    DataSet dsView = new DataSet();
    dsView = LoadProfile();
    DataTable tblExample = dsView.Tables["Example"];

    return tblExample;
}

Finally what I would like to do is match the user entry from the DataTable.

I have this in button event:

DataRow[] results;
results = LoadData().Select(txtExample.Text);

Beyond this point, I could use a for loop but there is only one record for each person.

I am trying to match the user entry with the dataset via the datatable.

Upvotes: 3

Views: 17665

Answers (2)

anagha
anagha

Reputation: 63

Use the following simple query on dataset:

DataRow[] dRow = dataSetName.Tables[0].Select("fieldToMatch = '" + userInput.ToString() + "' ");

Upvotes: 2

Steve
Steve

Reputation: 216348

The last line should be

DataRow[] results;
results = LoadData().Select("UserID = '" + txtExample.Text +"'");

Supposing that UserID is a field of text type. If instead is of numeric type then remove the quotes

results = LoadData().Select("UserID = " + txtExample.Text);

However I should point that the code in LoadProfile following the daSQL.Fill(ds); call is not needed and you can remove it (just return the DataSet though)

Upvotes: 4

Related Questions