参数计数C#中的不匹配错误 [英] Parameters count mismatch error in C#

查看:224
本文介绍了参数计数C#中的不匹配错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码....



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;
        }







............. < br $> b $ b






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


//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 ...

推荐答案

首先查看存储过程:将SP名称传递给方法,并为其构建一组参数处理。

你向SP传递了太多参数 - 但是我们不知道SP被调用了什么,它做了什么,期望了多少参数,或者实际传递了多少参数。



因此,使用调试器找出SP的调用内容。

找出它预期的参数数量和类型。

然后回到调试器,找出你传递的数量和类型。 br />


我们不能为你做任何事情!
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!


这篇关于参数计数C#中的不匹配错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆