Click here to Skip to main content
15,896,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Guys Nowadays am facing a issue and now am unable to resolve it bcz i have tried many ways to solve this problem but i was failed so now i want to get your help so please give me the better answer of this issue
This issue is when i delete single coloumn value from database it's delete all coloumn values.please give me the solution how i can delete single column value in ms accesss

C#
if (textBox63.Text == "")
            {
                MessageBox.Show("Sorry! Data was Empty!Please Try Again","Report",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }
            else
            {
                DialogResult dialogResult = form.ShowDialog();
                if (textBox.Text == "Black Emerald")
                {
                    connection.Open();
                    OleDbCommand command = new OleDbCommand();
                    command.Connection = connection;
                    command.CommandText = "Delete [Report] from Total";
                    command.ExecuteNonQuery();
                    connection.Close();
                }

                else
                {

                }


Thanks
Regards
M.Sajid.Lakha

What I have tried:

if (textBox63.Text == "")
{
MessageBox.Show("Sorry! Data was Empty!Please Try Again","Report",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
else
{
DialogResult dialogResult = form.ShowDialog();
if (textBox.Text == "Black Emerald")
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "Delete [Report] from Total";
command.ExecuteNonQuery();
connection.Close();
}

else
{

}
Posted
Updated 25-Jul-16 9:33am

By principle in databases, you delete rows, not columns.
If you want 0 in a column, you need to store 0 in that column, it is an update.

Guess some study of SQL is in order.

[Update]
Quote:
i dont want to delete this column i just wanna delete all values of this column
You can't delete the values, you want to put/store a new value in this column for every row !
Use the right wording and you will find much more help on Google.
 
Share this answer
 
v3
Comments
Member 9983063 24-Jul-16 17:49pm    
ok please tell me how to delete rows
Patrice T 24-Jul-16 18:01pm    
You already know it.
This is what you do by mistake.
Read SQL documentation about "delete"
Dave Kreskowiak 24-Jul-16 18:08pm    
The basic delete in SQL is:

DELETE FROM tableName [WHERE conditionExpression]
Apart from what others have said, in case when you are required to delete one record from the database you have to pass something to check against; a primary key is prefered. The code that you are using must be changed to:
SQL
-- Assyming you wanted to delete the records where report is null. 
DELETE FROM Total WHERE [Report] IS NULL

Dave gave you an idea of this, SQL expects you to tell what records to delete. Otherwise, it will simply delete the entire list of records. If, somehow you wanted to delete the column, you can instead alter the table and remove the column (if that is what is intended). That command would be like this,
SQL
ALTER TABLE Total DROP COLUMN [Report]

This will remove that column.

Edit

Since you wanted to remove the data in that column, instead of doing anything I would recommend UPDATE-ing the fields. You can do so in SQL, like this,
SQL
UPDATE Total SET [Report] = NULL

This will update the (entire — is this what you want?) table and set it to NULL. If you want to set a record or two to NULL, then try passing a WHERE clause there.
 
Share this answer
 
v2
Comments
Member 9983063 25-Jul-16 4:05am    
i dont want to delete this column i just wanna delete all values of this column
Afzaal Ahmad Zeeshan 25-Jul-16 15:16pm    
Missed that point of yours. Please see the update.
[no name] 25-Jul-16 15:52pm    
+5
Afzaal Ahmad Zeeshan 25-Jul-16 16:11pm    
Thank you!
Use ALTER TABLE to DROP the column.

ALTER TABLE (Transact-SQL)[^]

Note that you don't really need to remove the column. It would be simpler for your code to ignore the column when it isn't needed.
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 25-Jul-16 16:11pm    
Please read the OP's comment to my post Solution 2.
[no name] 26-Jul-16 12:24pm    
Now I'm not sure what the OP wants.
In any case ...
1. my answer is redundant and
2. OP hasn't accepted your answer.
Hopefully the OP will either accept your answer or clarify.

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