Click here to Skip to main content
15,887,436 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have weir problem with my database, because I cant delete last row, but when I run that same instuoction in SQL it works fine my code:
public void DeleteLastRowClovekData()
        {
            SqlConnection cnn;
            string databaseName = "[MydatabaseName]";
            string connection_string = Helper.GetInfo("[MYlinkForConnectionString]");
            string del;
            del = "DELETE FROM " + databaseName + " WHERE Id = (SELECT MAX(Id) FROM "       + databaseName + " )";
            cnn = new SqlConnection(connection_string);
            SqlCommand cursor = new SqlCommand(del, cnn);
            cnn.Open();
            cursor.ExecuteNonQuery();
            cnn.Close();
        }


BTW: There are 10 columns.

What I have tried:

I have tried this code, recreating table and I have tried Reader query but nothing is seems to be working. And this code:
SqlConnection cnn;
            string databaseName = "[MYdatabaseName]";
            string connection_string = Helper.GetInfo("[MylinkForConnectionString]");
            string del;
            del = "DELETE FROM " + databaseName + " WHERE Id = (SELECT MAX(Id) FROM " + databaseName + " )";
            cnn = new SqlConnection(connection_string);
            SqlCommand cursor = new SqlCommand(del, cnn);
            SqlDataReader Reader;
            cnn.Open();
            Reader = cursor.ExecuteReader();
            while (Reader.Read())
            {
            }
            cnn.Close();
Posted
Updated 3-Feb-20 1:06am
v2

Why are you trying to use a Reader? There is nothing to read - a DELETE command doesn't return any rows so it's probably throwing an exception.

Use ExecuteNonQuery and your problem should just go away.
 
Share this answer
 
Comments
BrogP 2-Feb-20 12:47pm    
I had not used reader in first case and it also dont work.
OriginalGriff 2-Feb-20 13:08pm    
"it dont work" is probably the most useless problem report we get - and we get it a lot. It tells us nothing about what is happening, or when it happens.
So tell us what it is doing that you didn't expect, or not doing that you did.
Tell us what you did to get it to happen.
Tell us any error messages.
Tell us what you looked at to find out why!
BrogP 2-Feb-20 13:24pm    
I fixed it already. Thanks for trying to help tho :)
MadMyche 2-Feb-20 15:07pm    
What was the solution?
Maciej Los 2-Feb-20 14:42pm    
5ed!
In addition to solution #1...

This command:
C#
del = "DELETE FROM " + databaseName + " WHERE Id = (SELECT MAX(Id) FROM " + databaseName + " )";

is very dangerous!
You may loose all your data! Please, read about SQL Injection - SQL Server | Microsoft Docs[^]

You shouldn't use concatenated strings. You have to use parameterized queries, see: Using Parameters for SQL Server Queries and Stored Procedures[^]
 
Share this answer
 
First thing I would suggest

Quote:
del = "DELETE FROM " + databaseName + " WHERE Id = (SELECT MAX(Id) FROM " + databaseName + " )";


put a breakpoint on this line and inspect what value del has assigned to it.

I am going to guess that you are trying to delete incorrectly as you are inputting the database name rather than the table that you need delete from.
 
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