-1

I making a database and I can add names, but I can't remove please help me.

This is my code:

private void button2_Click(object sender, EventArgs e)
{
    using (connection = new SqlConnection(connectionString))
    using (SqlCommand cmds = new SqlCommand("DELETE FROM Recipe WHERE firstName = @user", connection))
    {
        connection.Open();
        cmds.Parameters.AddWithValue("@user", cmds);
        cmds.ExecuteNonQuery();

    }
    populateRecipe();
}

This is the error

An unhandled exception of type System.ArgumentException occurred in System.Data.dll Additional information: No mapping exists from object type System.Data.SqlClient.SqlCommand to a known managed provider native type.

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
Captain
  • 113
  • 2
  • 9
  • 3
    Any errors that you can share? – Paul Sasik Aug 30 '15 at 07:13
  • 2
    what is the error you are getting? – Kryptonian Aug 30 '15 at 07:13
  • An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll Additional information: No mapping exists from object type System.Data.SqlClient.SqlCommand to a known managed provider native type. – Captain Aug 30 '15 at 07:25
  • I have read here http://stackoverflow.com/questions/9155004/difference-with-parameters-add-and-parameters-addwithvalue/9155103#9155103 that `cmds.Parameters.AddWithValue` is not a good way for work. It only send a `nvarchar` to the database instead of an `int`. – H. Pauwelyn Aug 30 '15 at 07:29

2 Answers2

3

Instead of passing the cmds itself to the AddWithValue method :

cmds.Parameters.AddWithValue("@user", cmds);

I suppose you should pass your actual uesrname :

cmds.Parameters.AddWithValue("@user", username);
Mohammad Chamanpara
  • 2,049
  • 1
  • 15
  • 23
  • im done learning c# now i need to continue SQL Learning CREATE TABLE [dbo].[Recipe] ( [Id] INT IDENTITY (1, 1) NOT NULL, [firstName] NVARCHAR (50) NOT NULL, PRIMARY KEY CLUSTERED ([Id] ASC) ); it's my database – Captain Aug 30 '15 at 07:22
  • @Captain I suggest you learning Entity Framework. It's not only pretty easy to use but you will also enjoy dealing with it. Good luck. – Mohammad Chamanpara Aug 30 '15 at 07:28
1

You are passing the cmds variable as the value for the @user parameter which doesn't make sense because it's a SqlCommand object:

cmds.Parameters.AddWithValue("@user", cmds);

Don't you intend to pass the user's name that you want to delete instead?

cmds.Parameters.AddWithValue("@user", "Username");
Saeb Amini
  • 23,054
  • 9
  • 78
  • 76