2010-03-31 5 views
0
string ConnectionString = WebConfigurationManager.ConnectionStrings["dbnameConnectionString"].ConnectionString; 
SqlConnection myConnection = new SqlConnection(ConnectionString); 

myConnection.Open(); 
try 
{ 
    string qry = "UPDATE customers SET [email protected] WHERE cid=1"; 
    SqlCommand insertQuery = new SqlCommand(qry, myConnection); 
    insertQuery.Parameters.Add(new SqlParameter("@firstname", txtFirstname.Text)); 
    insertQuery.ExecuteNonQuery(); 

    myConnection.Close(); 
} 
catch (Exception ee) 
{ 

} 

Une suggestion?asp.net ne peut pas mettre à jour la base de données

+0

Obtenez-vous une exception? –

+0

Non .............. – tom

+0

Donc, il n'y a pas de cid = 1 dans votre db? –

Répondre

0

Un peu de nettoyage de votre code, vous pouvez vous essayer (vous devez disposer des objets IDisposable):

string ConnectionString = // ... 
using (var connection = new SqlConnection(ConnectionString)) 
using (var command = connection.CreateCommand()) 
{ 
    connection.Open(); 
    command.CommandText = "UPDATE customers SET firstname = @firstname WHERE cid=1"; 
    command.Parameters.AddWithValue("@firstname", txtFirstname.Text); 
    var rowsUpdated = command.ExecuteNonQuery(); 
} 
+0

ne fonctionne toujours pas mais merci. – tom

+0

Vous allez devoir être un peu plus précis que "ne fonctionne pas" afin que nous puissions vous aider. –

+0

Quelle est la valeur de 'rowsUpdated' après l'exécution de cette instruction? –

Questions connexes