Click here to Skip to main content
16,016,477 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here are my code ....

private void SaveorUpdateLedger(string storedProcName)
        {
            DBSQLServer db = new DBSQLServer(AppSetting.ConnectionString());
            db.SaveOrUpdateRecord(storedProcName, GetObjectLedger());
        }

        private List<ledgerinsert> GetObjectLedger()
        {
            List<ledgerinsert> ledgerList = new List<ledgerinsert>();

            foreach (DataRow dr in Datatable.dtLedger.Rows)
            {
                LedgerInsert ledger = new LedgerInsert();

                ledger.Entry_Date = Convert.ToDateTime(dr["Entry_Date"]);

                ledger.Entry_Type = Convert.ToInt32(dr["Entry_Type"]);
                ledger.Party_ID = Convert.ToInt32(dr["Party_ID"]);

                ledger.Detail = Convert.ToString(dr["Detail"]);

                ledger.Amount = Convert.ToDecimal(dr["Amount"]);

                ledger.Account_Type = Convert.ToInt32(dr["Account_Type"]);
                ledger.User_ID = Convert.ToInt32(dr["User_ID"]);
                ledger.Sr_No = Convert.ToInt32(dr["Sr_No"]);
                ledger.PartyID_CR = Convert.ToInt32(dr["PartyID_CR"]);
                ledger.Region_ID = Convert.ToInt32(dr["Region_ID"]);
                ledger.Sector_ID = Convert.ToInt32(dr["Sector_ID"]);
                ledger.Company_ID = Convert.ToInt32(dr["Company_ID"]);
                ledger.PartyID_DR = Convert.ToInt32(dr["PartyID_DR"]);

                ledgerList.Add(ledger);

            }

            dgvPurchase.DataSource = ledgerList;
            return ledgerList;
        }



.............


C#
//SAVE OR UPDATE EXECUTE NON-QUERY
        public void SaveOrUpdateRecord(string storedProceName, object obj)
        {
            using (SqlConnection conn = new SqlConnection(_connstring))
            {
                using (SqlCommand cmd = new SqlCommand(storedProceName, conn))
                {
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    conn.Open();

                    //Parameters
                    Type type = obj.GetType();
                    BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
                    PropertyInfo[] properties = type.GetProperties(flags);

                    foreach (var property in properties)
                    {
                        cmd.Parameters.AddWithValue("@" + property.Name, property.GetValue(obj, null));
                    }

                    cmd.ExecuteNonQuery();
                }
            }
        }


What I have tried:

If I tried to insert only one row without the list then it works fine ... but my data is more than one rows .. then I get the error of parameters count mismatch ...
Posted
Updated 24-May-19 22:20pm
v2
Comments
BillWoodruff 25-May-19 0:11am    
Insert break-points, isolate the place the error occurs: then, describe the error and location here.
Richard MacCutchan 25-May-19 4:20am    
Look at the items returned from GetProperties and ensure they are the same as those specified in the stored procedure.

1 solution

Start by looking at your stored procedures: you pass the SP name in to your methods, and build a set of parameters for it to process.
And you are passing too many parameters to the SP - but we have no idea what the SP is called, what it does, how many parameters it is expecting, or how many you are actually passing.

So use the debugger to find out what the SP is called.
Find out how many - and what type - of parameters it expects.
Then back to the debugger to find out how many - and what type - you are passing.

We can't do any of that for you!
 
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