Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I'm trying to use a basic button to update an entry in my database. I already populated a single data with 5 columns on some textboxes. What I'm trying to do now is how to update the data by using the edited text inside the textboxes. All responses will be highly appreciated. Here is my code:

JavaScript
protected void ButtonUpdate_Click(object sender, EventArgs e)
        {
            var connectionString = "server=127.0.0.1;uid=root;" + "pwd=xxxxxxxx;database=DBFB;";
            MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
            
            using (MySqlConnection con = new MySqlConnection(connectionString)) 
            using (MySqlCommand cmd = new MySqlCommand("update TBL_FB Set FirstName=@FirstName, LastName=@LastName, ContactNumber=@ContactNumber, Email=@Email, Message=@Message where ID= " + Request.QueryString["ID"], con))
            {
                cmd.Parameters.Add("@FirstName", TextBoxFirstName.Text);
                cmd.Parameters.Add("@LastName", TextBoxLastName.Text);
                cmd.Parameters.Add("@ContactNumber", TextBoxContactNumber.Text);
                cmd.Parameters.Add("@Email", TextBoxEmail.Text);
                cmd.Parameters.Add("@Message", TextBoxMessage.Text);
                
                con.Open();
                cmd.ExecuteNonQuery();
            }
            Response.Redirect("Details.aspx?ID=" + TextBoxID.Text);
        }
Posted
Comments
Ziee-M 21-Feb-14 4:16am    
Hi,So you want the update to occur automatically once the user change the value of a textbox?
Tom Marvolo Riddle 21-Feb-14 4:21am    
you can update asusual.It will get the current value from textbox.If i understood it wrong then explain bit more
Ahmed Bensaid 21-Feb-14 5:43am    
Do you have an error ?
ZurdoDev 21-Feb-14 7:58am    
I don't understand. You have code there to do what you are asking. Where are you stuck?

problem solved... thanks to everyone who replied...

i just forgot to add this on the page load
<pre lang="Javascript">

if (Page.IsPostBack)
            {
            }
 
Share this answer
 
Why don't you compose your command string is a text string instead of parametrized string?

(This string, being an outside SQL string will require single quotes around appropriate values)

Then, if it doesn't work, you can then easily examine the tsql being sent to the server and find out why it's not doing what you wish.

Tastes vary, but a parametrized string is a lot of extra work unless you're entering data in a loop.
 
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