Click here to Skip to main content
15,917,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
con.Open();
//string un = TextBox1.Text;
SqlCommand cmd1 = new SqlCommand("UPDATE userdet SET username = @username, FirstName = @FirstName, MiddleName = @MiddleName, LastName = @LastName, Organisation = @Organisation, Bussiness = @Bussiness, Address1 = @Address1, Address2 = @Address2, City = @City, State = @State, Country = @Country, PinCode = @PinCode, Tel = @Tel, Email = @Email, Doj = @Doj, fax = @fax, website = @website, description = @description, dat = @dat WHERE (username = @username) OR (FirstName = @FirstName) OR (MiddleName = @MiddleName) OR (LastName = @LastName) OR (Organisation = @Organisation) OR (Bussiness = @Bussiness) OR (Address1 = @Address1) OR (Address2 = @Address2) OR (City = @City) OR (State = @State) OR (Country = @Country) OR (PinCode = @PinCode) OR (Tel = @Tel) OR (Email = @Email) OR (Doj = @Doj) OR (fax = @fax) OR (website = @website) OR (description = @description) OR (dat = @dat)", con);
SqlCommand cmd2 = new SqlCommand("update usertable set username=@username,password=@password,ustatus=@ustatus where username=@username or password=@password or ustatus=@ustatus", con);
//cmd1.Parameters.AddWithValue("@UserName", TextBox2.Text);
// cmd1.Parameters.AddWithValue("@username1", TextBox1.Text);
// cmd2.Parameters.AddWithValue("@username", TextBox1.Text);
cmd1.Parameters.AddWithValue("@username", TextBox2.Text);
cmd1.Parameters.AddWithValue("@FirstName", TextBox3.Text);
cmd1.Parameters.AddWithValue("@MiddleName", TextBox4.Text);
cmd1.Parameters.AddWithValue("@LastName", TextBox5.Text);
cmd1.Parameters.AddWithValue("@Organisation", TextBox6.Text);
cmd1.Parameters.AddWithValue("@Bussiness", TextBox7.Text);
cmd1.Parameters.AddWithValue("@Address1", TextBox8.Text);
cmd1.Parameters.AddWithValue("@Address2", TextBox9.Text);
cmd1.Parameters.AddWithValue("@City", TextBox10.Text);
cmd1.Parameters.AddWithValue("@State", TextBox11.Text);
cmd1.Parameters.AddWithValue("@Country", TextBox12.Text);
cmd1.Parameters.AddWithValue("@PinCode", TextBox13.Text);
cmd1.Parameters.AddWithValue("@Tel", TextBox14.Text);
cmd1.Parameters.AddWithValue("@Email", TextBox15.Text);
cmd1.Parameters.AddWithValue("@Doj", TextBox16.Text);
cmd1.Parameters.AddWithValue("@fax", TextBox17.Text);
cmd1.Parameters.AddWithValue("@website", TextBox18.Text);
cmd1.Parameters.AddWithValue("@description", TextBox19.Text);
cmd1.Parameters.AddWithValue("@dat", TextBox20.Text);

cmd1.ExecuteNonQuery();

cmd2.Parameters.AddWithValue("@username", TextBox2.Text);
cmd2.Parameters.AddWithValue("@password", TextBox21.Text);
cmd2.Parameters.AddWithValue("@ustatus", TextBox22.Text);

cmd2.ExecuteNonQuery();


Label21.Text = "Updated";
con.Close();


while executing. all the rows in the table is getting changed. actually if i set username in userdet as primary key its not updating. why? i want to upadate that too. please help
Posted

You are not supposed to update a primary key.
I suggest you use a serial no in your table which acts as your primary key - you can then change the username as you wish.
 
Share this answer
 
I think your problem is that you have way too many OR clauses in you SQL statement. If ANY ONE of them is true, then that row will be updated. So there is a very good chance that every row contains some info that passes at least one of the clauses. For example, if the fax number if blank, it will pass every row that has no fax number.
 
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