NilObjectException was not handled

I am trying to create a simple CRM. I created a Method “UpdateRow” with the following code and when I try to edit a client I get an “rs.edit” error. Please see the code below:

[code]Dim dr As New DatabaseRecord
dr = new DatabaseRecord
Dim sql As String

sql = “select LastName,FirstName,MiddleInitial,DOB,SocialSecurity,Phone,Email,Address,Address2,City,State,Zip,MedicareNumber,PartAEffDate,PartBEffDate,Company,PlanType,PlanName,EffDate,Premium from people where LastName = '” + lstPeople.Cell(lstPeople.ListIndex,0) + “’ and FirstName = '” + lstPeople.Cell(lstPeople.ListIndex,1) + “’ and MiddleInitial = '” + lstPeople.Cell(lstPeople.ListIndex,2) + “’ and DOB = '” + lstPeople.Cell(lstPeople.ListIndex,3) + “’ and SocialSecurity = '” + lstPeople.Cell(lstPeople.ListIndex,4) + “’ and Phone = '” + lstPeople.Cell(lstPeople.ListIndex,5) + “’ and Email = '” + lstPeople.Cell(lstPeople.ListIndex,6) + “’ and Address = '” + lstPeople.Cell(lstPeople.ListIndex,7) + “’ and Address2 = '” + lstPeople.Cell(lstPeople.ListIndex,8) + “’ and City = '” + lstPeople.Cell(lstPeople.ListIndex,9) + “’ and State = '” + lstPeople.Cell(lstPeople.ListIndex,10) + “’ and Zip '” + lstPeople.Cell(lstPeople.ListIndex,11) + “’ and MedicareNumber '” + lstPeople.Cell(lstPeople.ListIndex,12) + “’ and PartAEffDate '” + lstPeople.Cell(lstPeople.ListIndex,13) + “’ and PartBEffDate '” + lstPeople.Cell(lstPeople.ListIndex,14) + “’ and Company '” + lstPeople.Cell(lstPeople.ListIndex,15) + “’ and PlanType '” + lstPeople.Cell(lstPeople.ListIndex,16) + “’ and PlanName '” + lstPeople.Cell(lstPeople.ListIndex,17) + “’ and EffDate’” + lstPeople.Cell(lstPeople.ListIndex,18) + “‘and Premium’” + lstPeople.Cell(lstPeople.ListIndex,19) + “’”

Dim rs As RecordSet
rs = peopledb.SQLSelect(“people”)

rs.Edit

rs.field(“LastName”).StringValue = trim(txtLastName.text)
rs.field(“FirstName”).StringValue = trim(txtFirstName.text)
rs.field(“MiddleInitial”).StringValue = trim(txtMiddleInitial.text)
rs.field(“DOB”).StringValue = trim(txtDOB.text)
rs.field(“SocialSecurity”).StringValue = trim(txtSocial.text)
rs.field(“Phone”).StringValue = trim(txtPhone.text)
rs.field(“Email”).StringValue = trim(txtEmail.text)
rs.field(“Address1”).StringValue = trim(txtStreet1.text)
rs.field(“Address2”).StringValue = trim(txtStreet2.text)
rs.field(“City”).StringValue = trim(txtCity.text)
rs.field(“State”).StringValue = trim(txtState.text)
rs.field(“Zip”).StringValue = trim(txtZip.text)
rs.field(“MedicareNumber”).StringValue = trim(txtMedicareID.text)
rs.field(“PartAEffDate”).StringValue = trim(txtPartA.text)
rs.field(“PartBEffDate”).StringValue = trim(txtPartB.text)
rs.field(“Company”).StringValue = trim(txtCompany.text)
rs.field(“PlanType”).StringValue = trim(txtPlanType.text)
rs.field(“PlanName”).StringValue = trim(txtPlanName.text)
rs.field(“EffDate”).StringValue = trim(txtEffectiveDate.text)
rs.field(“Premium”).StringValue = trim(txtPremium.text)

rs.Update

peopledb.Commit

rs.Close

lstPeople.cell(lstPeople.ListIndex, 0) = trim(txtLastName.text)
lstPeople.cell(lstPeople.ListIndex, 1) = trim(txtFirstName.text)
lstPeople.cell(lstPeople.ListIndex, 2) = trim(txtMiddleInitial.text)
lstPeople.cell(lstPeople.ListIndex, 3) = trim(txtDOB.text)
lstPeople.cell(lstPeople.ListIndex, 4) = trim(txtSocial.text)
lstPeople.cell(lstPeople.ListIndex, 5) = trim(txtPhone.text)
lstPeople.cell(lstPeople.ListIndex, 6) = trim(txtEmail.text)
lstPeople.cell(lstPeople.ListIndex, 7) = trim(txtStreet1.text)
lstPeople.cell(lstPeople.ListIndex, 8) = trim(txtStreet2.text)
lstPeople.cell(lstPeople.ListIndex, 9) = trim(txtCity.text)
lstPeople.cell(lstPeople.ListIndex, 10) = trim(txtState.text)
lstPeople.cell(lstPeople.ListIndex, 11) = trim(txtZip.text)
lstPeople.cell(lstPeople.ListIndex, 12) = trim(txtMedicareID.text)
lstPeople.cell(lstPeople.ListIndex, 13) = trim(txtPartA.text)
lstPeople.cell(lstPeople.ListIndex, 14) = trim(txtPartB.text)
lstPeople.cell(lstPeople.ListIndex, 15) = trim(txtCompany.text)
lstPeople.cell(lstPeople.ListIndex, 16) = trim(txtPlanType.text)
lstPeople.cell(lstPeople.ListIndex, 17) = trim(txtPlanName.text)
lstPeople.cell(lstPeople.ListIndex, 18) = trim(txtEffectiveDate.text)
lstPeople.cell(lstPeople.ListIndex, 19) = trim(txtPremium.text)
[/code]

What am I doing wrong on this. I have another application that I created that uses the exact same code just less fields and works perfectly fine but this one is giving me an issue.

Don’t use RS.EDIT
Use proper a proper SQL Prepared Update Statement

(and use Prepared statement for SELECT also)
learning to use these tools will save you hours of headaches in the future

rs = peopledb.SQLSelect("people")

“people” isn’t a valid SQL statement, so the RecordSet that is returned will be nil.

I think you meant to write rs = peopledb.SQLSelect(sql)

Also, you should be checking for database errors:

if peopledb.Error then MsgBox "Database error: " + peopledb.ErrorMessage end

Ok I am fairly new to SQLiteDatabases @Dave S please explain a little more:

@Jared Feder where would I put the check database script and yes I copied an older script i have (sql) not (“people”)

Ok @Jared Feder I added the database check and received this error. Database error: near “’’”: syntax error

It looks like you’re missing the equals sign for every column starting with the zipcode.

That means there there is an error somewhere in your SQLSelect statement.
It will be hard to track down because of the complexity of the statement.

You would be better off going about it the way that @Dave S suggested by using a prepared statement.
This is greatly simplified from your code, but it would look something like this:

[code]dim ps as MySQLPreparedStatement = peopledb.Prepare(“SELECT LastName,FirstName FROM people WHERE LastName = ? and FirstName = ?”)

ps.BindType(0,MySQLPreparedStatement.MYSQL_TYPE_STRING)
ps.BindType(1,MySQLPreparedStatement.MYSQL_TYPE_STRING)

ps.Bind(0,lstPeople.Cell(lstPeople.ListIndex,0))
ps.Bind(1,lstPeople.Cell(lstPeople.ListIndex,1))

dim rs as RecordSet = ps.SQLSelect

if peopledb.Error then
MsgBox "Database error: " + peopledb.ErrorMessage
end

if rs <> nil then
// Do whatever you need to do with your RecordSet
end[/code]

I assumed that you are using mySQL, but if you are using another flavor you can change MySQLPreparedStatement to the appropriate type.

I am still at a loss. so my new code would look something like this: @Jared Feder

[code]dim ps as MySQLPreparedStatement = peopledb.Prepare(“SELECT LastName,FirstName,MiddleInitial,DOB,SocialSecurity,Phone,Email,Address,Address2,City,State,Zip,MedicareNumber,PartAEffDate,PartBEffDate,Company,PlanType,PlanName,EffDate,Premium from people where LastName = '” + lstPeople.Cell(lstPeople.ListIndex,0) + “’ and FirstName = '” + lstPeople.Cell(lstPeople.ListIndex,1) + “’ and MiddleInitial = '” + lstPeople.Cell(lstPeople.ListIndex,2) + “’ and DOB = '” + lstPeople.Cell(lstPeople.ListIndex,3) + “’ and SocialSecurity = '” + lstPeople.Cell(lstPeople.ListIndex,4) + “’ and Phone = '” + lstPeople.Cell(lstPeople.ListIndex,5) + “’ and Email = '” + lstPeople.Cell(lstPeople.ListIndex,6) + “’ and Address = '” + lstPeople.Cell(lstPeople.ListIndex,7) + “’ and Address2 = '” + lstPeople.Cell(lstPeople.ListIndex,8) + “’ and City = '” + lstPeople.Cell(lstPeople.ListIndex,9) + “’ and State = '” + lstPeople.Cell(lstPeople.ListIndex,10) + “’ and Zip = '” + lstPeople.Cell(lstPeople.ListIndex,11) + “’ and MedicareNumber = '” + lstPeople.Cell(lstPeople.ListIndex,12) + “’ and PartAEffDate = '” + lstPeople.Cell(lstPeople.ListIndex,13) + “’ and PartBEffDate = '” + lstPeople.Cell(lstPeople.ListIndex,14) + “’ and Company = '” + lstPeople.Cell(lstPeople.ListIndex,15) + “’ and PlanType = '” + lstPeople.Cell(lstPeople.ListIndex,16) + “’ and PlanName = '” + lstPeople.Cell(lstPeople.ListIndex,17) + “’ and EffDate = '” + lstPeople.Cell(lstPeople.ListIndex,18) + “’ and Premium = '” + lstPeople.Cell(lstPeople.ListIndex,19) + “’”)
)

ps.BindType(0,MySQLPreparedStatement.MYSQL_TYPE_STRING)
ps.BindType(1,MySQLPreparedStatement.MYSQL_TYPE_STRING)
ps.BindType(2,MySQLPreparedStatement.MYSQL_TYPE_STRING)
ps.BindType(3,MySQLPreparedStatement.MYSQL_TYPE_STRING)
ps.BindType(4,MySQLPreparedStatement.MYSQL_TYPE_STRING)
ps.BindType(5,MySQLPreparedStatement.MYSQL_TYPE_STRING)
ps.BindType(6,MySQLPreparedStatement.MYSQL_TYPE_STRING)
ps.BindType(7,MySQLPreparedStatement.MYSQL_TYPE_STRING)
ps.BindType(8,MySQLPreparedStatement.MYSQL_TYPE_STRING)
ps.BindType(9,MySQLPreparedStatement.MYSQL_TYPE_STRING)
ps.BindType(10,MySQLPreparedStatement.MYSQL_TYPE_STRING)
ps.BindType(11,MySQLPreparedStatement.MYSQL_TYPE_STRING)
ps.BindType(12,MySQLPreparedStatement.MYSQL_TYPE_STRING)
ps.BindType(13,MySQLPreparedStatement.MYSQL_TYPE_STRING)
ps.BindType(14,MySQLPreparedStatement.MYSQL_TYPE_STRING)
ps.BindType(15,MySQLPreparedStatement.MYSQL_TYPE_STRING)
ps.BindType(16,MySQLPreparedStatement.MYSQL_TYPE_STRING)
ps.BindType(17,MySQLPreparedStatement.MYSQL_TYPE_STRING)
ps.BindType(18,MySQLPreparedStatement.MYSQL_TYPE_STRING)
ps.BindType(19,MySQLPreparedStatement.MYSQL_TYPE_STRING)

ps.Bind(0,lstPeople.Cell(lstPeople.ListIndex,0))
ps.Bind(1,lstPeople.Cell(lstPeople.ListIndex,1))
ps.Bind(2,lstPeople.Cell(lstPeople.ListIndex,2))
ps.Bind(3,lstPeople.Cell(lstPeople.ListIndex,3))
ps.Bind(4,lstPeople.Cell(lstPeople.ListIndex,4))
ps.Bind(5,lstPeople.Cell(lstPeople.ListIndex,5))
ps.Bind(6,lstPeople.Cell(lstPeople.ListIndex,6))
ps.Bind(7,lstPeople.Cell(lstPeople.ListIndex,7))
ps.Bind(8,lstPeople.Cell(lstPeople.ListIndex,8))
ps.Bind(9,lstPeople.Cell(lstPeople.ListIndex,9))
ps.Bind(10,lstPeople.Cell(lstPeople.ListIndex,10))
ps.Bind(11,lstPeople.Cell(lstPeople.ListIndex,11))
ps.Bind(12,lstPeople.Cell(lstPeople.ListIndex,12))
ps.Bind(13,lstPeople.Cell(lstPeople.ListIndex,13))
ps.Bind(14,lstPeople.Cell(lstPeople.ListIndex,14))
ps.Bind(15,lstPeople.Cell(lstPeople.ListIndex,15))
ps.Bind(16,lstPeople.Cell(lstPeople.ListIndex,16))
ps.Bind(17,lstPeople.Cell(lstPeople.ListIndex,17))
ps.Bind(18,lstPeople.Cell(lstPeople.ListIndex,18))
ps.Bind(19,lstPeople.Cell(lstPeople.ListIndex,19))

dim rs as RecordSet = ps.SQLSelect

if peopledb.Error then
MsgBox "Database error: " + peopledb.ErrorMessage
end

if rs <> nil then
// Do whatever you need to do with your RecordSet
end
[/code]

And then also what about the update and commit and close commands: Getting the following error:

ClientsWindow.UpdateRow, line 1
Type mismatch error. Expected class MySQLPreparedStatement, but got interface PreparedSQLStatement
dim ps as MySQLPreparedStatement = peopledb.Prepare(“SELECT LastName,FirstName,MiddleInitial,DOB,SocialSecurity,Phone,Email,Address,Address2,City,State,Zip,MedicareNumber,PartAEffDate,PartBEffDate,Company,PlanType,PlanName,EffDate,Premium from people where LastName = '” + lstPeople.Cell(lstPeople.ListIndex,0) + “’ and FirstName = '” + lstPeople.Cell(lstPeople.ListIndex,1) + “’ and MiddleInitial = '” + lstPeople.Cell(lstPeople.ListIndex,2) + “’ and DOB = '” + lstPeople.Cell(lstPeople.ListIndex,3) + “’ and SocialSecurity = '” + lstPeople.Cell(lstPeople.ListIndex,4) + “’ and Phone = '” + lstPeople.Cell(lstPeople.ListIndex,5) + “’ and Email = '” + lstPeople.Cell(lstPeople.ListIndex,6) + “’ and Address = '” + lstPeople.Cell(lstPeople.ListIndex,7) + “’ and Address2 = '” + lstPeople.Cell(lstPeople.ListIndex,8) + “’ and City = '” + lstPeople.Cell(lstPeople.ListIndex,9) + “’ and State = '” + lstPeople.Cell(lstPeople.ListIndex,10) + “’ and Zip = '” + lstPeople.Cell(lstPeople.ListIndex,11) + “’ and MedicareNumber = '” + lstPeople.Cell(lstPeople.ListIndex,12) + “’ and PartAEffDate = '” + lstPeople.Cell(lstPeople.ListIndex,13) + “’ and PartBEffDate = '” + lstPeople.Cell(lstPeople.ListIndex,14) + “’ and Company = '” + lstPeople.Cell(lstPeople.ListIndex,15) + “’ and PlanType = '” + lstPeople.Cell(lstPeople.ListIndex,16) + “’ and PlanName = '” + lstPeople.Cell(lstPeople.ListIndex,17) + “’ and EffDate = '” + lstPeople.Cell(lstPeople.ListIndex,18) + “’ and Premium = '” + lstPeople.Cell(lstPeople.ListIndex,19) + “’”)

Please make things easier on us by using the code tags to format your code.

Sorry about the @Kem Tekinay still new at the forum

Still having issues with this code. I dont see what I am doing wrong please see the above.

Test this

dim columns(-1) as String = Array("LastName", "FirstName","MiddleInitial","DOB","SocialSecurity","Phone","Email","Address","Address2","City","State","Zip","MedicareNumber","PartAEffDate","PartBEffDate","Company","PlanType","PlanName","EffDate","Premium")

dim where(-1) as String
where.Append "LastName = ?"
where.Append "FirstName = ?"
where.Append "MiddleInitial = ?"
where.Append "DOB = ?"
where.Append "SocialSecurity = ?"
where.Append "Phone = ?"
where.Append "Email = ?"
where.Append "Address = ?"
where.Append "Address2 = ?"
where.Append "City = ?"
where.Append "State = ?"
where.Append "Zip = ?"
where.Append "MedicareNumber = ?"
where.Append "PartAEffDate = ?"
where.Append "PartBEffDate = ?"
where.Append "Company = ?"
where.Append "PlanType = ?"
where.Append "PlanName = ?"
where.Append "EffDate = ?"
where.Append "Premium = ?"

dim sql as String = "SELECT " + Join(columns, ",")
sql = sql + " from people where " + Join(where, " AND ")

dim ps as PreparedSQLStatement = peopledb.Prepare(sql)

When you prepare the statement, you need to replace the values with ? characters.

So instead of:

... where LastName = '" + lstPeople.Cell(lstPeople.ListIndex,0) + "' and FirstName = '" + lstPeople.Cell(lstPeople.ListIndex,1) + "' and MiddleInitial = '" + lstPeople.Cell(lstPeople.ListIndex,2) + "' and ...

You would write:

... where LastName = ? and FirstName = ? and MiddleInitial = ? and ...

Then the Bind method (which you got correct) will effectively insert your values in place of the ? characters in the order in which they appear.

@Jared Feder

This is updated Code

[code]
Dim dr As New DatabaseRecord
dr = new DatabaseRecord

Dim sql As String
sql = “SELECT LastName,FirstName,MiddleInitial,DOB,SocialSecurity,Phone,Email,Address,Address2,City,State,Zip,MedicareNumber,PartAEffDate,PartBEffDate,Company,PlanType,PlanName,EffDate,Premium from people where LastName = ? and FirstName = ? and MiddleInitial = ? and DOB = ? and SocialSecurity = ? and Phone = ? and Email = ? and Address = ? and Address2 = ? and City = ? and State = ? and Zip = ? and MedicareNumber = ? and PartAEffDate = ? and PartBEffDate = ? and Company = ? and PlanType = ? and PlanName = ? and EffDate = ? and Premium = ?;”)

dim ps as MySQLPreparedStatement
ps = peopledb.Prepare(sql)

ps.BindType(0,MySQLPreparedStatement.MySQL_Type_String)
ps.BindType(1,MySQLPreparedStatement.MySQL_Type_String)
ps.BindType(2,MySQLPreparedStatement.MySQL_Type_String)
ps.BindType(3,MySQLPreparedStatement.MySQL_Type_String)
ps.BindType(4,MySQLPreparedStatement.MySQL_Type_String)
ps.BindType(5,MySQLPreparedStatement.MySQL_Type_String)
ps.BindType(6,MySQLPreparedStatement.MySQL_Type_String)
ps.BindType(7,MySQLPreparedStatement.MySQL_Type_String)
ps.BindType(8,MySQLPreparedStatement.MySQL_Type_String)
ps.BindType(9,MySQLPreparedStatement.MySQL_Type_String)
ps.BindType(10,MySQLPreparedStatement.MySQL_Type_String)
ps.BindType(11,MySQLPreparedStatement.MySQL_Type_String)
ps.BindType(12,MySQLPreparedStatement.MySQL_Type_String)
ps.BindType(13,MySQLPreparedStatement.MySQL_Type_String)
ps.BindType(14,MySQLPreparedStatement.MySQL_Type_String)
ps.BindType(15,MySQLPreparedStatement.MySQL_Type_String)
ps.BindType(16,MySQLPreparedStatement.MySQL_Type_String)
ps.BindType(17,MySQLPreparedStatement.MySQL_Type_String)
ps.BindType(18,MySQLPreparedStatement.MySQL_Type_String)
ps.BindType(19,MySQLPreparedStatement.MySQL_Type_String)

ps.Bind(0,lstPeople.Cell(lstPeople.ListIndex,0))
ps.Bind(1,lstPeople.Cell(lstPeople.ListIndex,1))
ps.Bind(2,lstPeople.Cell(lstPeople.ListIndex,2))
ps.Bind(3,lstPeople.Cell(lstPeople.ListIndex,3))
ps.Bind(4,lstPeople.Cell(lstPeople.ListIndex,4))
ps.Bind(5,lstPeople.Cell(lstPeople.ListIndex,5))
ps.Bind(6,lstPeople.Cell(lstPeople.ListIndex,6))
ps.Bind(7,lstPeople.Cell(lstPeople.ListIndex,7))
ps.Bind(8,lstPeople.Cell(lstPeople.ListIndex,8))
ps.Bind(9,lstPeople.Cell(lstPeople.ListIndex,9))
ps.Bind(10,lstPeople.Cell(lstPeople.ListIndex,10))
ps.Bind(11,lstPeople.Cell(lstPeople.ListIndex,11))
ps.Bind(12,lstPeople.Cell(lstPeople.ListIndex,12))
ps.Bind(13,lstPeople.Cell(lstPeople.ListIndex,13))
ps.Bind(14,lstPeople.Cell(lstPeople.ListIndex,14))
ps.Bind(15,lstPeople.Cell(lstPeople.ListIndex,15))
ps.Bind(16,lstPeople.Cell(lstPeople.ListIndex,16))
ps.Bind(17,lstPeople.Cell(lstPeople.ListIndex,17))
ps.Bind(18,lstPeople.Cell(lstPeople.ListIndex,18))
ps.Bind(19,lstPeople.Cell(lstPeople.ListIndex,19))

dim rs as RecordSet = ps.SQLSelect

if peopledb.Error then
MsgBox "Database error: " + peopledb.ErrorMessage
end

if rs <> nil then
// Do whatever you need to do with your RecordSet
end[/code]

Now I am getting 2 errors:

Error 1:

ClientsWindow.UpdateRow, line 5 Syntax error sql = "SELECT LastName,FirstName,MiddleInitial,DOB,SocialSecurity,Phone,Email,Address,Address2,City,State,Zip,MedicareNumber,PartAEffDate,PartBEffDate,Company,PlanType,PlanName,EffDate,Premium from people where LastName = ? and FirstName = ? and MiddleInitial = ? and DOB = ? and SocialSecurity = ? and Phone = ? and Email = ? and Address = ? and Address2 = ? and City = ? and State = ? and Zip = ? and MedicareNumber = ? and PartAEffDate = ? and PartBEffDate = ? and Company = ? and PlanType = ? and PlanName = ? and EffDate = ? and Premium = ?;")
Error 2:

ClientsWindow.UpdateRow, line 8 Type mismatch error. Expected class MySQLPreparedStatement, but got interface PreparedSQLStatement ps = peopledb.Prepare(sql)

Error 1: You have a trailing ) after the string
Error 2: What is the type of the variable peopledb?

From reading your other thread, it looks like you are using a SQLite database, so its type should be SQLiteDatabase.
If that is the case, then change MySQLPreparedStatement to SQLitePreparedStatement and change your BindTypes to:

ps.BindType(0,SQLitePreparedStatement.SQLITE_TEXT)

@Jared Feder The entire source code is here: http://anderson-technology.com/xojo/MyClients.rar to make things a little easier to see what I am seeing

404 Not found

http://bestgacontracts.com/xojo/MyClients.rar just tested it and works now.

Ok, so I would suggest that you get rid of the database object that is a property of your window and connect to the database in code:

[code]dim peopledb as new SQLiteDatabase

// Get a reference to your sqlite database file however you would like
try
dim path as string = “path/to/your/sqlite/file”
dim dbFile as FolderItem = new FolderItem(path,FolderItem.PathTypeNative)
catch e as UnsupportedFormatException
MsgBox "Database file not found at: " + path
return
end

if dbFile = nil or not dbFile.Exists then
MsgBox “Database file not found”
return
end

peopledb.DatabaseFile = dbFile

if not peopledb.Connect then
MsgBox “Database connection error”
return
end[/code]