Reputation: 1367
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
Upvotes: 4
Views: 24120
Reputation: 1
Dim indexTable As Integer = ds.Tables.Count - 1 ' ultima tabla dentro del dataSet
Dim tmpTable = ds.Tables(indexTable) ' copias la tabla del dataset
tmpTable.DefaultView.RowFilter = "PHONE NOT like'0'" ' filtrar los datos de la temporal
ds.Tables.RemoveAt(indexTable) ' eliminas los datos sin filtrar
ds.Tables.Add(tmpTable.DefaultView.ToTable) ' agrega los datos filtrados de la temporal
ds.AcceptChanges() ' aceptas los cambios
Upvotes: 0
Reputation: 51
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
Upvotes: 1
Reputation: 8359
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'"
Upvotes: 3