0

Hello I found this code snippet for Adding Values to MySQL Commands in C#

MySqlCommand command = connection.CreateCommand();
command.CommandText = "INSERT INTO tb_mitarbeiter (Vorname) VALUES (?name)";
command.Parameters.AddWithValue("?name", mitarbeiter);
connection.Open();
command.ExecuteNonQuery();

Now I want to add data to more than one coloumn, but if i try it like this:

command.Parameters.AddWithValue("?id", Projektid, "?projektnummer", Projektnummer, "?anlageDatumProjekt", Projektanlage, "?auftraggeberProjekt",Projektauftraggeber, "?bezeichnungProjekt", Projektbezeichnung, "?nachAufwand", nachAufwand, "?bemerkungProjekt",Projektbemerkung, "?matFremdKalk", matfremdkalk, "?maStundenKalk",mastundenkalk, "?maKostenKalk", makostenkalk, "?maZuschlagFixKalk", mazuschlagfixkalk, "?maZuschlagVarKalk",mazuschlagvarkalk, "?maschStundenKalk",maschstundenkalk, "?maschKostenKalk",maschkostenkalk, "?maschZuschlagFixKalk",maschzuschlagfixkalk, "?maschZuschlagVarKalk",maschzuschlagvarkalk, "?summeAuftragKalk",summeauftragkalk, "?matFremdErfasst",matfremderfasst, "?maStundenerfasst",mastundenerfasst, "?maSelbstkostenErfasst",maselbstkostenerfasst, "?maschStundenErfasst",maschstundenerfasst, "?maschKostenVariabelErfasst",maschkostenvariabelerfasst, "?maschKostenFixErfasst",maschkostenfixerfasst, "?gemeinKostenBerechnet",gemeinkostenberechnet, "?abschlagRechnungErfasst",abschlagrechnungerfasst, "?rabatt", rabatt);

it just says that

No Overload for AddWithValue with 52 Arguments.

What do I have to change to get the code running?

3 Answers 3

3

You try to add all your 52 parameter and their values with one AddWithValue method. You can't do that.

First of all, you need to define all your parameters in your command with your column names like;

command.CommandText = "INSERT INTO tb_mitarbeiter (Vorname, id, projectnummber....) VALUES (?name, ?id, ?projektnummer....)";

Then you need to add all your 52 parameters in a 52 different AddWithValue method.

command.Parameters.AddWithValue("?name", mitarbeiter);
command.Parameters.AddWithValue("?id", Projektid);
command.Parameters.AddWithValue("?projektnummer", Projektnummer);
...
...

Or add all of them in a one MySqlParameterCollection and add this collection to your command as;

command.Parameters.Add(MyParameterCollection);
Sign up to request clarification or add additional context in comments.

1 Comment

thanks that worked for me, the tb mitarbeiter was a copy mistake there is a tbprojects with coloums for each value
0

Try to only add one value per AddWidthValue like

command.Parameters.AddWithValue("?name", mitarbeiter);
command.Parameters.AddWithValue("?id", Projektid);
command.Parameters.AddWithValue("?projektnummer", Projektnummer)

and so on.

Sven

Comments

0

You need to add each sql paramater on its own. An example would be in your case;

command.Parameters.AddWithValue("?id", Projektid);
command.Parameters.AddWithValue("?pr1ojektnummer", Projektnummer);

finally call the

command.ExecuteNonQuery();       

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.