Click here to Skip to main content
14,875,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i am trying to update user profile details using update query in asp.net. But no update also no error showing. i am trying from 2 days to over come this problem. please help me. my code is here:

What I have tried:

con.Open();

method 1:
    id is primary key in sql server database.

        string cmd = "UPDATE login SET firstname=@fname, lastname=@lname, username=@username, email=@email, phone=@phone, gender=@gender, professionality=@pro where id=@id";
        SqlCommand sql = new SqlCommand(cmd,con);
        sql.Parameters.AddWithValue("@id",id);
        sql.Parameters.AddWithValue("@fname", fname.Text);
        sql.Parameters.AddWithValue("@lname", lname.Text);
        sql.Parameters.AddWithValue("@username", username.Text);
        sql.Parameters.AddWithValue("@email", email.Text);
        sql.Parameters.AddWithValue("@phone", phone.Text);
        sql.Parameters.AddWithValue("@gender",gender.SelectedItem+"");
        sql.Parameters.AddWithValue("@pro", pro.Text);
        sql.ExecuteNonQuery();
        con.Close();


method 2:
         here session["user"] is not primary key. but username column having unique data.

        con.Open();
        SqlCommand cmd = new SqlCommand("update login set firstname='" + fname.Text + 
        "',lastname='" + lname.Text + "'username='" + username.Text + "',email='" + 
        email.Text + "',phone='" + phone.Text + "',gender='" + gender.SelectedItem + 
        "',professionality='" + pro.Text + "' where username='"+Session["user"]+"'", 
        con);
        cmd.ExecuteNonQuery();
        con.Close();
Posted
Updated 2-Feb-18 0:53am

Don't use method 2. Never use method 2. Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

If your code is not getting an error but the database is not getting updated, then that means the UPDATE WHERE clause matched no rows.
Why not? Well, that's the big question! It could be that the ID value you are passing is not what you thought, or you may be accessing the wrong DB, or the new values you are setting them to are the same as the old ones. We can't tell.

So, its going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Look at your id variable and check exactly what it contains - is it the ID you expect? If it isn't, why not?
If it is, use SSMS to look at the DB content and examine that row. Does it exist? What are its current values? Are they different to the values you are passing?
Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.
I'd also want to look at the return value from the SQL operation:
C#
int rowsAffected = sql.ExecuteNonQuery();


Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
   
Comments
Mukthahar Shaik 2-Feb-18 10:52am
   
thank you Griff for your replay.

int rowsAffected = sql.ExecuteNonQuery();

result showing 0 rows effected. but did not got any mistake in query. i have 10 columns in a row. but i just updating only 7 columns.
OriginalGriff 2-Feb-18 11:03am
   
Yes, you can use any column(s) you want in the WHERE clause, it doesn't have to be the primary key.
A return of 0 says that no rows were changed - so your WHERE clause is not matching any.
So what did the debugger show was in id, and which rows did SSMS show it matched?
Mukthahar Shaik 2-Feb-18 11:38am
   
thank you thank you thank you thank you bro. i solved.
i dont know how to explain, but in pageload i was initialized DB values to textboxes.then after edit textbox values. i clicked on update button. that time page again loaded and replaced with old values. so i always get old values even after i updated. now i put pageload code into

if(!ispostback)
{
//code;
}

now everything ok.
OriginalGriff 2-Feb-18 11:40am
   
You're welcome!
check query string in "cmd" and copy query sting from your code and past in query analyzer then check your query.
   
Comments
Mukthahar Shaik 2-Feb-18 10:54am
   
thank you for your replay. Query analyzer is tool?
presently i am using Visual Studio 2010. is VS 2010

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