Object reference not set to an instance of an object.

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 2
Reputation: zaimah is an unknown quantity at this point 
Solved Threads: 0
zaimah zaimah is offline Offline
Newbie Poster

Object reference not set to an instance of an object.

 
0
  #1
May 18th, 2009
i have been google it for answers but still cannot find the main problem.. when i debug, this error msg coming from exception section, but before that, when i put my cursor at conn (Button1_Click) to see the value it shows conn = nothing. But conn in other function got values.. i have declare conn as

Public in a module. Public conn = New SqlClient.SqlConnection("Data Source=KOMPZAI;User ID= sydm;Password=sydm;Initial Catalog=SPGAlatihan;Persist Security Info=True;")

i can see all the values but cannot add to the table.

  1.  
  2. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  3.  
  4. Dim dateNow As String
  5. dateNow = DateTime.Now.ToString("yyMMdd")
  6.  
  7. If CoopID.Text.Length > 0 And CoopName.Text.Length > 0 And RPerson.Text.Length > 0 And Problem.Text.Length > 0 And Solution.Text.Length > 0 And CSName.Text.Length > 0 And Status.Text.Length > 0 Then
  8.  
  9. Try
  10. Dim strSQL As String = "INSERT INTO CSLog (CoopID, CoopName, CoopName1, RPerson, Phone, Problem, Solution, CSName, Status) VALUES (@CoopID, @CoopName, @CoopName1, @RPerson, @Phone, @Problem, @Solution, @CSName, @Status)"
  11.  
  12. Dim cmd As New System.Data.SqlClient.SqlCommand(strSQL, conn)
  13.  
  14. cmd.Parameters.Add(New SqlClient.SqlParameter("@DocID", SqlDbType.Char, 15))
  15. cmd.Parameters("@DocID").Value = dateNow + CoopID.Text + GetRunNo().ToString("D4")
  16.  
  17. cmd.Parameters.Add(New SqlClient.SqlParameter("@CoopID", SqlDbType.Char, 5))
  18. cmd.Parameters("@CoopID").Value = CoopID.Text
  19.  
  20. cmd.Parameters.Add(New SqlClient.SqlParameter("@CoopName", SqlDbType.NVarChar, 50))
  21. cmd.Parameters("@CoopName").Value = CoopName.Text
  22.  
  23. cmd.Parameters.Add(New SqlClient.SqlParameter("@CoopName1", SqlDbType.NVarChar, 50))
  24. cmd.Parameters("@CoopName1").Value = CoopName1.Text
  25.  
  26. cmd.Parameters.Add(New SqlClient.SqlParameter("@RPerson", SqlDbType.Char, 20))
  27. cmd.Parameters("@RPerson").Value = RPerson.Text
  28.  
  29. cmd.Parameters.Add(New SqlClient.SqlParameter("@Phone", SqlDbType.Char, 15))
  30. cmd.Parameters("@Phone").Value = Phone.Text
  31.  
  32. cmd.Parameters.Add(New SqlClient.SqlParameter("@Problem", SqlDbType.NVarChar, 100))
  33. cmd.Parameters("@Problem").Value = Problem.Text
  34.  
  35. cmd.Parameters.Add(New SqlClient.SqlParameter("@Solution", SqlDbType.NVarChar, 100))
  36. cmd.Parameters("@Solution").Value = Solution.Text
  37.  
  38. cmd.Parameters.Add(New SqlClient.SqlParameter("@CSName", SqlDbType.Char, 20))
  39. cmd.Parameters("@CSName").Value = CSName.Text
  40.  
  41. cmd.Parameters.Add(New SqlClient.SqlParameter("@Status", SqlDbType.Char, 10))
  42. cmd.Parameters("@Status").Value = Status.Text
  43.  
  44.  
  45. cmd.Connection.Open()
  46. cmd.ExecuteNonQuery()
  47. cmd.Connection.Close()
  48. conn.Close()
  49.  
  50. Catch ex As Exception
  51.  
  52. Label6.Text = "PLEASE FILL IN ALL THE FIELD LISTED."
  53. Label6.Visible = True
  54.  
  55. Finally
  56.  
  57.  
  58. Label6.Text = "Cussecfully Saved ."
  59. Label6.Visible = True
  60. Clear()
  61.  
  62. End Try
  63.  
  64.  
  65. End If
  66.  
  67. End Sub
  68.  
  69. Private Function GetRunNo() As Long
  70.  
  71. Dim ds As DataSet = Nothing
  72.  
  73. Try
  74. Dim sql = "SELECT TOP 1 tarikh1, run_no from runNo"
  75. ' Dim comm = New SqlClient.SqlDataAdapter(sql, conn)
  76.  
  77. Dim comm = New SqlClient.SqlCommand(sql, conn)
  78.  
  79. Dim da = New SqlClient.SqlDataAdapter(comm)
  80.  
  81. ds = New DataSet
  82.  
  83. da.Fill(ds, "runNo")
  84.  
  85. Dim lastDateUsed As Date = ds.Tables(0).Rows(0).Item("tarikh1")
  86. Dim lastRunNoUsed As Long = ds.Tables(0).Rows(0).Item("run_no")
  87.  
  88. If DateTime.Now.Date > lastDateUsed.Date Then
  89. ' Chnage date to today's date
  90. lastDateUsed = DateTime.Now.Date
  91. ' reset runNo to 1
  92. lastRunNoUsed = 1
  93. Else
  94. ' Increment runNo
  95. lastRunNoUsed = lastRunNoUsed + 1
  96. End If
  97.  
  98. ' Update database
  99. UpdateRunNo(lastDateUsed, lastRunNoUsed)
  100.  
  101. Return lastRunNoUsed
  102. comm = Nothing
  103. Catch ex As Exception
  104. Label5.Visible = True
  105. Label5.Text = "Error. " & ex.Message
  106. Finally
  107. conn = Nothing
  108. ds = Nothing
  109. End Try
  110. End Function
  111.  
  112.  
  113. Private Sub UpdateRunNo(ByVal lastDateUsed As DateTime, ByVal lastRunNoUsed As Long)
  114. Dim comm As SqlClient.SqlCommand = Nothing
  115.  
  116. Try
  117. Dim sql = "UPDATE runNo SET tarikh1 = @tarikh1, run_no = @run_no"
  118. comm = New SqlClient.SqlCommand(sql, conn)
  119.  
  120. comm.Parameters.Add(New SqlClient.SqlParameter("@tarikh1", SqlDbType.DateTime))
  121. comm.Parameters("@tarikh1").Value = lastDateUsed
  122.  
  123. comm.Parameters.Add(New SqlClient.SqlParameter("@run_no", SqlDbType.BigInt))
  124. comm.Parameters("@run_no").Value = lastRunNoUsed
  125.  
  126.  
  127. comm.ExecuteNonQuery()
  128. comm.Connection.Close()
  129.  
  130. Catch ex As Exception
  131. Label5.Visible = True
  132. Label5.Text = "Error. " & ex.Message
  133.  
  134. Finally
  135. conn = Nothing
  136. comm = Nothing
  137. End Try
  138.  
  139. End Sub
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Object reference not set to an instance of an object.

 
0
  #2
May 18th, 2009
it shows conn = nothing
In Private Function GetRunNo() As Long you have
  1. Finally
  2. conn = Nothing
  3. ds = Nothing
  4. End Try
and in Private Sub UpdateRunNo(ByVal lastDateUsed As DateTime, ByVal lastRunNoUsed As Long) you have
  1. Finally
  2. conn = Nothing
  3. comm = Nothing
  4. End Try
Take conn = Nothing lines (or line) away.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 2
Reputation: zaimah is an unknown quantity at this point 
Solved Threads: 0
zaimah zaimah is offline Offline
Newbie Poster

Re: Object reference not set to an instance of an object.

 
0
  #3
May 18th, 2009
that was silly of me :-) TQ. but my problem for my DocID still not solve.. i try put all the data that i want for DocID in a msgbox just wanted to know whether the value is ok.. and it display the value, but why i cannot add the value to my table? when i try to add, the exception error msg is "String or binary data would be truncated.
The statement has been terminated."
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Object reference not set to an instance of an object.

 
0
  #4
May 18th, 2009
that was silly of me :-) TQ.


You have lines
  1. cmd.Parameters.Add(New SqlClient.SqlParameter("@DocID", SqlDbType.Char, 15))
  2. cmd.Parameters("@DocID").Value = dateNow + CoopID.Text + GetRunNo().ToString("D4")
Change them
  1. cmd.Parameters.Add(New SqlClient.SqlParameter("@DocID", SqlDbType.Char, 15))
  2. Dim DocIDParam As String
  3. DocIDParam = dateNow + CoopID.Text + GetRunNo().ToString("D4")
  4. If DocIDParam.Length > 15 Then
  5. MessageBox.Show("DocID length=" & DocIDParam.Length, "Parameter Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  6. ' Truncate param
  7. ' DocIDParam = DocIDParam.Substring(0, 15)
  8. ' Or exit sub
  9. ' Exit Sub
  10. Else
  11. cmd.Parameters("@DocID").Value = DocIDParam
  12. End If
Now you'll get a message box if parameter length exceeds 15 characters and you can either exit or truncate parameter.

If you still get that same error, check from the DB, what is the maximum length of DocID field.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC