Friday, February 24, 2012

Error "System.NullReferenceException" on Update

Hello Everyone,

I am getting this error "System.NullReferenceException: Object reference not set to an instance of an object." when i try to run the update query as in my following code.

When the new record condition is true then the code runs absolutely fine and inserts a new record to the database. However, when it goes in the edit mode to update the record it gives an error at '## ERROR POINT FOR THE UPDATE QUERY'. Why is this happening. i have attached the portion of my code below for refernce. Can someone please help me with this? Where am i going wrong. Seems like update query has a problem.

'-------------------------------
If mode = "edit" Then

insertSql = "update [Customer Info] set [Company Name]=@.CompanyName, [Business Type]=@.BusinessSector, [Address Line 1]=@.AddressLine1, [Address Line 2]=@.AddressLine2, [Address Line 3] = @.AddressLine3, [Town] = @.Town, [County]= @.County, [Post Code]=@.PostCode, [Country]=@.Country, [Telephone]=@.Telephone, [Fax]=@.Fax, [Email Address]=@.Email, [Contact Person]=@.ContactPerson, [Job Title]=@.JobTitle, [Website Address]=@.WebsiteAddress Where [Customer ID] = " & Request.QueryString("custid")

else

insertSql = "insert into [Customer Info] ([Customer ID], [Company Name], [Business Type], [Address Line 1], [Address Line 2], [Address Line 3], [Town], [County], [Post Code], [Country], [Telephone], [Fax], [Email Address], [Contact Person], [Job Title],[Website Address])"
insertSql = insertSql & " values (@.CustomerIDSq, @.CompanyName, @.BusinessSector, @.AddressLine1, @.AddressLine2, @.AddressLine3, @.Town, @.County, @.PostCode, @.Country, @.Telephone, @.Fax, @.Email, @.ContactPerson, @.JobTitle, @.WebsiteAddress)"

end if

objConn.open()
objCommand.CommandText = insertSql '## ERROR POINT FOR THE UPDATE QUERY
objCommand.Connection = objConn

objCommand.Parameters.Add("@.CustomerIDSq", SqlDbType.Int).Value = CustomerID
objCommand.Parameters.Add("@.CompanyName", SqlDbType.VarChar).Value = Trim(txtCompanyName.Text)
objCommand.Parameters.Add("@.BusinessSector", SqlDbType.Int).Value = BusinessSector
objCommand.Parameters.Add("@.AddressLine1", SqlDbType.VarChar).Value = Trim(txtAddressLine1.Text)
objCommand.Parameters.Add("@.AddressLine2", SqlDbType.VarChar).Value = Trim(txtAddressLine2.Text)
objCommand.Parameters.Add("@.AddressLine3", SqlDbType.VarChar).Value = Trim(txtAddressLine3.Text)
objCommand.Parameters.Add("@.Town", SqlDbType.VarChar).Value = Trim(txtTown.Text)
objCommand.Parameters.Add("@.County", SqlDbType.VarChar).Value = Trim(txtCounty.Text)
objCommand.Parameters.Add("@.PostCode", SqlDbType.VarChar).Value = Trim(txtPostCode.Text)
objCommand.Parameters.Add("@.Country", SqlDbType.VarChar).Value = Trim(txtCountry.Text)
objCommand.Parameters.Add("@.Telephone", SqlDbType.VarChar).Value = Trim(txtTelephone.Text)
objCommand.Parameters.Add("@.Fax", SqlDbType.VarChar).Value = Trim(txtFax.Text)
objCommand.Parameters.Add("@.Email", SqlDbType.VarChar).Value = Trim(txtEmail.Text)
objCommand.Parameters.Add("@.ContactPerson", SqlDbType.VarChar).Value = Trim(txtContactPerson.Text)
objCommand.Parameters.Add("@.JobTitle", SqlDbType.VarChar).Value = Trim(txtJobTitle.Text)
objCommand.Parameters.Add("@.WebsiteAddress", SqlDbType.VarChar).Value = Trim(txtWebsiteAddress.Text)

objCommand.executeNonQuery()
objConn.close()

'-------------------------------

Thanks,
SimShow us where you instantiate (new) the objCommand. That is likely the problem.|||Yea, that was the problem. i was not instantiating the command object. Now i did it and it is running very fine.

Thanks You very much, how these small things gets me.. i dunno.. :o.
Sim

No comments:

Post a Comment