0

i have created method name fetchProjectsForUpdateQty in my class named clsFoldingManagement. while returning value data back through function (in the form of dataset) i am receiving error on my winform combobox that object reference is not set to an instance of object. below is my method fetchProjectsForUpdateQty code i have written in class clsFoldingManagement

public static DataSet fetchProjectsForUpdateQty(int client_id)
    {
        using (var con = new SqlConnection(ConStr))
        {
            string query = "select  tblClient.ProjectName, tblFolding.Name , tblFolding.FoldingID from tblStockManagement LEFT OUTER JOIN tblClient ON tblClient.Client_ID=tblStockManagement.Client_ID LEFT OUTER JOIN tblFolding ON tblFolding.FoldingID=tblStockManagement.Client_ID  where tblStockManagement.client_id=@clientid and tblStockManagement.quantity >0";
            using (var cmd = new SqlCommand(query, con))
            {

                con.Open();
                cmd.Parameters.Add("@clientid", SqlDbType.Int).Value = client_id;
                cmd.ExecuteNonQuery();
                SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText, con);
                DataSet ds = new DataSet();
                da.Fill(ds, "DATA");
                return ds;
            }
        }
    }

and i have written following code on my combobox click event (cbUpdateProject_Click)

cbUpdateItemName.DataSource = null;
//DataSet data = clsFoldingManagement.fetchProjectDetails();
DataSet ds = clsFoldingManagement.fetchProjectsForUpdateQty(int.Parse(cbUpdateProject.SelectedValue.ToString()));
cbUpdateProject.DataSource = ds.Tables["DATA"].DefaultView;
cbUpdateProject.DisplayMember = "ProjectName";
cbUpdateProject.ValueMember= "Client_ID";

here are the exception details that i got:

 System.NullReferenceException was unhandled
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=FazalConstructions
  StackTrace:
       at FazalConstructions.frmStockMoving.cbUpdateProject_Click(Object sender, EventArgs e) in e:\Waqas Folder\FazalConstruction\FazalConstructions\FazalConstructions\frmStockMoving.cs:line 87
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.ComboBox.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(IntPtr 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(Form mainForm)
       at FazalConstructions.Program.Main() in e:\Waqas Folder\FazalConstruction\FazalConstructions\FazalConstructions\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly 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.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:
Waqas Ali
  • 83
  • 1
  • 10
  • Which line is throwing error and what's the stack trace? – Nikhil Agrawal Feb 12 '16 at 04:04
  • You should post the exception, with a full stack trace. – Kevin Burdett Feb 12 '16 at 04:04
  • where i am assigning function returning value to dataset name 'ds' DataSet ds = clsFoldingManagement.fetchProjectsForUpdateQty(int.Parse(cbUpdateProject.SelectedValue.ToString())); – Waqas Ali Feb 12 '16 at 04:06
  • updated the question with exception details – Waqas Ali Feb 12 '16 at 04:08
  • 1
    The most likely culprit seems to be `cbUpdateProject.SelectedValue` on the line you identified, have you checked to ensure this object is not null? – Kevin Burdett Feb 12 '16 at 04:11
  • yes it was actually null. it fixed the error when i assign the value to cbUpdateProject.SelectedValue, thanks – Waqas Ali Feb 12 '16 at 04:19
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – mason Feb 12 '16 at 04:20

1 Answers1

0

object reference is not set to an instance of object is one of my favorite errors thrown and it means exactly what it says. There is an object you are referencing in your code that isn't set to an instance of an object.

First, it's best to identify what line of code is throwing the exception.

Then analyze the objects in the line and make sure they are not null. If you saying cbUpdateProject.DataSource = ds.Tables["DATA"].DefaultView; and ds.Tables["DATA"] is null but your trying to use the DefaultView you will get the object reference (ds.Tables["DATA"]) is not set to instance of an object - because it is null.

If all the objects in your method are required you should throw some exceptions or handle them properly;

if (clsFoldingManagement == null) throw new Exception ("The class is not initialized");
if (ds.Tables["DATA"] == null) throw new Exception ("The DATA table in the dataset is not initialized");
if (cbUpdateProject.SelectedValue == null) throw new Exception ("The combobox selected value is not initialized");

cbUpdateItemName.DataSource = null;
//DataSet data = clsFoldingManagement.fetchProjectDetails();
DataSet ds = clsFoldingManagement.fetchProjectsForUpdateQty(int.Parse(cbUpdateProject.SelectedValue.ToString()));
cbUpdateProject.DataSource = ds.Tables["DATA"].DefaultView;
cbUpdateProject.DisplayMember = "ProjectName";
cbUpdateProject.ValueMember= "Client_ID";
OneRealWinner
  • 647
  • 3
  • 10