Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a web form that has a saved button and a couple of textboxes on it. The user can enter data into the textboxes and save it to the database temporary. The user can come back to the web form and the saved data is populated back into the textboxes. The user wants to change the data in the populated textbox but can't. The data changes back to what was populated from the database. I have a SQL Delete statement in place but it doesn't seem to work. What did I do wrong?


Here is the populated code with Delete Statement:
C#
if (TextBoxINST_ID.Text.Trim().Length > 0)
            {
                SqlConnection con44 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
                con44.Open();

                SqlCommand scmd44 = new SqlCommand("Select FT_UNDERGR, FT_GRAD, FTE_UNDERG, FTE_GRAD, NON_CREDIT, TOTAL_FTE, FCFTUHC, FCFTPBHC, FCPTUHC, FCPTPBHC, NCHC from TableSave where INST_ID = '" + TextBoxINST_ID.Text + "'", con44);
                SqlDataReader dr44 = scmd44.ExecuteReader();
                if (dr44.Read())
                {
                    TextBoxFTUG.Text = dr44["FT_UNDERGR"].ToString();
                    TextBoxFTG.Text = dr44["FT_GRAD"].ToString();
                    TextBoxTHUGDR.Text = dr44["FTE_UNDERG"].ToString();
                    TextBoxTHGDR.Text = dr44["FTE_GRAD"].ToString();
                    TextBoxNCCDR.Text = dr44["NON_CREDIT"].ToString();
                    TextBoxTCNC.Text = dr44["TOTAL_FTE"].ToString();
                    TextBoxTNFUG.Text = dr44["FCFTUHC"].ToString();
                    TextBoxTNFG.Text = dr44["FCFTPBHC"].ToString();
                    TextBoxTNCPUG.Text = dr44["FCPTUHC"].ToString();
                    TextBoxTNCPG.Text = dr44["FCPTPBHC"].ToString();
                    TextBoxTNNCC.Text = dr44["NCHC"].ToString();

                    TextBoxTNFUG.Text = string.Format("{0:0,0}", double.Parse(TextBoxTNFUG.Text));
                    TextBoxTNFG.Text = string.Format("{0:0,0}", double.Parse(TextBoxTNFG.Text));
                    TextBoxTNCPUG.Text = string.Format("{0:0,0}", double.Parse(TextBoxTNCPUG.Text));
                    TextBoxTNCPG.Text = string.Format("{0:0,0}", double.Parse(TextBoxTNCPG.Text));
                    TextBoxTNNCC.Text = string.Format("{0:0,0}", double.Parse(TextBoxTNNCC.Text));
                    TextBoxFTUG.Text = string.Format("{0:0,0}", double.Parse(TextBoxFTUG.Text));
                    TextBoxFTG.Text = string.Format("{0:0,0}", double.Parse(TextBoxFTG.Text));
                    TextBoxTHUGDR.Text = string.Format("{0:0,0}", double.Parse(TextBoxTHUGDR.Text));
                    TextBoxTHGDR.Text = string.Format("{0:0,0}", double.Parse(TextBoxTHGDR.Text));
                    TextBoxNCCDR.Text = string.Format("{0:0,0}", double.Parse(TextBoxNCCDR.Text));
                    TextBoxTCNC.Text = string.Format("{0:0,0}", double.Parse(TextBoxTCNC.Text));

                    ButtonSubmit.Visible = true;
                    ButtonPrint.Visible = false;
                    ButtonSave.Visible = false;
                }
                con44.Close();
                dr44.Close();

                if (TextBoxINST_ID.Text.Trim().Length > 0)
                {
                    SqlConnection con45 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
                    con45.Open();
                    {
                        SqlCommand scmd45 = new SqlCommand("Delete FT_UNDERGR, FT_GRAD, FTE_UNDERG, FTE_GRAD, NON_CREDIT, TOTAL_FTE, FCFTUHC, FCFTPBHC, FCPTUHC, FCPTPBHC, NCHC from TableSave where INST_ID = '" + TextBoxINST_ID.Text + "'", con45);
                        ButtonPrint.Visible = false;
                    }
                    con45.Close();
                }   
            }
        }
    }
Posted
Updated 27-Oct-14 5:17am
v3
Comments
Shweta N Mishra 27-Oct-14 12:51pm    
syntax for your delete statement is not correct. You can not select specific columns to delete.

Did you tried running the command in back end and check the result ?

The proper DELETE[^] statement is:
SQL
DELETE
FROM TableName
WHERE NumericField = 1

instead of
SQL
DELETE
FROM TableName
WHERE NumericField = '1'


I need to warn you: do not use sql queries in code behind, because of SQLInjection[^]. Use stored procedures instead.
Walkthrough: Displaying Data Using a Stored Procedure in the GridView Web Server Control[^]
How to call SQL Server stored procedures in ASP.NET by using Visual Basic .NET [^]
How to: Execute a Stored Procedure that Returns a Single Value[^]

Sample procedure:
SQL
CREATE PROCEDURE DeleteFromDatabase
    @instid INT
AS
BEGIN
    DELETE
    FROM TableName
    WHERE INST_ID = @instid

    RETURN @@ROWCOUNT

END


For further information about SQLInjection, please see:
How To: Protect From SQL Injection in ASP.NET[^]
SQL Injection and how to avoid it[^]
Stop SQL Injection Attacks Before They Stop You[^]
 
Share this answer
 
v3
Comments
Computer Wiz99 27-Oct-14 11:40am    
I had the correct Delete Statement before but it still didn't work. Thanks for the links.
Maciej Los 27-Oct-14 11:51am    
Does INST_ID is numeric value?
Computer Wiz99 27-Oct-14 13:37pm    
Yes it is. It is what connects the data to that user.
Maciej Los 27-Oct-14 13:40pm    
So, please have a look at WHERE condition in DELETE statement. It should be:
WHERE INST_ID = 1
instead of
WHERE INST_ID = '1'
Do you see the difference?
Computer Wiz99 27-Oct-14 13:42pm    
Ok, but if I put Where INST_ID = 1 then won't that look for a value of 1 in the database?
seems a bit light on the design side - as I see it, you have two options

a) delete the data from the table once it has been 'restored' back to the textboxes

or

b) save the data to a temporary table - add a button, basically 'save final' that saves the data into the permanent table
 
Share this answer
 
Comments
Computer Wiz99 26-Oct-14 18:26pm    
Garth J Lancaster, I like option A. How would I code that?
Garth J Lancaster 26-Oct-14 18:44pm    
you would probably execute something like "delete from TableSave where INST_ID = '" + TextBoxINST_ID.Text + "'" before the con44.close() used after (re) polulating the textboxes - mind you, you'd have to make sure INST_ID was unique in this scheme, or make the delete somehow unique by using other values, else you could trash your database - its not the solution I'd prefer
Computer Wiz99 27-Oct-14 9:57am    
It is not working. I have uploaded the code edit.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900