Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am working with a small POS application, but I ma having a problem during the insertion of data from win form to SQL database . To be more clearly I get this kind of error "Additional information: Procedure or function insertimi has too many arguments specified."

What should I change in my C# code to fix the error? Maybe the loop code how I get the data from datagridview. If someone could help me, about this problem.

Thanks you everyone

What I have tried:

C#
    conn.Open();
    SqlCommand cmd = new SqlCommand("insertimi", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Clear();
    cmd.Parameters.Add(new SqlParameter("@shenime", txtshenime.Text));
    cmd.Parameters.Add(new SqlParameter("@data", DateTime.Now));
    cmd.Parameters.Add(new SqlParameter("@kasieri", lbluser.Text));
    cmd.Parameters.Add(new SqlParameter("@emri_sh", lblshitesiemri.Text));
    cmd.Parameters.Add(new SqlParameter("@niptsh", int.Parse(lblshitesinipt.Text)));
    cmd.Parameters.Add(new SqlParameter("@adresash", lbladresashitesi.Text));
    cmd.Parameters.Add(new SqlParameter("@telefonish", lblshitesitelefon.Text));
    cmd.Parameters.Add(new SqlParameter("@emri_b", cmbbleresi.Text));
    cmd.Parameters.Add(new SqlParameter("@niptb", lblbleresinipt.Text));
    cmd.Parameters.Add(new SqlParameter("@adresab", lblbleresiadresa.Text));
    cmd.Parameters.Add(new SqlParameter("@telefonib", lblbleresitelefoni.Text));
    cmd.Parameters.Add(new SqlParameter("@nentotali", lblnentotali.Text));
    cmd.Parameters.Add(new SqlParameter("@vleratvsh", lblvleratvsh.Text));
    cmd.Parameters.Add(new SqlParameter("@zbritja", txtzbritja.Text));
    cmd.Parameters.Add(new SqlParameter("@totali", lbltotali.Text));

    foreach (DataGridViewRow row in dtgprofatura.Rows)
    {
        if (!row.IsNewRow)
        {

            cmd.Parameters.Add(new SqlParameter("@barkodi", row.Cells[0].Value));
            cmd.Parameters.Add(new SqlParameter("@emertimi", row.Cells[1].Value));
            cmd.Parameters.Add(new SqlParameter("@sasia", row.Cells[3].Value));
            cmd.Parameters.Add(new SqlParameter("@cmimi", row.Cells[2].Value));
            cmd.Parameters.Add(new SqlParameter("@totaliPCS", row.Cells[5].Value));
            cmd.Parameters.Add(new SqlParameter("@tvsh", row.Cells[4].Value));
            cmd.Parameters.Add(new SqlParameter("@vleratvshpcs", row.Cells[7].Value));
            cmd.Parameters.Add(new SqlParameter("@patvshpcs", row.Cells[6].Value));

        }
    }
    cmd.ExecuteNonQuery();                    
    conn.Close();
    clear();
    kRIJOToolStripMenuItem.PerformClick();
}
And the SP code:
SQL
 ALTER procedure [dbo].[insertimi]

@shenime varchar(max),
@data datetime,
@kasieri varchar(50),
@emri_sh varchar(50),
@niptsh varchar(50),
@adresash varchar(100),
@telefonish varchar(50),
@emri_b varchar(50),
@niptb varchar(50),
@adresab varchar(100),
@telefonib varchar(50),
@nentotali float,
@zbritja float,
@vleratvsh float,
@totali float,
@barkodi int,
@emertimi varchar(200),
@sasia int, 
@cmimi float,
@totaliPCS float,
@tvsh float,
@vleratvshpcs float,
@patvshpcs float
as


declare @lastId int;
insert into tblprofatura (Shenime,Data,Kasieri,Emri_sh,NIPT_sh,Adresa_sh,Telefoni_sh,Emri_b,NIPT_b,Adresa_b,Telefoni_b,Nentotali,Zbritja,VleraTVSH,Totali)


values(@shenime,@data,@kasieri,@emri_sh,@niptsh,@adresash,@telefonish,@emri_b,@niptb,@adresab,@telefonib,@nentotali,@zbritja,@vleratvsh,@totali)
set @lastId = SCOPE_IDENTITY();


insert into tblproofatura_details(NR_F,Barkodi,Emertimi,Cmimi,Sasia,TVSH,Totali,PaTVSHpcs,VleraTVSHpcs)
values (@lastId,@barkodi,@emertimi,@cmimi,@sasia,@tvsh,@totaliPCS,@patvshpcs,@vleratvsh)
Posted
Updated 29-Oct-20 0:39am
v2

If you don't understand an error message, google it: procedure or function insertimi has too many arguments specified - Google Search[^]
It's unlikely that you are the first person to meet this problem, and the quickest solution is to look for others and how they fixed it.

In this case, Google helps nicely: you get the error when you provide more parameters than the stored procedure is defined to accept: you provide 15, plus an extra 8 for every single row in the DataGridView, while the SP is defined as taking 23. If there is more than one row in the DGV, it will fail.

You need to think about exactly what you are trying to do: you can't pass multiple rows like that!
 
Share this answer
 
Comments
Member 14947303 29-Oct-20 4:31am    
But how to insert data from datagridview to SQL database in other way. I tried a loop to insert data from datagridview which datagridview may have more than one rows. I can't insert in other way just looping every row
OriginalGriff 29-Oct-20 4:46am    
Loop on the rows, or use a DataAdapter:
https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/modifying-data-with-stored-procedures
Every time through your loop, you add a new copy of several parameters to the command.

Instead, add the parameters outside of the loop, and set their values inside the loop. You'll need to move the ExecuteNonQuery call inside the loop as well.

You'll need to specify the correct database type for each parameter, since it won't have a value to guess the type from.
C#
SqlParameter pBarkodi = cmd.Parameters.Add("@barkodi", SqlDbType.???);
SqlParameter pEmertimi = cmd.Parameters.Add("@emertimi", SqlDbType.???);
// Repeat for other parameters...

foreach (DataGridViewRow row in dtgprofatura.Rows)
{
    if (!row.IsNewRow)
    {
        pBarkodi.Value = row.Cells[0].Value;
        pEmerimi.Value = row.Cells[1].Value;
        // Set for other parameters...
        cmd.ExecuteNonQuery();
    }
}
 
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