Problemi su update

martedì 03 maggio 2005 - 17.06

wolf Profilo | Junior Member

ciao a tutti, ho una tabella con i seguenti campi:
idnome - integer
cognome - varchar
nome - varcharù
indiriz - varchar
idcitta - integer
codfisc
piva
e la seguente procedura di aggiornamento:
Dim strConnSql As String = ConfigurationSettings.AppSettings("strConnSql")
Dim connSql As New SqlConnection(strConnSql)
Dim strUpd As String = "UPDATE nome SET nome=@nome, cognome=@cognome, indiriz=@indiriz, " & _
"idcitta=@idcitta, piva=@piva, codfisc=@codfisc WHERE idnome='" & txtID.Text & "'"
Dim cmd1 As New SqlCommand(strUpd, connSql)
cmd1.Parameters.Add("@nome", txtNome.Text)
cmd1.Parameters.Add("@cognome", txtCognome.Text)
cmd1.Parameters.Add("@indiriz", txtVia.Text)
cmd1.Parameters.Add("@idcitta", cboCit.SelectedValue)
cmd1.Parameters.Add("@piva", txtPIva.Text)
cmd1.Parameters.Add("@codfisc", txtCF.Text)
connSql.Open()
cmd1.ExecuteNonQuery()
connSql.Close()
ma la procedura mi provoca il seguente errore:
String or binary data would be truncated. The statement has been terminated.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: String or binary data would be truncated. The statement has been terminated.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[SqlException: String or binary data would be truncated.
The statement has been terminated.]
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) +742
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +194
registro.nome_mod.btnUpd_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\registro\nome_mod.aspx.vb:124
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1292

Da cosa può dipendere?

Brainkiller Profilo | Guru

Ciao,
dovrebbe essere abbastanza semplice.
Dal fatto che il nuovo valore inserito e che deve rimpiazzare il vecchio occupa più caratteri di quelli che accetta il campo del database.

Esempio se il campo Title è un nvarchar(10) e tu cerchi di inserire "ABCDEFGHILM" lui va in errore perchè 11 caratteri non ci stanno nel posto da 10.

ciao
david

wolf Profilo | Junior Member

grazie Brainkiller ma non ho capito una cosuccia.
Nella tabella di sqlserver il campo idcitta è di tipo integer ed ha lunghezza 4.
La dropdownlist sulla webform recupera i dati da una tabella 'citta' dove ci sono i campi idcitta (integer-4) e citta (varchar-50) e stabilisco che:
cboCit.DataTextField = "citta"
cboCit.DataValueField = "idcitta"
In fase di update cambio il valore iniziale (int) con un altro Int(4). Penso, quindi che non sia questo l'errore ma non ne sono sicuro.
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5