Reputation: 6868
I am having textbox where user can enter product id and based on that populate gridview.I have all the products in my excel file
and using OleDbConnection and OleDbDataAdapter
I fill my datatable which is binded with gridview using BindingSource
.
Is it possible to get some indication product is found or not(null or count check) in OleDbDataAdapter before filling datatable?
Code :
string fieldSelector = "[ProductID], [ProductName],[MRP]";
string query = $"SELECT {fieldSelector} FROM [{sheetName}$A1:Q15535] WHERE [ProductID] = {Convert.ToInt32(txtProductId.Text)}";
using (OleDbConnection cnnxls = new OleDbConnection(strConn))
using (OleDbDataAdapter oda = new OleDbDataAdapter(query, cnnxls))
{
oda.Fill(dtProductList);//dtProductList global variable at winform level
}
Lets say I have already added 2 products and now I enter product which is not found in excel then again i have to may be look inside datatable and based on that show message to user that "Product is not found".
Upvotes: 0
Views: 152
Reputation: 52
You will have to make an OleDbCommand.ExecuteScalar call to find the count of records returned. You may have to change fieldSelector to "count(*)" when using ExecuteScalar
Upvotes: 0