2

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
Shezi
  • 1,352
  • 4
  • 27
  • 51

1 Answers1

3

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

zabulus
  • 2,373
  • 3
  • 15
  • 27
  • you are right... that was exactly the reason. but i m a bit confused now. so say i want to display gridview with different columns depending on user's selection. The user has atleast 25 options to display data , so does it mean i will have to add 25 different tableadapters, each containing a combination of different columns? It doesn't make sense to me though :( – Shezi May 16 '11 at 09:48
  • You may try to use controls ObjectDataSources. This will bound gridview with your table schema in design time. In run time you can set uninteresting columns visible to false. – zabulus May 16 '11 at 09:55