Click here to Skip to main content
15,901,505 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Imports System.Data.OleDb

Public Class Form1
	'Dim conn As New OleDb.OleDbConnection
	Dim cmd As OleDb.OleDbCommand = Nothing
	
	Dim conString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\OLAKAY\Documents\New folder\DatabaseEneka.accdb"
	


	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Me.Text = "Nominal Role Update Form"

	End Sub

	Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
		'Declear the Variables and Data Types 

		Dim Sn, CompNo, SGL As String
		Dim DFA, DOB, DPA, DPS As String 'dateOfPresentAppointment
		Dim StaffName, IDno, HomeTown, StateofOrigin, sex, LGA, Qualification, SubjectS, SubjectT, Rank, LastSchool, Designation, Remark As String


		Sn = txtSerial.Text
		IDno = txtID.Text
		CompNo = txtComputerNumber.Text
		SGL = txtSGL.Text
		DOB = txtDOB.Text
		DFA = txtDfa.Text
		LGA = txtLGA.Text
		DPA = txtDateOfPresent.Text
		DPS = txtDatePostedTo.Text
		StaffName = txtStaffName.Text
		HomeTown = txtHomeTown.Text
		StateofOrigin = txtStateOfOrigin.Text
		sex = txtSex.Text
		Qualification = txtQualification.Text
		SubjectS = txtSubjectS.Text
		SubjectT = txtSubjectT.Text
		Remark = txtRemarks.Text
		Rank = txtRank.Text
		Designation = txtDelegation.Text
		LastSchool = txtLastSchooll.Text
		Try




			Dim conn = New OleDbConnection
			conn.ConnectionString = conString
			cmd.Connection = conn
			conn.Open()

			cmd.CommandText = "INSERT INTO StaffInfo(ID,StaffName,DateofBirth,HomeTown,StateofOrigin,LGA,Sex,DateOfFirstAppointment,ComputerNumber,IDNumber,QualificationWithDates,SubjectSpecialized,SubjectTaught,DateofPresentAppointment,Rank,Designation,DatePostedToSchool,LastSchoolServed,Remarks)VALUE  ('" & IDno & "','" & StaffName & "''" & DOB & "''" & HomeTown & "''" & StateofOrigin & "''" & DOB & "')"
			cmd.ExecuteNonQuery()
			conn.Close()
		Catch ex As Exception
			MsgBox(ex.Message)
			
			'			
			MsgBox("Data Saved")
		End Try
		

	


	End Sub

	Private Function VALUES(ByVal p1 As String, ByVal p2 As Object) As String
		Throw New NotImplementedException
	End Function

	Private Function VALUES(ByVal p1 As String) As String
		Throw New NotImplementedException
	End Function

End Class
Posted
Updated 19-Jan-15 10:14am
v2
Comments
PIEBALDconsult 19-Jan-15 16:16pm    
Do_not_use_string_concatenation_to_specify_values_! Use a parameterized statement. Every time.

And it looks like you never instantiate cmd.
Sergey Alexandrovich Kryukov 19-Jan-15 16:22pm    
Right. I credited your comment, both of your ideas in my answer where I tried to explain both aspects (the exception and SQL injection) where I tried to explain it in detail.
—SA

1 solution

PIEBALDconsult gave you a couple of good ideas in his comment to the question.

You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx[^],
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx[^].

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].


Now, let's address the problem of the way you compose your query. Not only repeated string concatenation is inefficient (because strings are immutable; do I have to explain why it makes repeated concatenation bad?), but there is way more important issue: it opens the doors to a well-known exploit called SQL injection.

This is how it works: http://xkcd.com/327[^].

What to do? Just read about this problem and the main remedy: parametrized statements: http://en.wikipedia.org/wiki/SQL_injection[^].

With ADO.NET, use this: http://msdn.microsoft.com/en-us/library/ff648339.aspx[^].

Please see my past answers for some more detail:
EROR IN UPATE in com.ExecuteNonQuery();[^],
hi name is not displaying in name?[^].

Good luck,
—SA
 
Share this answer
 
v3
Comments
joshrduncan2012 19-Jan-15 16:40pm    
Nice! My 5.
Sergey Alexandrovich Kryukov 19-Jan-15 16:42pm    
Thank you very much.
—SA
Thanks7872 20-Jan-15 1:38am    
+5
Sergey Alexandrovich Kryukov 20-Jan-15 2:04am    
Thank you, Rohan.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900