Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,087 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,570 people online right now. Registration is fast and FREE... Join Now!




Delete a row from database through delete button

 
Reply to this topicStart new topic

Delete a row from database through delete button

dmxxmd
14 Nov, 2007 - 11:44 PM
Post #1

New D.I.C Head
*

Joined: 25 Oct, 2007
Posts: 25



Thanked: 1 times
My Contributions
Hey guys

Im still busy with a webapp and the only thing that I still need to do is to be able to delete a record in other words a (row) from my datbase..

I am able to create a new row now all through my webapplication (website) but now I need to be able to delete the record that is in the textboxes..

my dataset fills a couple of textboxes so what you see on the textboxes is the current rows data, so I made a Button called Delete Record, Which I want to use to delete the records thats showing in the textboxes now...

Ive made a hidden field, so my rows got a unique number id, and then I made the hidden field = to the unique Id..
hidCode.Value = _ds.Tables[0].Rows[indexer]["AuditNameCode"].ToString();

here is my code :

CODE

protected void ButtonDelete_Click(object sender, EventArgs e)
    {
        SqlConnection _MyCon = new SqlConnection("Server=SECDW;database=SPAudit;uid=adffd;pwd=dfdfv;Connection Timeout=120;pooling=false");

        SqlCommand command = new SqlCommand("DELETE FROM MainDetail WHERE AuditNameCode = " + hidCode.Value  , _MyCon);
        
        command.CommandType = CommandType.Text;

        _MyCon.Open();

        command.ExecuteNonQuery();

        _MyCon.Close();

        
    }


Please can someone give me some help, its not working:)

Thanx

Please use code tags for posting code, like so => code.gif

This post has been edited by PsychoCoder: 15 Nov, 2007 - 05:25 AM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Delete A Row From Database Through Delete Button
15 Nov, 2007 - 04:38 AM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Is AuditNameCode a number or text in the database. If it's text, you'll want to put quotes around it.

Or, better yet, protect it from SQL injection and bind that var to it. e.g.

CODE

SqlCommand command = new SqlCommand("DELETE FROM MainDetail WHERE AuditNameCode = @AuditNameCode", _MyCon);
command.Parameters.AddWithValue("@AuditNameCode", hidCode.Value);
try {
   _MyCon.Open();
   command.ExecuteNonQuery();
} finally {
   _MyCon.Close();
}


Also note the try finally on the database connect. That's a pretty good thing to get into the habit of.

Hope this helps.

This post has been edited by baavgai: 15 Nov, 2007 - 04:39 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 08:15PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month