Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all

I was post a question from 6 days ago as link below
code of check the record duplicate[^]

and it's successful with me with table and when i try to use it with other table by some change it's get error as below

System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."


pls check and advise and thanks in advance

full code below

VB
Imports System.Data.OleDb
Public Class SpecialColor
    Dim Con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=|DataDirectory|\db1.mdb;")

    Dim Sqltr As String = "Select * From SpecialColor Where OrderNo LIKE '%" & addOrder.OrderNoText.Text & "%'"
    Dim Da As New OleDbDataAdapter(Sqltr, Con)
    Dim Ds As New DataSet
    Dim Cmdb As New OleDbCommandBuilder

    Private Sub SpecialColor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Da.Fill(Ds, "SpecialColor")
        OrderNoTxt.DataBindings.Add(New Binding("Text", Ds, "SpecialColor.OrderNo"))
        PrintOntxt.DataBindings.Add(New Binding("Text", Ds, "SpecialColor.PrintOn"))
        SpColortxt.DataBindings.Add(New Binding("Text", Ds, "SpecialColor.SpecialColor"))
        Colortxt.DataBindings.Add(New Binding("Text", Ds, "SpecialColor.color"))
        RefTxt.DataBindings.Add(New Binding("Text", Ds, "SpecialColor.Ref"))
    End Sub

    Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
    Sub SaveData()

        If Ds.Tables("SpecialColor").AsEnumerable().Count(Function(row) row.Field(Of String)("Ref").ToLower() = Trim(RefTxt.Text.ToLower())) > 0 Then
            MsgBox("Order added Before !", MsgBoxStyle.Information)
            Return
        End If
        Dim CurRec As Integer
        CurRec = Me.BindingContext(Ds, "SpecialColor").Position
        Dim OrderNo As String = Ds.Tables("SpecialColor").Rows(CurRec).Item("OrderNo")
        Dim PrintOn As String = Ds.Tables("SpecialColor").Rows(CurRec).Item("PrintOn")
        Dim SpColor As String = Ds.Tables("SpecialColor").Rows(CurRec).Item("SpecialColor")

        Ds.Tables("SpecialColor").Rows(CurRec).BeginEdit()
        Ds.Tables("SpecialColor").Rows(CurRec).Item("color") = Me.Colortxt.Text
        Ds.Tables("SpecialColor").Rows(CurRec).Item("Ref") = Me.RefTxt.Text
        Da.UpdateCommand = New OleDbCommand("Update SpecialColor Set Color=?,Ref=? Where OrderNo=? And PrintOn=? And SpecialColor=? ", Con)
        Da.UpdateCommand.Parameters.Clear()
        Da.UpdateCommand.Parameters.AddWithValue("?", Me.Colortxt.Text.ToString)
        Da.UpdateCommand.Parameters.AddWithValue("?", Me.RefTxt.Text.ToString)
        Da.UpdateCommand.Parameters.AddWithValue("?", OrderNo)
        Da.UpdateCommand.Parameters.AddWithValue("?", PrintOn)
        Da.UpdateCommand.Parameters.AddWithValue("?", SpColor)
        Ds.Tables("SpecialColor").Rows(CurRec).EndEdit()




        Da.Update(Ds, "SpecialColor")
       


    End Sub
    Private Sub First_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles First.Click
        Me.BindingContext(Ds, "SpecialColor").Position = 0
    End Sub

    Private Sub Perv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Perv.Click
        Me.BindingContext(Ds, "SpecialColor").Position -= 1
    End Sub

    Private Sub Next1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Next1.Click
        Me.BindingContext(Ds, "SpecialColor").Position += 1
    End Sub

    Private Sub Last_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Last.Click
        Dim mngr As BindingManagerBase = Me.BindingContext(Ds, "SpecialColor")
        mngr.Position = mngr.Count - 1
    End Sub

    Private Sub New1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles New1.Click
        Dim result = MessageBox.Show("you sure you want to Exit ? ", "caption", MessageBoxButtons.YesNo)
        If result = DialogResult.Yes Then
            MyBase.Close()
        End If
    End Sub

    Private Sub Finish_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Finish.Click
        Dim result = MessageBox.Show("Did you want to Save your data before Exit ? ", "caption", MessageBoxButtons.YesNo)
        If result = DialogResult.Yes Then
            SaveData()
            MyBase.Close()
        ElseIf result = DialogResult.No Then
            MyBase.Close()

        End If
    End Sub

    Private Sub Previous_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Previous.Click
        PrintDetails.Show()
        MyBase.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        




        If Me.BindingContext(Ds, "SpecialColor").Position = Me.BindingContext(Ds, "SpecialColor").Count - 1 Then
            SaveData()
            Me.Close()
        Else
            SaveData()
            Me.BindingContext(Ds, "SpecialColor").Position += 1

        End If
    End Sub
Posted
Updated 17-Oct-13 14:32pm
v2
Comments
Sergey Alexandrovich Kryukov 17-Oct-13 20:39pm    
In what line?
—SA
Ahmed Shafea 17-Oct-13 20:50pm    
here
If Ds.Tables("SpecialColor").AsEnumerable().Count(Function(row) row.Field(Of String)("Ref").ToLower() = Trim(RefTxt.Text.ToLower())) > 0 Then
Matt T Heffron 17-Oct-13 20:56pm    
Any chance RefTxt is null? or RefTxt.Text?
Ahmed Shafea 17-Oct-13 21:05pm    
sorry english not a First language for me
This code for check This ref this entered before in my data before or not ..if entered before cancel this processed
Matt T Heffron 17-Oct-13 21:16pm    
In any case, something in that line of code is returning null and then being de-referenced.
You just need to figure out what.

Okay, you pointed out where the exception with the message "Object reference not set to an instance of an object" is thrown. Good. Now you need to see where the null is. There are several candidates: Ds, Ds.Tables (not very likely), Ds.Tables("SpecialColor") (most likely, "not found by key" situation), and so on. You need to learn how to deal with such situations by yourself. You cannot ask someone for help each time it happens to you.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx[^],
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx[^].

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

Good luck,
—SA
 
Share this answer
 
Comments
Ahmed Shafea 17-Oct-13 21:32pm    
This error with below code
If Ds.Tables("SpecialColor").AsEnumerable().Count(Function(row) row.Field(Of String)("Ref").ToLower() = Trim(RefTxt.Text.ToLower())) > 0 Then
MsgBox("Order added Before !", MsgBoxStyle.Information)
Return
End If

and quick watch said
AsEnumerable Name 'AsEnumerable' is not declared.
Sergey Alexandrovich Kryukov 17-Oct-13 23:33pm    
Wait a second... If your code is already running, everything is declared by definition, because it compiled. What to you watch then?
—SA
Ahmed Shafea 18-Oct-13 6:40am    
what's you meant with everything is declared by definition ?
Ahmed Shafea 18-Oct-13 7:14am    
full details for my error

System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="PrintShopDataBase"
StackTrace:
at PrintShopDataBase.SpecialColor._Lambda$__2(DataRow row) in F:\VbDotNet\PrintShopDataBase\PrintShopDataBase\SpecialColor.vb:line 29 at System.Linq.Enumerable.Count[TSource](IEnumerable`1 source, Func`2 predicate) at PrintShopDataBase.SpecialColor.SaveData() in F:\VbDotNet\PrintShopDataBase\PrintShopDataBase\SpecialColor.vb:line 29 at PrintShopDataBase.SpecialColor.Button1_Click(Object sender, EventArgs e) in F:\VbDotNet\PrintShopDataBase\PrintShopDataBase\SpecialColor.vb:line 113 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(ApplicationContext context) at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) at PrintShopDataBase.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Sergey Alexandrovich Kryukov 18-Oct-13 9:55am    
I mean I don't understand how can you possibly run into "is not declared" in is a compilation error.
And this is not what mentioned in the stack trace you show. What's the use of it, without seeing all that lines of code and their line numbers? As I say, use the debugger and do what I advised...
—SA
XML
<b>Hi

You can add or configure the used table in your used dataset.

Check used table is present in the dataset or not.

If not then configure the table in dataset.

Hope this  solution is help for you.</b>
 
Share this answer
 
Comments
Ahmed Shafea 18-Oct-13 6:46am    
Sorry i don't know what's you meant with configure the used table in your used dateset ?

all fields in table display in forms ....
basurajkumbhar 28-Oct-13 9:20am    
e.g.
your table name is stud
open your dataset and from your server explore drag drop the your table stud in daset
and run your application.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900