Click here to Skip to main content
15,900,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this code runs with out any errors.but still it doesn't delete the specific record in the table. what is wrong with the code?please help me to find the error!!

private string dbconstr = "Data Source=.\SQLEXPRESS;AttachDbFilename=path.mdf;Integrated Security=True;User Instance=True";
        private SqlConnection con;
        private SqlCommand cmd;
        private SqlDataReader dr;
        string query = "";

try
   {
    con = new SqlConnection(dbconstr);
    con.Open();
    query = "delete from test_table where name='"+textBox1.Text+"'";
    cmd = new SqlCommand(query, con);
    dr = cmd.ExecuteReader();
    con.Close();
    MessageBox.Show("AAA");
   }
catch 

following is the updated code,

try
   {
    con = new SqlConnection(dbconstr);
    con.Open();
    query = "delete from test_table where name='"+textBox1.Text+"'";
    cmd = new SqlCommand(query, con);
    int rowsDeleted = cmd.ExecuteNonQuery();
    MessageBox.Show(rowsDeleted + " rows deleted");
    //cmd.ExecuteNonQuery();
    con.Close();
    //MessageBox.Show("AAA");
   }
catch { }

and the test_table definition is,

column name    Data type
id_image         int
name             nvarchar(50)
party            nchar(10)
party_im         image
name_im          image


can't we delete a row as usual when there is an image type data is existing in the table??
Posted
Updated 4-Jun-11 4:38am
v3
Comments
thatraja 2-Jun-11 9:57am    
I think the passed value is the issue. Include the table structure with data in your question.
Tarun.K.S 2-Jun-11 10:03am    
Correct. There must be some small mistake.
Tarun.K.S 2-Jun-11 10:20am    
Found any solution yet?

simple error...

use :

int num=cmd.ExecuteNonQuery();


not reader...

reader is used to retrieve data from the database...
 
Share this answer
 
v3
Comments
Member 7779792 2-Jun-11 9:46am    
i replaced
dr = cmd.ExecuteReader(); with cmd.ExecuteNonQuery();
but still its as same as previous! what should i do to correct it?
Kim Togo 2-Jun-11 9:55am    
If you do a "SELECT * FROM test_table WHERE name='" + textBox1.Text + '", do you then find any rows ?
Tarun.K.S 2-Jun-11 10:02am    
Good question. Awaiting answer.
Member 7779792 2-Jun-11 10:01am    
yes!
textBox1.Text contains "abc" and table is also having a record with "abc"
karan joshua 2-Jun-11 10:06am    
check it again by removing all private specifier....
You have to use ExecuteNonQuery instead of <code>ExecuteReader:
Try now:
C#
try
   {
    con = new SqlConnection(dbconstr);
    con.Open();
    query = "delete from test_table where name='"+textBox1.Text+"'";
    cmd = new SqlCommand(query, con);
    //dr = cmd.ExecuteReader();
    int rowsDeleted = cmd.ExecuteNonQuery();
    MessageBox.Show(rowsDeleted + " rows deleted");
    con.Close();
    //MessageBox.Show("AAA");
   }
 
Share this answer
 
Comments
Member 7779792 2-Jun-11 9:54am    
changed the code as u suggested,but still it is same as previous.specified record is not deleted from the table.
Tarun.K.S 2-Jun-11 10:07am    
Query looks fine. Can you try writing it more neatly like:
DELETE FROM test_table WHERE name = '" + textBox1.Text + "'";
It might not make a difference but it will be more readable.
Monjurul Habib 2-Jun-11 14:50pm    
nice answer. my 5.
Tarun.K.S 2-Jun-11 14:52pm    
Thank you very much. :)
RaviRanjanKr 5-Jun-11 0:00am    
Good Answer Tarun, My 5 :)
 
Share this answer
 
Comments
Tarun.K.S 2-Jun-11 14:52pm    
Yes that should help the OP. 5+
Monjurul Habib 2-Jun-11 14:58pm    
thank you.
The method looks fine, the logic looks fine... everyone has given everything you need to delete a row... all that is left to double check is you. :)

All I mean by that is, sometimes we get caught up in the code when soemthing doesn't seem to be working, and fail to step back a little.

First, make sure you are running your code against the right database and table. Sometimes, people use test tables that mimick production tables, then when they are testing code they might be changing a test table, but looking at the production table expecting the result.

Second, if you are getting the field value to look for from a textbox, keep in mind that 1- the query is case-sensitive, so if the [name] field contains "ABC", it won't be found using name='abc'. 2- make sure there are no extra spaces before or after the text in the textbox, you can make sure by simply using
"delete from table where name='" + textbox1.Text.Trim() + "'"


Third, if you have a window open in your IDE showing the contents of that table, then execute the code, and don't refresh that grid showing you the rows, it could appear like the record is not gone... but it very well could be. (kind of an obvious one... but it happens...)

Lastly, let everyone know if you are getting any exceptions or messages when executing this code, or if it runs fine but just doesn't seem to be removing the record...

Best of luck!
 
Share this answer
 
The code looks fine. Just to mention certain points in addition to the rest of the answers.

1.Make sure you are pointing to the right database.
2.Check the value is exists in the table.
3.If the datatype of name column is varchar, space has factor. So make sure the value matches exactly what's there in the table.

Good luck.
 
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