Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I have an error"'object' does not contain a definition for 'AddWithValue' and no accessible extension method 'AddWithValue' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)" where did I do wrong

What I have tried:

C#
private void btnadd_Click(object sender, EventArgs e)
     {
         try
         {
             con.Close();
             cmd = new MySqlCommand("SELECT * FROM account where code = '" + txtcode.Text + "' OR description = '" + txtaccountname.Text + "'", con.con);
             con.Open();
             reader = cmd.ExecuteReader();
             if (!reader.Read())
             {
                 //insert tenant details
                 con.Close();
                 cmd = new MySqlCommand("INSERT INTO `account`(`Type`, `Code`, `Description`) VALUES (@type,@code,@description);", con.con);
                               cmd.Parameters.AddWithValue("@type", txttype.SelectedValue);
                 cmd.Parameters.AddWithValue("@code", txtcode.Text);
                 cmd.Parameters.AddWithValue("@description", txtaccountname.Text);

                 con.Open();
                 reader = cmd.ExecuteReader();
Posted
Updated 9-Oct-22 18:26pm
v2

1 solution

First off, never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

You already know how to use parameters so why expose your DB to just anyone?

But to fix the problem you have found, we';d need to know exactly which line it complains about - because I don't think it's in that code fragment!
Double click the error message in Visual Studio, and it'll take you directly to it.
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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