ASP.NET insert from bll to mysql
I am trying to do an insert into a MySql database using a 3 layer solution (or whatever it can be called).
I have done this several times with MS-sql database and it worked very well.
But now when I try to do an insert, I get the ID cannot be null. I thought the database took care of this. If I write the insert directly in the code and use MySqlCommand and executeNonQuery, it works great.
Can't I use BLL and DAL with MySql?
Error message:
System.Data.NoNullAllowedException: Column 'GiftID' does not allow null values. at System.Data.DataColumn.CheckNullable (DataRow row) at System.Data.DataColumn.CheckColumnConstraint (DataRow row, DataRowAction action) at System.Data.DableTable.RaiseRowChanging (DataRowChangeEventArgs args, DataRow eRow eRow, DataRowAction) .Data.DataTable.SetNewRecordWorker (DataRow String, Int32 Suggested Record, DataRowAction, Boolean isInMerge, Int32 Position, Boolean fireEvent, Exception & deferredException) at System.Data.DataTable.InsertRow (DataRow String, Int32, ID, Int32 pos, Boole ) to System.Data.DataRowCollection.Add (DataRow row) PayEx.payexusersDataTable.AddpayexusersRow (payexusersRow row) in the c: \ Users \ IT \ AppData \ Local \ Temp \ Temporary ASP directory.NET Files \ payex \ 45bd406a \ 10c84208 \ App_Code. cyqhjqo7.1.cs: line 444 in PayExBLL.AddPayExUser (line name, line name, line company, line address, line index, line city, line phone, line email, ContactMe byte, UInt32 count, UInt32 transaction number, anonymous byte , line g Currency) in c: \ Users \ IT \ Documents \ Visual Studio 2008 \ WebSites \ payex \ App_Code \ BLL \ PayExBLL.cs: line 66 in _Default.btn_next3_Click (object sender, EventArgs e) in c: \ Users directory \ IT \ Documents \ Visual Studio 2008 \ WebSites \ payex \ Default.aspx.cs: line 191line g Currency) in c: \ Users \ IT \ Documents \ Visual Studio 2008 \ WebSites \ payex \ App_Code \ BLL \ PayExBLL.cs: line 66 in _Default.btn_next3_Click (object sender, EventArgs e) in c: \ Users \ directory IT \ Documents \ Visual Studio 2008 \ WebSites \ payex \ Default.aspx.cs: line 191line g Currency) in c: \ Users \ IT \ Documents \ Visual Studio 2008 \ WebSites \ payex \ App_Code \ BLL \ PayExBLL.cs: line 66 in _Default.btn_next3_Click (object sender, EventArgs e) in c: \ Users \ directory IT \ Documents \ Visual Studio 2008 \ WebSites \ payex \ Default.aspx.cs: line 191
My code:
[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
public bool AddPayExUser(string Firstname, string Lastname, string Company, string Address, string Zip, string City, string Phone, string Email, byte ContactMe, uint Amount, uint TransactionNumber, byte Anonymous, string Currency)
{
PayEx.payexusersDataTable puTable = new PayEx.payexusersDataTable();
PayEx.payexusersRow puRow = puTable.NewpayexusersRow();
puRow.Firstname = Firstname;
puRow.Lastname = Lastname;
puRow.Company = Company;
puRow.Address = Address;
puRow.Zip = Zip;
puRow.City = City;
puRow.Phone = Phone;
puRow.Email = Email;
puRow.ContactMe = ContactMe;
puRow.Amount = Amount;
puRow.TransactionNumber = TransactionNumber;
puRow.Anonymous = Anonymous;
puRow.Currency = Currency;
puTable.AddpayexusersRow(puRow);
int rowsAffected = Adapter.Update(puTable);
return rowsAffected == 1;
}
a source to share
System.Data.NoNullAllowedException: Column 'GiftID' does not allow nulls. at
From the look and feel of your code, you are forgetting to pass a parameter GiftID
to your function, but it was expecting (and cannot be null) in your table row.
Hence the exception. Either install it in your code above or provide the default in your MySQL database.
a source to share
EDIT: This comment suggests that you are inserting into a table for which GiftId is the primary key, which seems unlikely. If so, Eoin Campbell's answer makes more sense!
Check if GiftId column is AUTO_INCREMENT; what is the MySQL identity equivalent.
You can recreate the column as an id, for example:
ALTER TABLE Gifts
DROP COLUMN GiftId;
ALTER TABLE items
ADD COLUMN GiftId INT NOT NULL AUTO_INCREMENT FIRST;
a source to share