473,379 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Object reference not set to an instance of an object

126 100+
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.. :)

2 893
jay123
121 100+
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
lenniekuah
126 100+
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

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

Similar topics

2
by: Pkpatel | last post by:
Hi, I keep getting this error every time I try to load crystalreportviewer on a webform with a dataset. Here is the error: -------------------------------------------------------- Server...
6
by: NewToDotNet | last post by:
I am getting "Object reference not set to an instance of an object. " when I attempt to open a C# windows service class in design view, although I was able to initially create the service and open...
4
by: Frawls | last post by:
Hi, I get the following error when trying to run a search on my aspx site, this error only occours if the product im searching for does not exist. Can anybody explain this please and help me...
6
by: blash | last post by:
Can someone help me? I really don't have a clue. My company staff told me they often got such error: "Object reference not set to an instance of an object." when they are in search result page...
2
by: jw56578 | last post by:
why am i getting the "Object reference not set to an instance of an object" error on all page registeration tags in my project. <%@ Page Language="vb" AutoEventWireup="false"...
3
by: nemo | last post by:
Hi, My application works fine on the localhost but spits this error as soon as I put it on the server. I know this error occurs when an object has not been instantiated prior to a reference, but...
3
by: Adam | last post by:
We have a web site that uses .vb for the web pages and .cs for a class module. We are getting the error in .NET 2.0 and VS 2005 beta 2. It does work with .NET 1.1. When trying to access a page...
6
by: kalaivanan | last post by:
hi, i am a beginner in c#. i have theoretical knowledge about object, reference and instance. but i want to know clearly about what is an object, reference and instance. can any one help me? or...
1
by: vishnu | last post by:
Hi, I am working on asp.net project which I converted the code fron VB to C# and instead of RaiseEvent in VB code I used the following code. using System; using System.Data; using...
4
by: livmacca | last post by:
Hi, I am new to VB .Net programming and is trying to create a webpage. I encountered the following error and is totally clueless on how to make it work: ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.