Click here to Skip to main content
16,011,685 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this code will tell me that my update was successful but will not update my database. I have stripped the code down to nothing and still cannot find the problem.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
 
namespace GigGuide
{
    public partial class userProfile : System.Web.UI.Page
    {
 

        protected void Page_Load(object sender, EventArgs e)
        {
 
            if (!IsPostBack)
            {
 

                LblUserName.Text = Convert.ToString(Session["New"]);
 
                string fillTextBoxes = "Select FirstName, Surname,TelephoneNo,PersonalEmail,Password,ContactbyText,ContactByEmail from Personal where UserName ='" + LblUserName.Text + "'";
 

                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ProjectDatabaseConnectionString"].ConnectionString);
                SqlCommand com = new SqlCommand(fillTextBoxes, conn);
                SqlDataReader reader;
 
                try
                {
                    conn.Open();
                    reader = com.ExecuteReader();
                    reader.Read();
 
                    TextBoxFirstName.Text = reader["FirstName"].ToString();
                    TextBoxSurName.Text = reader["Surname"].ToString();
                    TextBoxTelNo.Text = reader["TelephoneNo"].ToString();
                    TextBoxOldemail.Text = reader["PersonalEmail"].ToString();
                    TextBoxOldPassword.Text = reader["Password"].ToString();
                    reader.Close();
                    conn.Close();
                }
                catch (Exception ex)
                {
                    Response.Write("ERROR" + ex.ToString());
                }
 
            }
        }
 
        protected void ButtonUpdate_Click(object sender, EventArgs e)
        {
 

            try
            {
 

                SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ProjectDatabaseConnectionString"].ConnectionString);
                conn1.Open();
 

 

 
                // conn1.Open();
                SqlCommand com1;
                string updateQuery = "update Personal set PersonalEmail = @email,FirstName = @fname, Surname = @sname,TelephoneNo = @telno, Password = @password, ContactbyText = @conText, ContactByEmail = @conEmail Where UserName = '" + LblUserName + "'";
                com1 = new SqlCommand(updateQuery, conn1);
                com1.Parameters.AddWithValue("@email", TextBoxNewEmail.Text);
                com1.Parameters.AddWithValue("@password", TextBoxNewPassword.Text);
                com1.Parameters.AddWithValue("@fname", TextBoxFirstName.Text);
                com1.Parameters.AddWithValue("@sname", TextBoxSurName.Text);
                com1.Parameters.AddWithValue("@telno", TextBoxTelNo.Text);
                // com1.Parameters.AddWithValue("@location", locID);
                com1.Parameters.AddWithValue("@conText", RadioButtonListText.SelectedValue);
                com1.Parameters.AddWithValue("@conEmail", RadioButtonListEmail.SelectedValue);
 

                com1.ExecuteNonQuery();
                conn1.Close();
 
                Response.Write("Update was successful");
            }
 

 

 

            catch (Exception ex)
            {
                Response.Write("error" + ex.ToString());
            }
        }
    }
}
Posted
Comments
[no name] 22-Apr-14 17:00pm    
What is "LblUserName"? Should probably be LblUserName.Text or use a parameter like you should be using anyway.
lala24 22-Apr-14 17:44pm    
thank you so much i knew it was something stupid :)

1 solution

Posted to remove from the unanswered list

string updateQuery = "update Personal set PersonalEmail = @email,FirstName = @fname, Surname = @sname,TelephoneNo = @telno, Password = @password, ContactbyText = @conText, ContactByEmail = @conEmail Where UserName = '" + LblUserName + "'";

should be:
string updateQuery = "update Personal set PersonalEmail = @email,FirstName = @fname, Surname = @sname,TelephoneNo = @telno, Password = @password, ContactbyText = @conText, ContactByEmail = @conEmail Where UserName = '" + LblUserName.Text + "'";


Seems odd to me though that you mixed a parameterized query along with an SQL injection attack. You should use a parameter with LblUserName.Text also.
 
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