vb.net query to display specific rows of datatable

2019-02-20 05:53发布

问题:

How do I display the specific rows returned by the query in a Table Query code is

SELECT name, STD, Fees, paid
FROM  register
WHERE (paid - Fees < 0)

I get error

    "An unhandled exception of type 'System.Data.ConstraintException'               
    occurred in System.Data.dll
    Additional information: Failed to enable constraints. One or more rows                                               

    contain values violating non-null, unique, or foreign-key constraints."

But when I return all rows it works fine How do I display only specific rows?

I used

Dim test As DataTable
test = Me.RegisterTableAdapter.GetDataBy ' GetDataBy is Query    
DataGridView1.DataSource = test

回答1:

Make the query by selecting all the columns in the table. If you omit the columns in the query data TableAdapter populates null.

SELECT student_id, name, contact_number, address, STD, FEES, Duration, image_lotacion, (....) 
FROM  register WHERE (paid - Fees < 0)

Another option is to allow the DataTable accepts null values and no error. In the properties of each column in the DataTable, set the AllowDBNull property to True, and the property NullValue Empty or 0 if it is a numeric or Boolean value. You should also remove the primary key and allow the column is zero, because if not include in the office, also will give error.