开发者

Mysterious constraint violation in c# db-related code

I use stored procs to get data from db (ms sql 2008 r2 express). Two fields in source table may contain NULL values so when i add data source to my project in VS in database designer i set nullable fields' AllowDBNull property to True and NullValue property to (Empty) (both are string fields).

Despite all those actions i get exception every time generated datatadapter tries to get row with null values. I've checked everything: AllowDBNull is True, NullValue is (Empty), no unique keys are in the table.

What the hell is wrong with my code?

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

Code:

foreach (BusStationDataSet.selectCarrierListRow row in _tblSource.Rows)
                    {
//Next line of code throws exception
                        BusStationDataSet.selectCarrierPhonesDataTable _tblPhones = _taPhones.GetData(row.carrierId, Settings.Default.DataLanguage);
                        phones = string.Empty;
                        int i = 0;

                        foreach (BusStationDataSet.selectCarrierPhonesRow phoneRow in _tblPhones.Rows)
                        {
                            i++;
                            string end = i == _tblPhones.Rows.Count ? "" : ", ";

                            phones += (phoneRow.countryCode != string.Empty ? phoneRow.countryCode : "") + (phoneRow.regionCode != string.Empty ? " (" + phoneRow.regionCode + ") " : "") + phoneRow.number +
                                end;
                        }
                        phones = phones == string.Empty 开发者_开发问答? MultilangInterface.Instance.GetMessageString(MultilangInterface.Messages.NullValue) : phones;

                        _dsDestination.Tables["Carrier"].Rows.Add(new object[]{
                            null,
                            row.carrierId,
                            row.title,
                            row.carrierType,
                            row.country,
                            row.city,
                            row.country + ", " + row.region + ", " + row.city + ", " + row.street + ", " + row.building + (string.IsNullOrEmpty(row.addressLine)?"":", " + row.addressLine),
                            row.lastName +" "+ row.firstName +" "+ row.middleName,
                            phones
                        });
                    }


Finally I've found the issue. There were two generated tables in project's dataset with almost similar names: selectCarriersPhonesDataTable and selectCarrierPhonesDataTable. The second one was the source of exception because for both of nullable fields the NullValue property was set to (Throw Exception). After I got rid of this diambiguation everything turned out to be working flawless. Definitely I should get enough sleep :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜