I'm trying to do this with LINQ:
SQL>> SELECT p_Id from items WHERE p_Name = 'R1'
LINQ>> var getpID = (from p in myDatabaseDataSet.Items
where p.p_Name == ptxtBox.Text
select p.p_Id).SingleOrDefault();
int p_Id = getpID;
But getpID always return 0. What's wrong?.
Edit: nvm the syntax is correct. Reason why it always returned 0 is the value doesn't exist in the db. I had the txt box value inserted in the db and then was trying to call it again before refreshing myDatabaseSet. So I needed to add Fill() and then getpID is able to read the value.
Full code:
int c_Id = Convert.ToInt32(pcomboBox.SelectedValue);
int p_Id = 0;
string itemName = ptxtBox.Text;
this.Validate();
this.itemsBindingSource.EndEdit();
this.itemsTableAdapter.Insert(c_Id, itemName);
this.myDatabaseDataSet.AcceptChanges();
this.itemsTableAdapter.Fill(myDatabaseDataSet.Items);
var getpID = (from p in myDatabaseDataSet.Items
where p.p_Name == itemName
select p.p_Id).SingleOrDefault();
p_Id = getpID;