Wednesday, March 7, 2012

Registration form problem. String or binary data would be truncated

I have created a registration form. It works fine IF ALL fields are filled. However, all fields are not required. When I test the registration page and leave a field blank, I receive the following message:

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

Stack Trace:

[SqlException: String or binary data would be truncated.
The statement has been terminated.]

Even if I leave a validated field blank, I receive the same message instead of the required field validation control error message.

Here is the code for the registration page. I use web matrix to create the code as directed in the tutorial.

Can anyone help a newbie?

Function AddMember( _
ByVal firstName As String, _
ByVal lastName As String, _
ByVal streetAddress1 As String, _
ByVal streetAddress2 As String, _
ByVal memCity As String, _
ByVal state As String, _
ByVal zipCode As String, _
ByVal primAreaCode As String, _
ByVal primPhone As String, _
ByVal primExt As String, _
ByVal secAreaCode As String, _
ByVal secPhone As String, _
ByVal secExt As String, _
ByVal memEmail As String, _
ByVal memUserID As String, _
ByVal memPassword As String, _
ByVal secretQuestion As String, _
ByVal secretAnswer As String, _
ByVal memBirthMonth As String, _
ByVal memBirthDay As String, _
ByVal memBirthYear As String) As Integer
Dim connectionString As String = "server='(local)'; trusted_connection=true; database='Members'"
Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)

Dim queryString As String = "INSERT INTO [Members] ([FirstName], [LastName], [StreetAddress1], [StreetAddress2"& _
"], [MemCity], [State], [ZipCode], [PrimAreaCode], [PrimPhone], [PrimExt], [SecAr"& _
"eaCode], [SecPhone], [SecExt], [MemEmail], [MemUserID], [MemPassword], [SecretQu"& _
"estion], [SecretAnswer], [MemBirthMonth], [MemBirthDay], [MemBirthYear]) VALUES "& _
"(@.FirstName, @.LastName, @.StreetAddress1, @.StreetAddress2, @.MemCity, @.State, @.Zip"& _
"Code, @.PrimAreaCode, @.PrimPhone, @.PrimExt, @.SecAreaCode, @.SecPhone, @.SecExt, @.Me"& _
"mEmail, @.MemUserID, @.MemPassword, @.SecretQuestion, @.SecretAnswer, @.MemBirthMonth"& _
", @.MemBirthDay, @.MemBirthYear)"
Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

Dim dbParam_firstName As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_firstName.ParameterName = "@.FirstName"
dbParam_firstName.Value = firstName
dbParam_firstName.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_firstName)
Dim dbParam_lastName As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_lastName.ParameterName = "@.LastName"
dbParam_lastName.Value = lastName
dbParam_lastName.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_lastName)
Dim dbParam_streetAddress1 As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_streetAddress1.ParameterName = "@.StreetAddress1"
dbParam_streetAddress1.Value = streetAddress1
dbParam_streetAddress1.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_streetAddress1)
Dim dbParam_streetAddress2 As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_streetAddress2.ParameterName = "@.StreetAddress2"
dbParam_streetAddress2.Value = streetAddress2
dbParam_streetAddress2.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_streetAddress2)
Dim dbParam_memCity As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_memCity.ParameterName = "@.MemCity"
dbParam_memCity.Value = memCity
dbParam_memCity.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_memCity)
Dim dbParam_state As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_state.ParameterName = "@.State"
dbParam_state.Value = state
dbParam_state.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_state)
Dim dbParam_zipCode As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_zipCode.ParameterName = "@.ZipCode"
dbParam_zipCode.Value = zipCode
dbParam_zipCode.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_zipCode)
Dim dbParam_primAreaCode As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_primAreaCode.ParameterName = "@.PrimAreaCode"
dbParam_primAreaCode.Value = primAreaCode
dbParam_primAreaCode.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_primAreaCode)
Dim dbParam_primPhone As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_primPhone.ParameterName = "@.PrimPhone"
dbParam_primPhone.Value = primPhone
dbParam_primPhone.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_primPhone)
Dim dbParam_primExt As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_primExt.ParameterName = "@.PrimExt"
dbParam_primExt.Value = primExt
dbParam_primExt.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_primExt)
Dim dbParam_secAreaCode As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_secAreaCode.ParameterName = "@.SecAreaCode"
dbParam_secAreaCode.Value = secAreaCode
dbParam_secAreaCode.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_secAreaCode)
Dim dbParam_secPhone As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_secPhone.ParameterName = "@.SecPhone"
dbParam_secPhone.Value = secPhone
dbParam_secPhone.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_secPhone)
Dim dbParam_secExt As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_secExt.ParameterName = "@.SecExt"
dbParam_secExt.Value = secExt
dbParam_secExt.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_secExt)
Dim dbParam_memEmail As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_memEmail.ParameterName = "@.MemEmail"
dbParam_memEmail.Value = memEmail
dbParam_memEmail.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_memEmail)
Dim dbParam_memUserID As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_memUserID.ParameterName = "@.MemUserID"
dbParam_memUserID.Value = memUserID
dbParam_memUserID.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_memUserID)
Dim dbParam_memPassword As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_memPassword.ParameterName = "@.MemPassword"
dbParam_memPassword.Value = memPassword
dbParam_memPassword.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_memPassword)
Dim dbParam_secretQuestion As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_secretQuestion.ParameterName = "@.SecretQuestion"
dbParam_secretQuestion.Value = secretQuestion
dbParam_secretQuestion.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_secretQuestion)
Dim dbParam_secretAnswer As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_secretAnswer.ParameterName = "@.SecretAnswer"
dbParam_secretAnswer.Value = secretAnswer
dbParam_secretAnswer.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_secretAnswer)
Dim dbParam_memBirthMonth As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_memBirthMonth.ParameterName = "@.MemBirthMonth"
dbParam_memBirthMonth.Value = memBirthMonth
dbParam_memBirthMonth.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_memBirthMonth)
Dim dbParam_memBirthDay As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_memBirthDay.ParameterName = "@.MemBirthDay"
dbParam_memBirthDay.Value = memBirthDay
dbParam_memBirthDay.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_memBirthDay)
Dim dbParam_memBirthYear As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_memBirthYear.ParameterName = "@.MemBirthYear"
dbParam_memBirthYear.Value = memBirthYear
dbParam_memBirthYear.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_memBirthYear)

Dim rowsAffected As Integer = 0
dbConnection.Open
Try
rowsAffected = dbCommand.ExecuteNonQuery
Finally
dbConnection.Close
End Try

Return rowsAffected

End Function

Do u have all the fields in the table defined as "not null"?|||no. some fields allow nulls.

No comments:

Post a Comment