2015-04-17 14 views
5

Desidero aggiornare le informazioni utente sul database im utilizzando C#. ecco il mio codice, non funziona e non mi da errori.Aggiornamento delle informazioni nel database

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's information to the Database. 
    SqlCommand cmd = new SqlCommand("UPDATE BUYER SET Name = @Name,Surname = @Surname,Email [email protected],CellNumber [email protected]", 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(); 
+1

Non correlato, ma raccomanderei EntityFramework invece di utilizzare l'API SQL con le tue query. – mirichan

+4

Hai tracciato la tua query con SQL Server Profiler? Qualche comando viene effettivamente inviato al server? Nota a margine: stai usando 'update' senza' where' condition - quindi aggiornerai ** tutte ** le righe nella tua tabella con gli stessi valori. –

+0

penso che op dovrebbe prima elaborare la parte "non funziona". in che modo esattamente "non funziona"? @op se non si ottiene alcun errore, allora forse il codice ** funziona **, ma non nel modo in cui lo si desidera. – Banana

risposta

2

Il problema è che si sta aprendo una connessione (che è corretta), ma che si sta aprendo di nuovo.

Ecco perché l'aggiornamento non si verifica. Inoltre, non è necessario chiudere la connessione in una dichiarazione finale. Ai fini di questo, non è richiesto.

Inoltre si sta eseguendo una dichiarazione di aggiornamento, quindi assicurarsi di fornire una condizione per aggiornare il record specifico.

Ho modificato il codice in modo appropriato, questo dovrebbe risolvere il problema: (provato e testato)


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 protected],CellNumber [email protected] WHERE Email [email protected]", 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 
    } 
} 

Fammi sapere l'esito.

0

Perché non provare questo con l'elaborazione asincrona impostata su falso. NB tutte le righe nella tabella verranno aggiornate in quanto non vi sono restrizioni nella query, ovvero nessuna clausola Where.

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's 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

perché non ci sono parametri SQL? – Sybren

+0

Perché in effetti non ne hai bisogno. Ad esempio aggiornamento Acquirente impostare Nome = 'Fred' dove Nome = 'Freddy' – Ewan

+0

così perché non sei obbligato a usarli e non li usi? Penso che sia meglio usarli. – Sybren

0

ho usato tentativo di cattura o il tuo codice:

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's information to the Database. 
     SqlCommand cmd = new SqlCommand("UPDATE BUYER SET Name = @Name,Surname = @Surname,Email [email protected],CellNumber [email protected]", 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(); 
    } 
} 

usano una traccia, controllare se con.open(); è vero, e se qualche cattura accade.

Problemi correlati