question

SparkerSpark-0420 avatar image
0 Votes"
SparkerSpark-0420 asked ErlandSommarskog answered

Code, Object reference not set to an instance of an object

Below code is throwing the error : Object reference not set to an instance of an object

Well i am unable to figure out what could be throwing this error. Kindly help.

Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click

     Try
         Dim conn As New SqlConnection
         conn.ConnectionString = ConfigurationManager.ConnectionStrings("connStr").ConnectionString

         Dim cmd As New SqlCommand
         cmd.Connection = conn
         conn.Open()


         'INSERT INTO patientDetails & implantDetails
         cmd.CommandText = "INSERT INTO patientDetails (ptFirstName,ptLastName,ptAddress,ptDistrict,ptState,ptPinCode,ptPhoneNumber,ptDOB,ptGender,ptNote,ptDateOfReg,ptPhysician,ptHospital) VALUES (@ptFirstName,@ptLastName,@ptAddress,@ptDistrict,@ptState,@ptPinCode,@ptPhoneNumber,@ptDOB,@ptGender,@ptNote,@ptDateOfReg,@ptPhysician,@ptHospital)"
         'TO PREVENT OVER WRITING OF INSERT STATEMENT
         'SIX ROWS GET INSTERTED INTO (implantDetails) FROM SINGLE INSERT STATEMENT 
         cmd.CommandText = cmd.CommandText + " INSERT INTO implantDetails (ptID,productNameID,impSerial,impDate) VALUES (@ptID,@productNameID,@impSerial,@impDate) , (@ptID1,@productNameID1,@impSerial1,@impDate1), (@ptID2,@productNameID2,@impSerial2,@impDate2), (@ptID3,@productNameID3,@impSerial3,@impDate3)"


         '=============================================================================================================
         'GET THE VALUE OF PRIMARY KEY (IDENTITY) OF patientDetails
         'GETS THE PRIMARY KEY OF LAST INSERTED ROW INTO THE TABLE
         Dim strSQL As String
         Dim priKey As Integer
         strSQL = "SELECT IDENT_CURRENT('patientDetails')"
         'Create new connection
         Dim conn1 As New SqlConnection(ConfigurationManager.ConnectionStrings("connStr").ConnectionString)

         Dim cmd1 As New SqlCommand(strSQL, conn1)
         cmd1.Connection.Open()
         'Save primary key into 'priKey'
         priKey = cmd1.ExecuteScalar().ToString()
         'PRIMARY KEY OF LAST INSERTED ROW IS INCREMENTED BY 1 (to get current the value of primary key generated)
         priKey = priKey + 1
         cmd1.Connection.Close()
         '=============================================================================================================

         cmd.Parameters.Add("@ptFirstName", SqlDbType.VarChar).Value = txtFirstName.Text
         cmd.Parameters.Add("@ptLastName", SqlDbType.VarChar).Value = txtLastName.Text
         cmd.Parameters.Add("@ptAddress", SqlDbType.VarChar).Value = txtPatientAddress.Text
         cmd.Parameters.Add("@ptDistrict", SqlDbType.VarChar).Value = DD_District.SelectedItem.Text
         cmd.Parameters.Add("@ptState", SqlDbType.VarChar).Value = txtState.Text
         cmd.Parameters.Add("@ptPinCode", SqlDbType.VarChar).Value = txtPatientPinCode.Text
         cmd.Parameters.Add("@ptPhoneNumber", SqlDbType.VarChar).Value = txtPatientPhone.Text
         cmd.Parameters.Add("@ptDOB", SqlDbType.VarChar).Value = txtPatientDOB.Text.ToString
         cmd.Parameters.Add("@ptGender", SqlDbType.VarChar).Value = DDPatientGender.SelectedItem.Text
         cmd.Parameters.Add("@ptNote", SqlDbType.VarChar).Value = txtOtherInfo.Text
         cmd.Parameters.Add("@ptDateOfReg", SqlDbType.Date).Value = DateTime.Now.ToString
         cmd.Parameters.Add("@ptPhysician", SqlDbType.VarChar).Value = RadDD_Physican1.SelectedItem.Text + " , " + txt_Physician2.Text
         cmd.Parameters.Add("@ptHospital", SqlDbType.VarChar).Value = RadDD_HospitalName.SelectedItem.Text

           

         '================= TABLE implantDetails  =====================================================

         'FOREIGN KEY FOR 'implantDetails' -- PatientID
         ' priKey --> primary key generated for patientDetails



         cmd.Parameters.Add("@ptID", SqlDbType.Int).Value = priKey
         
         cmd.Parameters.Add("@impDate", SqlDbType.Date).Value = RadDatePicker1.SelectedDate.Value
         cmd.Parameters.Add("@productNameID", SqlDbType.NVarChar).Value = RadDD_PacemakerModel.SelectedValue
         cmd.Parameters.Add("@impSerial", SqlDbType.VarChar).Value = txtPaceSerial.Text

         cmd.Parameters.Add("@ptID1", SqlDbType.Int).Value = priKey
         cmd.Parameters.Add("@impDate1", SqlDbType.Date).Value = RadDatePicker1.SelectedDate.Value
         cmd.Parameters.Add("@productNameID1", SqlDbType.NVarChar).Value = RadDD_Lead1.SelectedValue
         cmd.Parameters.Add("@impSerial1", SqlDbType.VarChar).Value = txtLead1Serial.Text

         cmd.Parameters.Add("@ptID2", SqlDbType.Int).Value = priKey
         cmd.Parameters.Add("@impDate2", SqlDbType.Date).Value = RadDatePicker1.SelectedDate.Value
         cmd.Parameters.Add("@productNameID2", SqlDbType.NVarChar).Value = RadDD_Lead2.SelectedValue
         cmd.Parameters.Add("@impSerial2", SqlDbType.VarChar).Value = txtLead2Serial.Text

          
         cmd.Parameters.Add("@ptID3", SqlDbType.Int).Value = priKey
         cmd.Parameters.Add("@impDate3", SqlDbType.Date).Value = RadDatePicker1.SelectedDate.Value
         cmd.Parameters.Add("@productNameID3", SqlDbType.NVarChar).Value = RadDD_Lead3.SelectedValue
         cmd.Parameters.Add("@impSerial3", SqlDbType.VarChar).Value = txtLead3Serial.Text

         cmd.ExecuteNonQuery()
         conn.Close()


           


         'Confirmation Message
         lblStatus.ForeColor = Drawing.Color.Green
         lblStatus.Font.Bold = True
         lblStatus.Text = "Implant Details Updated Successfully "

     Catch ex As Exception

         lblStatus.ForeColor = Drawing.Color.Red
         lblStatus.Font.Bold = True
         lblStatus.Text = ex.Message

     End Try

 End Sub
sql-server-generalwindows-forms
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Well i am unable to figure out what could be throwing this error. Kindly help.

Debugg your code to see on which code line the error is raised: Visual Studio debugger documentation


0 Votes 0 ·

1 Answer

ErlandSommarskog avatar image
0 Votes"
ErlandSommarskog answered

I can't say why you get "Object reference not set to an instance of an object". You will need to debug it to see on which line it occurs.

However, I happened to notice this line:

"SELECT IDENT_CURRENT('patientDetails')"

I don't think this is very good, as this could lead to problems if two users are running the this code simultaneously. Both users will get the same value in the prikey variable, but only one of them (at most) will insert a row with that value. You need to capture the value after the fact with scope_identity.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.