Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I have problem at "I don't know what's wrong with my code. The code can be executed, but my data can't be deleted.
Here my code:

What I have tried:

C#
private void DeleteCmd(object sender, EventArgs e)
            {
                conn = new SqlConnection("Server=TA;Data Source=TA; Database=dbSaham;Integrated Security=SSPI");
                conn.Open();
                string sql = "";
                ds = new DataSet();
                da = new SqlDataAdapter();
                DialogResult hsl = MessageBox.Show("Anda Yakin ?", "Hapus", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                if (hsl == DialogResult.Yes)
                {
                    sql = String.Concat("Delete From UNICJK Where Date = '", dateTimePicker4.Value.Date.ToString(), "'");
                }
                da.DeleteCommand = new SqlCommand(sql, conn);
                string pesan = String.Concat(da.DeleteCommand.ExecuteNonQuery(), " Record berhasil dihapus");
                MessageBox.Show(pesan, "Info Hapus");
                dgvSaham.Visible = true;
                SqlCommand cmd2 = new SqlCommand("Select * From UNICJK", conn);
                da.SelectCommand = cmd2;
                da.Fill(ds, "UNICJK");
                dgvSaham.DataSource = ds.Tables["UNICJK"];
            }
Posted
Updated 4-Apr-17 10:06am
v5
Comments
CHill60 4-Apr-17 8:16am    
You don't appear to be executing the SqlCommand that does the delete. The only time you do a da.Fill is after setting the SelectCommand
[no name] 4-Apr-17 8:22am    
If you are saving Dates in your database as strings instead of Dates, that is likely your problem.

Your Date column has probably the type DATETIME. When then passing a date only string using dateTimePicker4.Value.Date.ToString(), that will be extended to a DATETIME with the time portion set to 00:00:00 and only those records match that have that time.

You have to specify that you only want to compare the date portion:
C#
sql = String.Concat("DELETE FROM UNICJK WHERE CAST(Date AS DATE) = '", dateTimePicker4.Value.Date.ToString(), "'");

In your case it would be even better to use a parameterised command to avoid passing dates formatted as strings:
C#
da.DeleteCommand = new SqlCommand("DELETE FROM UNICJK WHERE CAST(Date AS DATE) = CAST(@PickerDate AS DATE)", conn);
da.DeleteCommand.Parameters.AddWithValue("@PickerDate", dateTimePicker4.Value)
 
Share this answer
 
Comments
Richard Deeming 4-Apr-17 8:46am    
It would be even better if you made the query SARGable! :)
DELETE FROM UNICJK WHERE Date >= @PickerDate And Date < DateAdd(day, 1, @PickerDate)
Jochen Arndt 4-Apr-17 8:56am    
You are right.
But I'm actually not sure if the time part is always zero for a date picker.
But it should be no problem to use an intermediate variable with the time set to zero then.
Richard Deeming 4-Apr-17 8:57am    
If you set the parameter value to dateTimePicker4.Value.Date, the time part will be zero. :)
Never build an SQL query by concatenating with user inputs, it is named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability.
SQL injection - Wikipedia[^]
SQL Injection[^]

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
Member 12461669 4-Apr-17 23:49pm    
thank's for your suggest, but i can't do debug, i don't know how to do, but i will learn it soon.

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