Click here to Skip to main content
15,908,906 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
private void BtnDelete_Click(object sender, EventArgs e)
{
    String cmdString = "DELETE FROM [Product_Details] WHERE ID=@ID";

    SqlCommand cmd = new SqlCommand(cmdString, con);
    cmd.Parameters.Add(new SqlParameter("ID", Int32.Parse(cmbProducts.SelectedValue.ToString())));

    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
    MessageBox.Show(" Data Deleted Successfully ");
}


What I have tried:

Above is my delete code and here i want to add as, if a selected person will able to delete the particular line of data others can't delete the particular line of data
Posted
Updated 31-Jul-19 22:12pm
v4

Your question doesn't make a lot of sense: once an item is deleted from the DB, it is deleted - no other user can delete deleted information, any more than two different people can burn the same bank note!

If what you are saying is that "if it's already been deleted don't show a 'Deleted successfully' message' then use the return value from ExecuteNonQuery to tell you how many rows were deleted:
int affected = cmd.ExecuteNonQuery();
If it's zero, there where no matching rows, so nothing was deleted.

If that isn't what you are having difficulty with, you will need to explain in much better detail exactly what your problem is!
 
Share this answer
 
Comments
Member 14137624 1-Aug-19 2:59am    
i want if a person want to delete a data he should have authentication then only he can delete the data
OriginalGriff 1-Aug-19 3:14am    
What kind of authorisation? There are many possibilities here:
1) SQL Users have to have the appropriate access to the DB, and "deleting rows" can be forbidden to some users and not others - that means that your SQL connection string could be different for "allowed users" and "non-allowed users".
2) Your users presumably have to log in to your system: in which case (as well as the provisions of GDPR, which I hope you are ware of) your system "knows" what kind of user he is - you can check that and not even try!
3) It may be that your user logs into his OS and that's the "user" you are referring to. Again, that's code in your app to check it.

Seriously, we only get what you type to work with - I'm not being awkward, just uninformed about your whole system and how that small piece of code fits into it!
Member 14137624 1-Aug-19 3:18am    
i have insert delete update and search part in my software this is a inventory system for my company, here i can login to the software system and insert update delete and search data by while deleting only selected persons can delete the data in the company i don't have any username password for sql server how i can do for deleting part
OriginalGriff 1-Aug-19 3:37am    
If you're using "Integrated security" as part of your connection string, you'll have to change that when you release the software as it's unlikely to work in production (but your connection string will be different for prod and dev anyway or you'll have massive problems later). And the SQL users you set up should have just enough permission to do their job, no more, no less - or you leave you DB open to accidental or deliberate damage.

If you users log into your inventory system then use the user data to decide if they are "allowed" - we can't help you with that, we have no access to your login code, or the DB that backs it up - so we have no idea what you store that identifies users as "allowed" or "not allowed"!
OriginalGriff 1-Aug-19 3:50am    
Oh gawd, you're a student ... that's gawd awful code.
So many major errors in so small a chunk of code.
Hardcoded connection strings, SQL Injection prone code, multiple hardcoded connection strings so they aren't always used, VS default names for controls, no consistency in coding style, occasional use of "using" block, occasional use of "try...catch" blocks, ...

And it doesn't show anything useful at all to do with your users.

Don't post your whole code, I'm not your tutor and I don't want to wade through undocumented stuff like that to try and work out what might be relevant - it's a waste of my time. You want me to look at code, show me the relevant bits so I don't have to hunt for them (and can't criticise the rest of it).
Have a look at this article on Authorization that's built into .Net Security In ASP.NET MVC[^]
 
Share this answer
 

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