Filter records from the dataset in vb.net
-
01-07-2021 - |
Pergunta
I want to filter values from a dataset. It contain phone numbers starting with zero and non zero values. How can I filter a phone number (start with non zero no) from the dataset. Below is the vb.net code and the resulting error.
cmd = New OracleCommand("select PHONE from reports.renewal_contact_t where run_date=to_date('" + TextBox1.Text + "','mm/dd/yyyy') and EXP_DATE =to_date('" + TextBox2.Text + "','mm/dd/yyyy') and region not in('TNP')", cn)
ada = New OracleDataAdapter(cmd)
ada.Fill(ds, "reports.renewal_contact_t ")
Dim ds1 As New DataSet
ds1.Tables("reports.renewal_contact_t").DefaultView.RowFilter = "PHONE NOT like'0'"
Error: Object reference not set to an instance of an object. error in rowfilter line
Solução
Within your ds1 there is no table *reports.renewal_contact_t*. Change it to
cmd = New OracleCommand("select PHONE from reports.renewal_contact_t where run_date=to_date('" + TextBox1.Text + "','mm/dd/yyyy') and EXP_DATE =to_date('" + TextBox2.Text + "','mm/dd/yyyy') and region not in('TNP')", cn)
ada = New OracleDataAdapter(cmd)
ada.Fill(ds, "reports.renewal_contact_t")
' no need for a new dataset - as you fill ds (and not ds1)!!
ds.Tables("reports.renewal_contact_t").DefaultView.RowFilter = "PHONE NOT like'0'"
Outras dicas
This will work against a dataset with a 'schema' similar to
**data example>*
Id Value
1 303-888-8888
2 0-418-895-9598
TryCast(filteredDS.Tables(0).Rows.Cast(Of DataRow).Where(Function(row) Not row.ItemArray(1).ToString().Substring(0, 1).Equals("0")), IEnumerable(Of DataRow)).ToList().ForEach(Sub(row) row.Delete())
--removes any record where the phone starts with anything other than a zero
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow