I've got an error when i'm using MySql Stored Procedure and
Data Access Layer (MVC)have a method used to read data from database using MySqlDataAdapter
and MySqlParameter
and DataTable
to read data called storedata
i was tested this method before in a login controller and it's work as well : the main error here when i need to store last accountId using MySql last_insert_id()
function but it always returns null value i tried to Convert it to int then add 1 in every time form opening , but that is useless because it's returns an error
System.ArgumentException: 'Parameter 'accountId' not found in the collection.'
here is the method in Data Access Layer that i use it to store data from database
public DataTable storeData(string trChannel,MySqlParameter[] list_OF)
{
InitializeDb();// Database configration method
MySqlCommand Transmeter = new MySqlCommand
{
CommandType = CommandType.StoredProcedure,
CommandText = trChannel,
Connection = dbcon // dbcon its Connection string comming from DAL
};
if (list_OF != null)
{
Transmeter.Parameters.AddRange(list_OF);
}
MySqlDataAdapter massenger = new MySqlDataAdapter(Transmeter);
DataTable _mainContainer = new DataTable();
massenger.Fill(_mainContainer);
disConnect();
return _mainContainer;
}
The stored procedure that i used it like this after i tried to use last_insert_id() and i failed so i change the sp to this and its work and give me result when i call it inside MySQL Server
CREATE DEFINER=`root`@`localhost` PROCEDURE `getAccountId`(out accountId int(5))BEGIN select Max(acotId)from accounts;set @accountId=last_insert_id(acotId); END
In this case the server going throw an error
Unknown "acotId" in fields list
but i don't need to fix it because it's give me a result , so when i called this procedure inside application using a method that return a DataTable
value give the first error that i wrote it before System.ArgumentException: 'Parameter 'accountId' not found in the collection.'
for the record i got more than five methods contains the same error most of them with input or output parameter and most of them without ..
public DataTable getAcotId()
{
DataTable pdbContainer = new DataTable();
pdbContainer = _socket.storeData("getAccountId",null);//_socket it's a link to data access layer
return pdbContainer;
}
I tried also method like this with a parameter and it's also didn't works
public DataTable getCusttId()
{
MySqlParameter[] parCut = new MySqlParameter[1];
parCut[0] = new MySqlParameter("?custId", MySqlDbType.Int16, 5) ;
parCut[0].Direction = ParameterDirection.Output;
DataTable pdbContainer = new DataTable();
pdbContainer = _socket.storeData("getCustomerId", parCut);
return pdbContainer;
}
Finally i want to convert the result from this methods to int and i think about using this code
int customerFinalId = getCustId.Rows.Fields[i].Feilds<int>("custId")
That is all guys and i'm dire to need help immediately for more necessary
Thank you a lot for helping me ..
You got MySQL, that is a problem for this problem. It is very common that you need to figure out "the Primary Key of the thing you just inserted". Usually to update the (G)UI.
If this was SQL, the OUTPUT clause would be your friend. This thing alone is worth its memory footprint in gold. But MySQL does not have any equivalent Syntax. That means you have to do it the hard way.
When figuring this value out, it is very important to guard against race conditions. The reliable way is to replace the implicit Transactions and Table locks of the DML statement with a explicit one that covers both the DML statement and the DQL Satement (SELECT) that follows.
Unfortunatley I am not that solid in MySQL Syntax, so somebody else will have to give you exact code.
There are multiple issues with your code
You NEED to provide all the parameters your SP expects or else it will just error out. Since you do not have any try and catch in your SP your entire query will bail on first error.
@accountId is a sql variable and you need to declare that if you want to use it
The answer here is the second method of getCusttId
replacing the ?
by @
, which works for me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.