Search code examples
asp.netsqltableadapter

ASP.NET Get a single value returned from TableAdapter Select Query


I'm using TableAdapters on my ASP.NET Project and I've become stuck on an issue of the way to retrieve this data.

The code is as follows:

    BookingDataTableAdapters.bookingTableAdapter ta = new BookingDataTableAdapters.bookingTableAdapter();

    String booking_id_string = Request["id"];
    int booking_id = Int32.Parse(booking_id_string);


    BookingData.bookingDataTable table = ta.GetOwnerUsername(booking_id);

    String username = string.Empty;

    foreach (DataRow dr in table.Rows)
        username = dr[0].ToString();

However this will error with the following:

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

However when I run the query in the query builder, it runs fine without error. I've have also disabled "EnforceConstraints" inside the table adapter xsd file properties.

I've no idea what's wrong, is there a better way for me to get this single value back from my query in ASP.NET

Many thanks in advance, I appreciate the help :-)


Solution

  • What that tells me is that BookingData has been changed in the database and the object in your project was not updated. When you get the row it cant fill the object because perhaps the database has a null and the object does not allow it. Update the dataset.