Clarify Seeker
Clarify Seeker

Reputation: 117

Enter data to particular column in sql

I have a text box that retrieve email from membership table in database. User may edit their email and update the new email. My question is, how to replace the old email with the new one? What is the query for sql?

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
    SqlCommand cmd = new SqlCommand("Insert into aspnet_membership("i dont know how whether to write all columns or only email);

    cmd.CommandType = CommandType.Text;
    cmd.Parameters.AddWithValue("@email", Textbox2.Text);

Upvotes: 0

Views: 1482

Answers (6)

RL89
RL89

Reputation: 1916

It will work like any other simple update SQL Query:

update TableName set Columname = @Value where Username = @Value

Upvotes: 2

András Ottó
András Ottó

Reputation: 7695

My question is, how to replace the old email with the new one?

You need an UPDATE in this case, instead of an INSERT:

   SqlCommand cmd = new SqlCommand("UPDATE aspnet_membership SET 
          email = @email WHERE userID = @userID", conn);

Upvotes: 3

user841123
user841123

Reputation:

Try this: You may need UserId, and new Email to replace existing email address for the given existing user.

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
SqlCommand cmd = new SqlCommand("UPDATE aspnet_membership SET email = @newEmail WHERE UserID = @userID");

cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@email", Textbox2.Text);
cmd.Parameters.AddWithValues("@UserId", YourUserId);

conn.open();
cmd.ExecuteNonQuery();
conn.close();

Upvotes: 0

Mayank Pathak
Mayank Pathak

Reputation: 3681

As you want to Update EmailID so your query should be

SqlCommand cmd = new SqlCommand("Update aspnet_membership set email=@email where  UserID=@UserID");

This should work for you, because your Insertquery which you are trying, this will always insert a new record instead of updating older records..

Upvotes: 0

spajce
spajce

Reputation: 7092

check this link from MSDN http://msdn.microsoft.com/en-us/library/ms186862.aspx

SELECT REPLACE('abcdefghicde','cde','xxx'); GO

Upvotes: 0

VIRA
VIRA

Reputation: 1504

you need to use update query and not insert query.

see syntax here:

http://www.w3schools.com/sql/sql_update.asp

Upvotes: 0

Related Questions