Updating database information

I want to update user information in im database using C #. here is my code, it does not work, and it does not give me any errors.

protected void Page_Load(object sender, EventArgs e) { lbldisplay.Text = "<b><font color=BLUE>" + "WELLCOME:: " + "</font>" + "<b><font color=white>" + Session["Name"] + "</font>"; SqlConnection con = new SqlConnection("Data Source=STUDENT-PC;Initial Catalog=webservice_BuyBid;Integrated Security=True"); } protected void btnLogOut_Click(object sender, EventArgs e) { //this will redirect the page to the home.aspx page. Response.Redirect("Home.aspx"); } protected void btnUpdate_Click(object sender, EventArgs e) { //creating a new connection to the database SqlConnection con = new SqlConnection("Data Source=STUDENT-PC;Initial Catalog=BuyBid;Integrated Security=True;Asynchronous Processing=True;"); con.Open(); //creates a new sqlcommand to update the buyer information to the Database. SqlCommand cmd = new SqlCommand("UPDATE BUYER SET Name = @Name,Surname = @Surname,Email =@Email ,CellNumber =@CellNumber ", con); con.Open(); cmd.Parameters.AddWithValue("@Name", txtNameUpdate.Text); cmd.Parameters.AddWithValue("@Surname", txtSurnameUpdate.Text); cmd.Parameters.AddWithValue("@Email", txtemailUpdate.Text); cmd.Parameters.AddWithValue("@CellNumber", txtCellUpdate.Text); int rows = cmd.ExecuteNonQuery(); con.Close(); 
+5
source share
3 answers

Your problem is that you open the connection (this is correct), but then you open it again.

This is why the update does not occur. Also, you do not need to close the connection in the last declaration. This is not required.

You also execute the update statement, so make sure you give it a condition to update a specific record.

I edited your code correctly, this should solve your problem: (Tried and tested)


  protected void btnUpdate_Click(object sender, EventArgs e) { try { //creating a new connection to the database SqlConnection con = new SqlConnection("Data Source=STUDENT- PC;Initial Catalog=BuyBid;Integrated Security=True;Asynchronous Processing=True;"); con.Open(); //creates a new sqlcommand to update the buyers information to the Database. SqlCommand cmd = new SqlCommand("UPDATE BUYER SET Name = @Name,Surname = @Surname,Email =@Email ,CellNumber =@CellNumber WHERE Email =@Email ", con); //con.Open(); cmd.Parameters.AddWithValue("@Name", txtNameUpdate.Text); cmd.Parameters.AddWithValue("@Surname", txtSurnameUpdate.Text); cmd.Parameters.AddWithValue("@Email", txtemailUpdate.Text); cmd.Parameters.AddWithValue("@CellNumber", txtCellUpdate.Text); cmd.BeginExecuteNonQuery(); } catch (Exception e) { Console.WriteLine("Exception occured: " + e.StackTrace); } finally { // close connection if it is still open // editing from phone so just writting the comments here } } 

Let me know about the results.

+2
source

Why not try this if asynchronous processing is set to false. NB all rows in the table will be updated, since you have no restrictions in your query, i.e. no Where clause.

  try { SqlConnection con = new SqlConnection("Data Source=STUDENT-PC;Initial Catalog=BuyBid;Integrated Security=True;Asynchronous Processing=False;"); //creates a new sqlcommand to update the buyer information to the Database. SqlCommand cmd = new SqlCommand("UPDATE BUYER SET Name = '" + txtNameUpdate.Text + "' ,Surname = '" + txtSurnameUpdate.Text + "', Email = '" + txtemailUpdate.Text + "', CellNumber = '" + txtCellUpdate.Text + "'", con); con.Open(); int rows = cmd.ExecuteNonQuery(); con.Close(); } catch (Exception Ex) { MessageBox.Show(Ex.Message + Environment.NewLine + Ex.TargetSite, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } 
0
source

I used try catch or your code:

 protected void btnUpdate_Click(object sender, EventArgs e) { try { //creating a new connection to the database SqlConnection con = new SqlConnection("Data Source=STUDENT-PC;Initial Catalog=BuyBid;Integrated Security=True;Asynchronous Processing=True;"); con.Open(); //creates a new sqlcommand to update the buyer information to the Database. SqlCommand cmd = new SqlCommand("UPDATE BUYER SET Name = @Name,Surname = @Surname,Email =@Email ,CellNumber =@CellNumber ", con); con.Open(); cmd.Parameters.AddWithValue("@Name", txtNameUpdate.Text); cmd.Parameters.AddWithValue("@Surname", txtSurnameUpdate.Text); cmd.Parameters.AddWithValue("@Email", txtemailUpdate.Text); cmd.Parameters.AddWithValue("@CellNumber", txtCellUpdate.Text); int rows = cmd.ExecuteNonQuery(); } } catch (Exception) { } finally { con.Close(); } } 

use this trace, check not con.open (); true, and if any catch occurs.

0
source

All Articles