Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
//when I put break point in *** :
C#
protected void btnremove_Click(object sender, EventArgs e)
   {

      *** if (cat.removeCategory(Grid.SelectedRow.Cells[1].Text))
           lblmsg.Text = "category deleted successfully";
       else
           lblmsg.Text = "category not deleted , may have products";

       Grid.DataBind();
   //>>>>>>>>>>>>>>>>>>>>>>>>>>>>> and run the program flowing the step into:

  public bool removeCategory(string Catno)
    {
//?????????? look here?
        this.catno =Catno;
       return delete();
    }
//>>>>>>>> and then : this is method in a class :-  
 public bool delete()
    {
        return loadProperties2list("d"); //
    }

//>>>>>>>  the flowing and above is methods in class i called db:-
protected override bool loadProperties2list(string typeOfOperation)
    {
        SortedList sl = new SortedList();
        sl.Add("@check", typeOfOperation);
        sl.Add("@catno", catno);  ????????????? look here catno is null in  the table in database  but i can,t change it to not checked in allow null 
        sl.Add("@catname", catname);
        sl.Add("@description", description);
        procedureName = "manageCategory";
        if (dbVar.runProcedure(procedureName, sl) == 1)
            return true;
        else
            return false;
    }


//>>>>>>>>>>> and then in  step into :
public int runProcedure(string procedureName,SortedList paraVal)
    {
 initialize(CommandType.StoredProcedure, procedureName);
           
        for (int x = 0; x < paraVal.Count; x++)
            
            
            cmd.Parameters.AddWithValue(paraVal.GetKey(x).ToString(), paraVal.GetByIndex(x).ToString());   ////?????????? here the error message i show

        return  runUpdate();
}


//>>>>>>>>  in over it called:------
private void initialize(CommandType CT,string DBcall)
    {
          conn = new SqlConnection();
         cmd = new SqlCommand();


        
        conn.ConnectionString = ConfigurationManager.ConnectionStrings[1].ToString();
        cmd.Connection = conn;
        cmd.CommandType =CT ;
        cmd.CommandText = DBcall;
        conn.Open(); 
    }




//>>>>>>>>>>>>>>>>>>and here i find the error message (Object reference not set to an instance of an object.)


 cmd.Parameters.AddWithValue(paraVal.GetKey(x).ToString(), paraVal.GetByIndex(x).ToString());

//>>>>>>>>>>>> in above
Posted
Updated 10-Jul-13 12:56pm
v3
Comments
[no name] 10-Jul-13 18:00pm    
You need to debug your code, find out which object is null and make it not null before you use it.

Even though you pointed out the line where the exception is thrown, this is not enough. You should check up if cmd is null or cmd.Parameters, or maybe something else.

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
lukeer 8-May-14 3:53am    
Nice.
Sergey Alexandrovich Kryukov 8-May-14 8:53am    
Thank you.
—SA
VB
C# : I understand your problem , the problem is that SortedList Cannot accept null values and this code make problem with you in delete operation only because you come with one value CatNO so I advice you to put this line of code :
  if(ParaVal.GetByIndex(x) != null)


 for (int x = 0; x < paraVal.Count; x++)

            if(ParaVal.GetByIndex(x) != null)

 cmd.Parameters.AddWithValue(paraVal.GetKey(x).ToString(), paraVal.GetByIndex(x).ToString());
 
Share this answer
 
v2
Your source code isn't really clear, If you could use more visual aids like making some things bold and other stuff. I had to strain and look carefully to even see where the error came from.. But since you initialized your command object and connection objects, i don't imagine the error would come from them. Where is the value coming from, is it directly from a control or where.

Secondly, why don't you try putting a breakpoint starting from the beginning of the function where the error is coming from and step into your code, you will know what line exactly it is coming from that way.
 
Share this answer
 

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



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