Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to pass multiple rows to database..i tried below code but only first row inserting after then error raising..
C#
public static bool SaveChitGroupDetails(List<ChitGroup> lst)
        {
            try
            {
                _dal = DataLayer.GetInstance();
                _dal.Sql = StoredProcedures.SaveChitGroupDetails;
                _dal.CommandType = CommandType.StoredProcedure;
                foreach (ChitGroup  obj in lst)
                {
                    _dal.AddParameter("_grcode", obj.GrpCode, DbType.String, ParameterDirection.Input);
                    _dal.AddParameter("_brid", obj.Brid, DbType.Int16, ParameterDirection.Input);
                    _dal.AddParameter("_prdcode", obj.Prdcode, DbType.String, ParameterDirection.Input);
                    _dal.AddParameter("_valuecode", obj.Valuecode, DbType.String, ParameterDirection.Input);
                    _dal.AddParameter("_companychit", obj.Companychit, DbType.Boolean, ParameterDirection.Input);
                    _dal.AddParameter("_series", obj.Series, DbType.String, ParameterDirection.Input);
                    _dal.AddParameter("_status", obj.Status, DbType.String, ParameterDirection.Input);
                    _dal.AddParameter("_bylawnumber", obj.Bylawnumber, DbType.String, ParameterDirection.Input);
                    _dal.AddParameter("_registrationdate", obj.RegistrationDate, DbType.Date, ParameterDirection.Input);
                    _dal.AddParameter("_commencedate", obj.CommenceDate, DbType.Date, ParameterDirection.Input);
                    _dal.AddParameter("_terminatedate", obj.TerminateDate, DbType.Date, ParameterDirection.Input);
                    _dal.AddParameter("_maxdiscount", obj.MaxDiscount, DbType.Int16, ParameterDirection.Input);
                    _dal.AddParameter("_active", obj.Active, DbType.Boolean, ParameterDirection.Input);
                    _dal.AddParameter("_userid", obj.Userid, DbType.String, ParameterDirection.Input);
                    _dal.AddParameter("_amndmntdate", obj.AmendmentDate, DbType.Date, ParameterDirection.Input);
                    _dal.AddParameter("_amndtrmntdate", obj.AmendTerminateDate, DbType.Date, ParameterDirection.Input);
                    _dal.AddParameter("_description", obj.Description, DbType.String, ParameterDirection.Input);
                    _dal.AddParameter("_docno", obj.DocNo, DbType.String, ParameterDirection.Input);
                    _dal.AddParameter("_assignedvalue", obj.AssignedValue, DbType.Decimal, ParameterDirection.Input);
                    _dal.AddParameter("_type", obj.Type, DbType.String, ParameterDirection.Input);
                    int rtrnid = _dal.ExecuteNonQuery();
                    
                }
                //int rtrnid = _dal.ExecuteNonQuery();

                return true;
            }
            catch (Exception ex)
            {
                throw ex;
                return false;
            }
            finally
            {
                if (_dal.Connected)
                    _dal.Connection.Close();

            }
        }
Posted
Comments
Krunal Rohit 30-Sep-15 7:29am    
What error ?

-KR

You should not add parameters in the loop (because they are added at each iteration, which will cause trouble at the second iteration).

Instead, you should define the parameters before the loop, and assign them a value in the loop.
 
Share this answer
 
Comments
ZurdoDev 30-Sep-15 7:35am    
Or just call .Parameters.Clear() at the beginning of the loop.
phil.o 30-Sep-15 7:46am    
Yes, too. But then there's an overhead clearing and recreating parameters at each iteration (even if I don't know if the extra time it will take is really problematic).
ZurdoDev 30-Sep-15 7:48am    
True. However, the code is much easier to do with Parameters.AddWithValue() than defining each parameter separately and then setting its value.

Everything has a trade-off. ;)
User-11630313 30-Sep-15 7:52am    
thank you...@RyanDev your answer is simple and super
Me? I'd convert the list to a DataTable and pass that directly via a DataAdapter.
This might help: Converting a List to a DataTable[^]
 
Share this answer
 
you can try entity framework to connect the database, it is far easy when doing list operation.
check out https://msdn.microsoft.com/en-us/library/aa697427(v=vs.80).aspx
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900