Click here to Skip to main content
14,878,179 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am deleting a row with some conditions(In this User is Varchar datatype and Favorite is Textdatatype). But I don't know delete query is not working.

But when I gave the data directly like this

string delquery = "Delete from Parameter Where Group = 'Favoriten'  AND User = ?  AND Favorite Like  'DeliverySchedule'"
.Then my delete query is working. But passing with parameters is not working. Can someone help me in this issue?

What I have tried:

C#
 private void deleteItem_Click(object sender, RoutedEventArgs e)
 {
            OleDbConnection oledbconn = dvxr.getconnR46D100();
            OleDbDataAdapter oleda = new OleDbDataAdapter();
            try
            {
                string username = dvxr.mDVXRT.usercode.ToString();
                string favname = dvxr.mDVXRT.favoritename.ToString();
                string delquery = "Delete from Parameter Where Group = 'Favoriten'  AND User = ?  AND Favorite Like ? ";
                OleDbCommand olecmd = new OleDbCommand(delquery, oledbconn);
                olecmd.Parameters.AddWithValue("@uid", username);
                olecmd.Parameters.AddWithValue("@fname", favname);
                oleda.DeleteCommand = olecmd;
                olecmd.ExecuteNonQuery();
                oledbconn.Close(); 
                MessageBox.Show("Favorite is Deleted");
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }

}
Posted
Updated 3-Apr-19 4:29am
v4
Comments
Richard Deeming 4-Apr-19 11:14am
   
NB: Assuming you're connecting to Microsoft SQL Server, the text type has been deprecated since 2005, and will be removed in a future version. You should be using varchar(max) instead.
ntext, text, and image (Transact-SQL) - SQL Server | Microsoft Docs[^]

1 solution

We can't tell: we have no idea what your data looks like, or what username or favname contain.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
   

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