Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Deleting from an SQL database

Deleting from an SQL database

Scheduled Pinned Locked Moved Database
databasecsharphelpannouncement
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    alee15 10 88
    wrote on last edited by
    #1

    Hi all, I am doing a program with c# in which I am connecting it to an SQL database. I have a button (called btnDelete) in which in it I have the following code:- string query = ("DELETE FROM tblUsers WHERE User_name = ("+ txtDelete.Text +")"); SqlCommand myCommand = new SqlCommand(); myCommand.CommandText = query; myCommand.Connection = myConnection; myCommand.ExecuteNonQuery(); //Calling a method to update the database UpdateDataIntoDatabase(); MessageBox.Show( "Deleted." ); When I run the program and click the delete button the following error occurs pointing in the myCommand.ExecuteNonQuery() part:- An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll Additional information: System error. Can some one please tell me what do I have because I can't see anything wrong! Thanks a lot guys

    D R 2 Replies Last reply
    0
    • A alee15 10 88

      Hi all, I am doing a program with c# in which I am connecting it to an SQL database. I have a button (called btnDelete) in which in it I have the following code:- string query = ("DELETE FROM tblUsers WHERE User_name = ("+ txtDelete.Text +")"); SqlCommand myCommand = new SqlCommand(); myCommand.CommandText = query; myCommand.Connection = myConnection; myCommand.ExecuteNonQuery(); //Calling a method to update the database UpdateDataIntoDatabase(); MessageBox.Show( "Deleted." ); When I run the program and click the delete button the following error occurs pointing in the myCommand.ExecuteNonQuery() part:- An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll Additional information: System error. Can some one please tell me what do I have because I can't see anything wrong! Thanks a lot guys

      D Offline
      D Offline
      DotNetDominator
      wrote on last edited by
      #2

      Hi, replace your String Query to one given here. string query = "DELETE FROM tblUsers WHERE User_name = '" + txtDelete.Text + "'"; I this should work now. Also, Just check your connection is open before you assign it to myCommand. Thank you.

      A 1 Reply Last reply
      0
      • A alee15 10 88

        Hi all, I am doing a program with c# in which I am connecting it to an SQL database. I have a button (called btnDelete) in which in it I have the following code:- string query = ("DELETE FROM tblUsers WHERE User_name = ("+ txtDelete.Text +")"); SqlCommand myCommand = new SqlCommand(); myCommand.CommandText = query; myCommand.Connection = myConnection; myCommand.ExecuteNonQuery(); //Calling a method to update the database UpdateDataIntoDatabase(); MessageBox.Show( "Deleted." ); When I run the program and click the delete button the following error occurs pointing in the myCommand.ExecuteNonQuery() part:- An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll Additional information: System error. Can some one please tell me what do I have because I can't see anything wrong! Thanks a lot guys

        R Offline
        R Offline
        Rob Graham
        wrote on last edited by
        #3

        I case you can't see what's different in DotNetD's answer, he replaced the double quotes surrounding the textbox variable with single quotes. I strongly recommend that you check out this[^] article by Colin Angus Mackay before someone types ';truncate table tblusers; in your textbox and destroys your database. If you use parameters rather than direct text concatenation, you will avoid both the problem you are having now, and the vulnerability to sql injection attacks in your app. We need to graduate from the ridiculous notion that greed is some kind of elixir for capitalism - it's the downfall of capitalism. Self-interest, maybe, but self-interest run amok does not serve anyone. The core value of conscious capitalism is enlightened self-interest. Patricia Aburdene -- modified at 10:07 Sunday 7th May, 2006

        1 Reply Last reply
        0
        • D DotNetDominator

          Hi, replace your String Query to one given here. string query = "DELETE FROM tblUsers WHERE User_name = '" + txtDelete.Text + "'"; I this should work now. Also, Just check your connection is open before you assign it to myCommand. Thank you.

          A Offline
          A Offline
          alee15 10 88
          wrote on last edited by
          #4

          Ohh thanks... I arranged the quotes and it worked out Thanks a lot!!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups