Object reference not set to an instance of an object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lenniekuah
    New Member
    • Oct 2006
    • 126

    Object reference not set to an instance of an object

    Hi Good Guys,
    I need your help. Please help me.
    I encounter another surprising deadly problem.

    I am trying to create a new ProductID and stored it in a variable intProdID and also to display it on the Label.

    But I encounter this error message to my surprise:
    Object reference not set to an instance of an object
    this part of coding generate the error message:

    Do While DR.Read
    Me.lblProdID.Te xt = DR.Item("NewPro dID") <-Error here
    Loop



    ------------------------------------------
    Here are the overall coding

    Code:
    Option Explicit On
    
    Imports System.Data.SqlClient
    Imports System.Data
    Imports System.Drawing
    Imports System
    
     'common variable
      Dim intProdId As Integer = 0
    
     Private Sub FrmProductMaintain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) 
    			Handles MyBase.Load 
         
            FGetNewProductID()
            intProdId = Me.lblProdID.Text  <---error as well 
    
     End Sub
    
    -------------------------------------------------
      Private Sub FGetNewProductID()
        Dim strSql As String = "Select max(ProductID) + 1 as [NewProdID] from TBLProducts"
    
     try
      sqlconn = New SqlConnection(connstr)
      sqlconn.Open()
      sqlcmd = New SqlCommand(strSql)
      sqlcmd.Connection = sqlconn
    
      DR = sqlcmd.ExecuteReader
      Do While DR.Read
        Me.lblProdID.Text = DR.Item("NewProdID")<--Error here
      Loop
    
       DR.Close()
       sqlcmd.Dispose()
       sqlconn.Close()
    
      Catch ex As Exception
           MessageBox.Show(ex.Message)
      End Try
    
     End Sub
    I am very surprise of this error.
    Please help me.


    Cheers,
    Lennie
    Last edited by lenniekuah; Sep 13 '10, 09:05 AM. Reason: Type Error
  • jay123
    New Member
    • Sep 2008
    • 121

    #2
    Hi lenniekuah,
    Your top error can surely be sorted by changing to
    Code:
     intProdId = CInt(Me.lblProdID.Text)
    as you are trying to allocate some Text value to an Integer.

    i consider that your ProductId is Integer, you should use
    Code:
    Me.lblProdID.Text = CStr(DR.Item("NewProdID"))
    out of interest, have you declared 'connstr' somewhere.. :)

    Comment

    • lenniekuah
      New Member
      • Oct 2006
      • 126

      #3
      Hi Jay123,

      You are wonderful. Thank you for helping me. Based on your instruction I checked my coding and Yes, I made a mistake. It's awesome I fixed it as per your instructions and now it's working.

      Thank you very much for your help. You are awesome and wonderful in helping.

      I am so glad to meet you here at this wonderful FORUM.


      Cheers......Yaa a....Hoooo..... ..

      Lennie
      Last edited by lenniekuah; Sep 14 '10, 08:36 PM. Reason: spelling error

      Comment

      Working...