By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,568 Members | 1,688 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,568 IT Pros & Developers. It's quick & easy.

Object reference not set to an instance of an object

100+
P: 126
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.Text = DR.Item("NewProdID") <-Error here
Loop



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

Expand|Select|Wrap|Line Numbers
  1. Option Explicit On
  2.  
  3. Imports System.Data.SqlClient
  4. Imports System.Data
  5. Imports System.Drawing
  6. Imports System
  7.  
  8.  'common variable
  9.   Dim intProdId As Integer = 0
  10.  
  11.  Private Sub FrmProductMaintain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) 
  12.             Handles MyBase.Load 
  13.  
  14.         FGetNewProductID()
  15.         intProdId = Me.lblProdID.Text  <---error as well 
  16.  
  17.  End Sub
  18.  
  19. -------------------------------------------------
  20.   Private Sub FGetNewProductID()
  21.     Dim strSql As String = "Select max(ProductID) + 1 as [NewProdID] from TBLProducts"
  22.  
  23.  try
  24.   sqlconn = New SqlConnection(connstr)
  25.   sqlconn.Open()
  26.   sqlcmd = New SqlCommand(strSql)
  27.   sqlcmd.Connection = sqlconn
  28.  
  29.   DR = sqlcmd.ExecuteReader
  30.   Do While DR.Read
  31.     Me.lblProdID.Text = DR.Item("NewProdID")<--Error here
  32.   Loop
  33.  
  34.    DR.Close()
  35.    sqlcmd.Dispose()
  36.    sqlconn.Close()
  37.  
  38.   Catch ex As Exception
  39.        MessageBox.Show(ex.Message)
  40.   End Try
  41.  
  42.  End Sub
  43.  
  44.  
I am very surprise of this error.
Please help me.


Cheers,
Lennie
Sep 13 '10 #1

✓ answered by jay123

Hi lenniekuah,
Your top error can surely be sorted by changing to
Expand|Select|Wrap|Line Numbers
  1.  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
Expand|Select|Wrap|Line Numbers
  1. Me.lblProdID.Text = CStr(DR.Item("NewProdID"))
  2.  
out of interest, have you declared 'connstr' somewhere.. :)

Share this Question
Share on Google+
2 Replies


100+
P: 121
Hi lenniekuah,
Your top error can surely be sorted by changing to
Expand|Select|Wrap|Line Numbers
  1.  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
Expand|Select|Wrap|Line Numbers
  1. Me.lblProdID.Text = CStr(DR.Item("NewProdID"))
  2.  
out of interest, have you declared 'connstr' somewhere.. :)
Sep 14 '10 #2

100+
P: 126
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......Yaaa....Hoooo.......

Lennie
Sep 14 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.