In my program, I have a section to insert information about a customer into an Access database. I get the error below at the .ExecuteNonQuery.
Message: System.Data.OleDb.OleDbException (0x80004005): Field 'Customer.Business_Phone' cannot be a zero-length string.
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
The problem I'm having, is that the Business_Phone does have a value in it. I added the msgbox to be sure, and it displays "9876543210", just like I entered. The database field is set to not allow zero length strings, because I want this to be a required field. What I don't understand is how it's getting that @BUSPH is a zero length string, when I set it to the value of the textbox, which is the same value that I print to the msgbox so I know that there is a value there. Also, I have a check before I add the field to the insert string to make sure I am not adding null values, so in theory it shouldn't be included in the input string if it is zero length, right? My code section is below. Any ideas what could be causing this error?
Dim custstr As String = "INSERT INTO [Customer] ("
Dim valuestr As String = ") VALUES ("
If txt_company.Text.ToString <> vbNullString Then
custstr += "Company"
valuestr += "@CMPNY"
End If
If txt_lastname.Text.ToString <> vbNullString Then
custstr += ", Last_Name"
valuestr += ", @LSTNM"
End If
If txt_firstname.Text.ToString <> vbNullString Then
custstr += ", First_Name"
valuestr += ", @FSTNM"
End If
If txt_busphone.Text.ToString <> vbNullString Then
custstr += ", Business_Phone"
valuestr += ", @BUSPH"
End If
If txt_homephone.Text.ToString <> vbNullString Then
custstr += ", Home_Phone"
valuestr += ", @HOMPH"
End If
If txt_mobilephone.Text.ToString <> vbNullString Then
custstr += ", Mobile_Phone"
valuestr += ", @MBLPH"
End If
If txt_fax.Text.ToString <> vbNullString Then
custstr += ", Fax"
valuestr += ", @FAXNM"
End If
If txt_email.Text.ToString <> vbNullString Then
custstr += ", Email"
valuestr += ", @EMAIL"
End If
custstr += valuestr + ")"
Dim custcmd As New OleDbCommand(custstr, con)
custcmd.Parameters.AddWithValue("@CMPNY", txt_company.Text.ToString)
custcmd.Parameters.AddWithValue("@LSTNM", OleDbType.VarChar).Value = txt_lastname.Text.ToString
custcmd.Parameters.AddWithValue("@FSTNM", OleDbType.VarChar).Value = txt_firstname.Text.ToString
custcmd.Parameters.AddWithValue("@BUSPH", OleDbType.VarChar).Value = txt_busphone.Text.ToString
custcmd.Parameters.AddWithValue("@HOMPH", OleDbType.VarChar).Value = txt_homephone.Text.ToString
custcmd.Parameters.AddWithValue("@MBLPH", OleDbType.VarChar).Value = txt_mobilephone.Text.ToString
custcmd.Parameters.AddWithValue("@FAXNM", OleDbType.VarChar).Value = txt_fax.Text.ToString
custcmd.Parameters.AddWithValue("@EMAIL", OleDbType.VarChar).Value = txt_email.Text.ToString
MsgBox("Business phone: " & txt_busphone.Text.ToString)
custcmd.ExecuteNonQuery()
thanks!