Click here to Skip to main content
15,914,488 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Here is my code

C#
public void beginTrans()
        {
            try
            {
                if (dbTran == null)
                {
                    if (dbConn.State == 0) { createConn(); }
                    dbTran = dbConn.BeginTransaction();
                    dbCommand.Transaction = dbTran;
                    dbAdapter.SelectCommand.Transaction = dbTran;
                    dbAdapter.InsertCommand.Transaction = dbTran;
                    dbAdapter.UpdateCommand.Transaction = dbTran;
                    dbAdapter.DeleteCommand.Transaction = dbTran;
                }
            }
            catch (Exception exc) { throw exc; }
        }

        public void commitTrans()
        {
            try
            {
                if (dbTran != null)
                {
                    dbTran.Commit();
                    dbTran = null;
                }
            }
            catch (Exception exc) { throw exc; }
        }

        public void rollBackTrans()
        {
            try
            {
                if (dbTran != null)
                {
                    dbTran.Rollback();
                    dbTran = null;
                }
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }


C#
public DataSet insertQuery(DataSet ds, Hashtable ht)
        {
            try
            {
                dTable = ds.Tables[0];
                dRow = dTable.NewRow();
                object value;
                foreach (string ha in ht.Keys)
                {
                    value = ht[ha];
                    dRow[ha] = value;
                }
                dTable.Rows.Add(dRow);
                return ds;
            }
            catch (Exception exc) { throw exc; }
        }



C#
public override void insert_lavlah(Hashtable hash)
        {
            try
            {
                dAccess.beginTrans();
                dSet.Clear();
                getDSet = dAccess.insertQuery(dSet, hash);
                dAccess.commitTrans();
            }
            catch (Exception exc)
            {
                dAccess.rollBackTrans();
                throw exc;
            }
        }


C#
private void btnOk_zereg_Click(object sender, EventArgs e)
        {
            // bagshiin zeregt ogogdol nemeh
            //zereg.save_lavlah();
            Hashtable hTab = new Hashtable();
            hTab.Add("zereg_code", Convert.ToInt32(txtID_addZereg.Text));
            hTab.Add("zereg_ner", txtName_addZereg.Text);
            zereg.insert_lavlah(hTab);
            txtID_addZereg.Clear();
            txtName_addZereg.Clear();
            gbox_addZereg.Visible = false;

            //zereg.save_lavlah();
        }


Where is my fault!!!
ERROR IS: Object reference not set to an instance of an object.
Posted
Comments
AmitGajjar 3-Nov-12 7:17am    
On which line ?

1 solution

When you get this kind of problem, it is normally pretty easy for you to sort out - not so easy for us.

We can't tell exactly where your problem is - you don't tell us which line you get the exception on - but the debugger is a very good tool for working this out.
In VS, before you start testing, go to the menu bar, and look under "Debug...Exceptions..."
In the dialog that results, put a tick in every single check box, especially those in the "thrown" column.
Press OK

Now run your app. When the get the null reference error, the debugger will stop and show you exactly which line caused it. You can now examine the line and look for a variable containing a null. It's now up to you to find out why it is null - but at least you will know which it is! :laugh:
 
Share this answer
 
Comments
Tsepu 3-Nov-12 7:42am    
ERROR LINE IS : catch (Exception exc)
{
dAccess.rollBackTrans();
throw exc;
}

I think here is fault:
dbCommand.Transaction = dbTran;
dbAdapter.SelectCommand.Transaction = dbTran;
dbAdapter.InsertCommand.Transaction = dbTran;
dbAdapter.UpdateCommand.Transaction = dbTran;
dbAdapter.DeleteCommand.Transaction = dbTran;
OriginalGriff 3-Nov-12 7:49am    
That is not the error line - that is where the problem is caught.
The exception detail will tell you which actual line is causing the problem.

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