ASP.NET dataset getdataBy Failed to enable constra

2019-07-14 05:35发布

hi i have a very simple webform i have a button and a gridview on this form and a dataset that contains linked tables bill, docket, docket_bill etc.

On Button click i use the following code

  protected void button_click(object sender, EventArgs e)
{
billTableAdapter Billta = new billTableAdapter();
gridview1.datasource = Billta.getTop20Bills();
gridview1.databind()'
}

Now when i click on the button, i get the following error

"Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign key constraints"

However, when i change the code to

 protected void button_click(object sender, EventArgs e)
    {
    billTableAdapter Billta = new billTableAdapter();
    gridview1.datasource = Billta.getdata();
    gridview1.databind()'
    }

It works fine. billTa.getData() gets all the rows from the dataset and shows up in the gridview. but when i add a query and select only few columns, then it gives me the aforementioned error.

Any idea what is wrong here?

SQL Scripts for getdata() = select * from bill

SQL script for getTop20Bills = select top 20 bill_id, bill_amount from bill

1条回答
时光不老,我们不散
2楼-- · 2019-07-14 06:20

I guess you have more than two columns in your billTableAdapter. In getTop20Bills() you select only two columns you need. You have to add missing columns to your second script. Or you can create new Table Adapter, that contains only this two columns and bind grid view to new adapter

查看更多
登录 后发表回答