1

when you select items from the listbox, you want to delete selecteditems. Why doesnt it work when selected data removed from database? I must have missed something. I got error message No mapping exists from object type.

This is a method parameter:

  IsDelete = _dinnerRemover.RemoveDinners(lstDinner.SelectedItems);

This class is to delete data from database

public bool RemoveDinners(dynamic dinnerItems)
{
    Dinners = new List<FoodInformation>();
    using (var sqlConn = new SqlConnection(_sqlConnectionString))
    {

        const string sqlQuery = "delete from DinnerTemplates where Dinner = @dinner";

        using (var command = new SqlCommand(sqlQuery, sqlConn))
        {
            try
            {
                //command.CommandType = CommandType.StoredProcedure;
                //command.CommandText = "sp_dinner";
                foreach (var item in dinnerItems)
                {
                    command.CommandType = CommandType.Text;
                    command.Parameters.AddWithValue("@dinner", item);
                    command.ExecuteNonQuery();

                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                sqlConn.Close();
            }
        }
    }

    return Dinners;
}
8
  • no it is not string. parameter is dynamic type. what is data type parameter for listbox.selectedItems? Commented Jun 19, 2013 at 12:04
  • The error message tells you exactly what the problem is and it doesn't appear to have anything to do with the code you posted. Commented Jun 19, 2013 at 12:04
  • see if inserting the exact string to delete works. Or adding .ToString() Commented Jun 19, 2013 at 12:08
  • listbox.selectedItems are string type. Commented Jun 19, 2013 at 12:10
  • i just edited my post. Look at the parameter. I have tried List<string> for the parameter. it doesnt work Commented Jun 19, 2013 at 12:11

4 Answers 4

2

If dinnerItems is a list of strings then say that, don't use dynamic unless you absolutely have to.

To delete a bunch of items, issue one sql query with an IN clause. Don't issue lots of individual queries.

Try this:

    public int RemoveDinners(List<string> dinnerItems)
    {
        using (var sqlConn = new SqlConnection(_sqlConnectionString))
        {

            const string sqlQuery = "delete from DinnerTemplates where Dinner in ({0})";

            using (var command = new SqlCommand())
            {

                var paramNames = new string[dinnerItems.Count];

                int i = 0;
                foreach (string item in dinnerItems)
                {
                    string paramName = "@Dinner" + i;
                    command.Parameters.AddWithValue(paramName, item);
                    paramNames[i] = paramName;
                    i += 1;
                }

                command.CommandText = String.Format(sqlQuery, String.Join(",", paramNames));
                command.Connection = sqlConn;
                command.CommandType = CommandType.Text;

                sqlConn.Open();

                return command.ExecuteNonQuery();
            }
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Wow it is working like a bomb. Thank you very very much! I am gonna mark answer :)
0

You have to bear in mind that you kind of left out some really relevant code, like what is a DinnerItem, since you're getting the error on a line related to its type.

However, the reason you're getting that error is because item can't be marshaled to a type of something like string or int.

That's probably because item is likely a custom class. One option would be to override the ToString method of the class:

public override string ToString() {
    // return some property value, or set of property values
    // strung together here.
}

another option would be to send in the actual Property you want off of item when issuing AddWithValue.

Comments

0

You need to define SqlDbType for command's parameter.

Comments

0

don't use dynamic type,use string.. if i were you,i would rather

IsDelete = _dinnerRemover.RemoveDinners(lstDinner.SelectedItems.ToString());

change the parameter to :

public bool RemoveDinners(string dinnerItems)

and the query to :

const string sqlQuery = "delete from DinnerTemplates where Dinner = dinnerItems";

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.