1

I am making progress on this database access stuff... Here's what I need to do: I have an existing DataSet(and therefore a DataTable) that is defined in the class namespace, and I am trying to make a function that allows the user to pick from a list of items, and use the SelectedItem.Value(which contains a unique Primary Key from the Database) to show more information about the item that is selected. I believe I need to search through the DataSet and read it into something like a DataRow so that I can display the values from that(there is a way to do this based on column name, correct?)

I just can't figure out how to say "copy/select the row whose primary key "partnumber" is equal to SelectedItem.Value"

Kyle Preiksa
  • 516
  • 2
  • 7
  • 23
  • Have you looked at 'DataTable.Select()': http://msdn.microsoft.com/en-us/library/h71xaeh0.aspx – CodingGorilla Jun 08 '12 at 18:37
  • I have `DataRow[] myRows = queryTable.Select("productsid='" + selectionList.SelectedItem.Value + "'");` but it returns an error saying could not find column [prodcutsid] even though that is clearly one of the column names(unless it is case sensitive in this case) – Kyle Preiksa Jun 08 '12 at 18:45

1 Answers1

2

There are three common methods

  1. Use DataRowCollection.Find can only be used if a column has been defined as a primary key

    var row = table.Rows.Find(SelectedItem.Value)
    
  2. Use DataTable.Select(String) which uses a filter like a string

    var rows = table.Select("IdFieldSelectedItem.Value = " + SelectedItem.Value);
    var row = rows[0];
    
  3. Use Linq to Dataset

    var row =  (from t in table.AsEnumerable()
                where t.Field<int>("Id") == SelectedItem.Value
                select t).First();
    
Conrad Frix
  • 51,984
  • 12
  • 96
  • 155
  • @Conrad Frix Ok, so I was using the Select method properly. However, it seems like my DataSet contains no data. Any ideas why? I have `public partial class Default : System.Web.UI.Page { DataSet queryData = new DataSet(); DataTable queryTable = new DataTable();` Then in a button event handler, I query the database(actually a web service) like: `protected void updateTable(oject sender, EventArgs e) { queryData = pws.Query...//web service method queryTable = queryData.Tables["parts"] }` Then this function `protected void updateData(object sender, EventArgs e) { // as shown in your answer }` – Kyle Preiksa Jun 08 '12 at 19:20
  • When are you calling `.Select()` is that also from the button handler that filled the dataset? – Conrad Frix Jun 08 '12 at 19:40
  • No, the .Select() is called from the "selectedIndexChanged" handler for a drop down list. I am displaying a table, then want the user to select from a dropdown list after the table is up. Upon selecting an item from the drop down, I want additional information to appear. – Kyle Preiksa Jun 08 '12 at 19:50
  • Ok so every time you have a post back the `queryData` and `queryTable` get reinitialized. In order to keep the DataSet alive you're going to need to do something. Typically people use `Session` for that. e.g Add `Session["QueryTable"] = queryTable;` to your button handler and `if(IsPostBack) { queryTable = (DataTable)Session["QueryTable"]; }` to your page load. See [Persist c# objects across postbacks](http://stackoverflow.com/questions/6309185/persist-c-sharp-objects-across-postbacks) – Conrad Frix Jun 08 '12 at 20:16
  • Excellent! That solved my problem. I think I'm finally starting to understand all this data and binding stuff! :-) – Kyle Preiksa Jun 08 '12 at 20:54