Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1097119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:23:02+00:00 2026-05-17T00:23:02+00:00

My code to insert data is as follows it works for the first time

  • 0

My code to insert data is as follows it works for the first time if i tried to insert data for the second time i am getting the error

My complete code

  using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ACHDAL
{
public class EntryDetail
{

    int[] debits ={ 25, 26, 27, 28, 29, 35, 36, 37, 38, 39, 46, 47, 48, 48, 49, 55, 56, 81, 84, 86, 88 };
    int[] credits ={ 20, 21, 22, 23, 24, 30, 31, 32, 33, 34, 40, 41, 42, 43, 44, 45, 50, 51, 52, 53, 54, 82, 83, 85, 87 };
    private string m_strRecordTypeCode;
    private string m_strTransactionCode;
    private string m_strRecievingDFIIdentification;
    private string m_strCheckDigit;
    private string m_strDFIAccountNumber;
    private string m_strAmount;
    private string m_strIdentificationNumber;
    private string m_strRecievingcompanyName;
    private string m_strDiscretionaryData;
    private string m_strAddendaRecordIndicator;
    private string m_strTraceNumber;
    private static string m_path = string.Empty;

    private bool m_flag = false;

    public string RecordTypeCode
    {
        get { return m_strRecordTypeCode; }
        set { m_strRecordTypeCode = value; }
    }

    public string TransactioCode
    {
        get { return m_strTransactionCode; }
        set { m_strTransactionCode = value; }
    }

    public string RecievingDFIIdentification
    {
        get { return m_strRecievingDFIIdentification; }
        set { m_strRecievingDFIIdentification = value; }
    }

    public string CheckDigit
    {
        get { return m_strCheckDigit; }
        set { m_strCheckDigit = value; }
    }

    public string DFIAccountNumber
    {

        get { return m_strDFIAccountNumber; }
        set { m_strDFIAccountNumber = value; }
    }

    public string Amount
    {
        get { return m_strAmount; }
        set { m_strAmount = value; }
    }

    public string IdentificationNumber
    {
        get { return m_strIdentificationNumber; }
        set { m_strIdentificationNumber = value; }
    }

    public string RecievingCompanyName
    {
        get { return m_strRecievingcompanyName; }
        set { m_strRecievingcompanyName = value; }
    }

    public string DiscretionaryData
    {
        get { return m_strDiscretionaryData; }
        set { m_strDiscretionaryData = value; }
    }

    public string AddendaRecordIndicator
    {
        get { return m_strAddendaRecordIndicator; }
        set { m_strAddendaRecordIndicator = value; }
    }

    public string TraceNumber
    {
        get { return m_strTraceNumber; }
        set { m_strTraceNumber = value; }
    }

    public bool addEntrydetails()
    {
        this.m_flag = false;
        if (m_strRecordTypeCode != string.Empty && m_strTransactionCode != string.Empty && m_strRecievingDFIIdentification != string.Empty &&
            m_strCheckDigit != string.Empty && m_strDFIAccountNumber != string.Empty && m_strAmount != string.Empty && m_strRecievingcompanyName != string.Empty
            && m_strAddendaRecordIndicator != string.Empty)
        {
            saveEntry(m_path);
            m_flag = true;
        }
        else
        {
            m_flag = false;
        }
        return m_flag;
    }

    public bool BankRoutingNumberValidation(string sInput)
    {
        m_flag = false;

        bool bRule1 = false;
        bool bRule2 = false;

        int iResult = 0;
        if (sInput.Length == 9)
        {
            iResult = 3 * Convert.ToInt32(sInput.Substring(0, 1)) +
            7 * Convert.ToInt32(sInput.Substring(1, 1)) +
            Convert.ToInt32(sInput.Substring(2, 1)) +
            3 * Convert.ToInt32(sInput.Substring(3, 1)) +
            7 * Convert.ToInt32(sInput.Substring(4, 1)) +
            Convert.ToInt32(sInput.Substring(5, 1)) +
            3 * Convert.ToInt32(sInput.Substring(6, 1)) +
            7 * Convert.ToInt32(sInput.Substring(7, 1)) +
            Convert.ToInt32(sInput.Substring(8, 1));

            if ((iResult % 10) == 0)
            {
                bRule1 = true;

                iResult = 7 * Convert.ToInt32(sInput.Substring(0, 1)) +
                3 * Convert.ToInt32(sInput.Substring(1, 1)) +
                9 * Convert.ToInt32(sInput.Substring(2, 1)) +
                7 * Convert.ToInt32(sInput.Substring(3, 1)) +
                3 * Convert.ToInt32(sInput.Substring(4, 1)) +
                9 * Convert.ToInt32(sInput.Substring(5, 1)) +
                7 * Convert.ToInt32(sInput.Substring(6, 1)) +
                3 * Convert.ToInt32(sInput.Substring(7, 1));

                if ((iResult % 10) == Convert.ToInt32(sInput.Substring(8, 1)))
                {
                    bRule2 = true;
                }

                m_flag = bRule1 & bRule2;
            }
        }
        return m_flag;

    }


    public bool saveEntry(string strPath)
    {
        long Amnt = 0;
        double AmtforFileControl_debit=0;
        double AmtforFileControl_credit=0;
        m_flag = true;

        Append.sb.AppendLine(); 
             //When i put a break point i am getting the error here               Object reference not set to an instance of an object.


        Append.sb.Append(m_strRecordTypeCode.PadLeft(1, '0'));
        Append.sb.Append(m_strTransactionCode.PadLeft(2, '0'));

        Append.sb.Append(m_strRecievingDFIIdentification.PadLeft(9, '0'));
        Append.sb.Append(m_strDFIAccountNumber.PadRight(17, ' '));
        Append.sb.Append(m_strAmount.PadLeft(10, '0'));
        int length = debits.Length;
        for (int j = 0; j < length; j++)
        {
            if (m_strTransactionCode == debits[j].ToString())
            {
                Amnt = Convert.ToInt64(m_strAmount);
                if(Append.oldbatchcontrol!=string.Empty)
                {
                    AmtforFileControl_debit=Convert.ToDouble(m_strAmount);
                }
                Append.debitAmnt += Amnt;
                break;
            }
        }
        int CreditLength = credits.Length;
        for (int k = 0; k < CreditLength; k++)
        {
            if (m_strTransactionCode == credits[k].ToString())
            {
                Amnt = Convert.ToInt64(m_strAmount);
                if(Append.oldbatchcontrol!=string.Empty)
                {
                    AmtforFileControl_credit=Convert.ToDouble(m_strAmount);
                }
                Append.creditAmnt += Amnt;
                break;
            }
        }
        Append.sb.Append(m_strIdentificationNumber.PadRight(15, ' '));
        Append.EntryHash += Convert.ToDouble(m_strRecievingDFIIdentification);
        Append.sb.Append(m_strRecievingcompanyName.PadRight(22, ' '));
        Append.sb.Append(m_strDiscretionaryData.PadRight(2, ' '));
        Append.sb.Append(m_strAddendaRecordIndicator.PadLeft(1, '0'));
        Append.sb.Append(m_strTraceNumber.PadLeft(15,'0'));

        //Entry Count for EntryDetail
        Append.Entrycnt++;

        if (Append.oldbatchcontrol != string.Empty)
        {
            //Append.sb.AppendLine();
            string strRecordtype = "8";

            string strServcclasscode = Append.StandEntryCode.PadLeft(3, '0');
            //m_strServiceClassCode.PadLeft(3, '0').ToString();

            string Entrycnt = Append.Entrycnt.ToString().PadLeft(6, '0');

            filecontrolvariables.entrycount++;

            string EntryHash = Append.EntryHash.ToString().PadLeft(10, '0');

            filecontrolvariables.Entryhash += Convert.ToDouble(m_strRecievingDFIIdentification);

            string debitAmnt = Append.debitAmnt.ToString().PadLeft(12, '0');

            filecontrolvariables.debitamt += AmtforFileControl_debit;

            string creditAmnt = Append.creditAmnt.ToString().PadLeft(12, '0');

            filecontrolvariables.creditamt = AmtforFileControl_credit;

            string CompIdentification = Append.Companyidentification.PadRight(10, ' ');
            //m_strCompanyIdentification.PadRight(10, ' ');

            string MessageAuthenticationCode = "".PadRight(19, (char)32).ToString();

            string Reserved = "".PadRight(6, (char)32).ToString();

            string Odfi = Append.OriginalOdfi.PadLeft(8, '0');
            //m_strOriginationDFIIdentification.PadLeft(8, '0');

            string batchno = Append.Batchnum_edit.ToString().PadLeft(7, '0');
            //m_strBatchNumber.PadLeft(7, '0');

            Append.sb.AppendLine();
            Append.sb.Append(strRecordtype);
            Append.sb.Append(strServcclasscode);
            Append.sb.Append(Entrycnt);
            Append.sb.Append(EntryHash);
            Append.sb.Append(debitAmnt);
            Append.sb.Append(creditAmnt);
            Append.sb.Append(CompIdentification);
            Append.sb.Append(MessageAuthenticationCode);
            Append.sb.Append(Reserved);
            Append.sb.Append(Odfi);
            Append.sb.Append(batchno);

            StreamReader forAddEntry = new StreamReader(strPath);
            string Filedata = string.Empty;
            while (Filedata == forAddEntry.ReadToEnd())
            {
                Filedata.Replace(Append.oldbatchcontrol, Append.sb.ToString());
            }
            forAddEntry.Close();
            StreamWriter sw = new StreamWriter(strPath);
            sw.Write(Filedata);
            sw.Close();
        }

        //Append.Batchcnt++;

        return m_flag;
    }
}
public static class Append
{
    public static string OriginalOdfi=string.Empty;
    public static string oldbatchcontrol=string.Empty;
    public static StringBuilder sb = new StringBuilder();
    public static StringBuilder _sb
    {
        get { return sb; }
        set
        {
            sb = value;
        }
    }

    public static int Batchcnt = 0;
    public static int Batchnum_edit=0;
    public static int Traceno = 0;
    public static int Entrycnt = 0;
    public static double EntryHash = 0;
    public static long debitAmnt = 0;
    public static long creditAmnt = 0;
    public static int Seccode = 0;
    public static string Companyidentification = string.Empty;
    public static string StandEntryCode = string.Empty;

}

}

Stack Trace

         at ACHDAL.EntryDetail.saveEntry(String strPath) in D:\ACHWINAPPLICATION\ACHDAL\EntryDetail.cs:line 164
   at ACHWINAPPLICATION.frmEntryDetails.btnSave_Click(Object sender, EventArgs e) in     D:\ACHWINAPPLICATION\ACHWINAPPLICATION\frmEntryDetails.cs:line 114
  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.RunDialog(Form form)
     at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
        at ACHWINAPPLICATION.frmMain.addEntry_Click(Object sender, EventArgs e) in            D:\ACHWINAPPLICATION\ACHWINAPPLICATION\frmMain.cs:line 553
         at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
          at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
         at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
         at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
           at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e,     ToolStripItemEventType met)
       at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e,  ToolStripItemEventType met)
       at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
         at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
         at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
        at System.Windows.Forms.Control.WndProc(Message& m)
      at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
         at System.Windows.Forms.ToolStrip.WndProc(Message& m)
      at System.Windows.Forms.ToolStripDropDown.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(Form mainForm)
     at ACHWINAPPLICATION.Program.Main() in D:\ACHWINAPPLICATION\ACHWINAPPLICATION\Program.cs:line 20
  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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:23:03+00:00Added an answer on May 17, 2026 at 12:23 am

    I don’t see it in this code, so it’s probably happening elsewhere; but since you never actually clear sb, you might as well precede the line in question with sb = new StringBuilder(); anyway.

    Yes, I know it’s a hack.

    Edit

    It’s clear from the OP’s followup comments that this problem is external to the code shown. @Kirk Woll’s suggestion for debugging the problem is a good start, but given the OP’s response I suspect a structural problem of some kind.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to insert/update data by using link or image I need a code
I have table that I insert data with following query (from c# code): INSERT
I have written a code to insert the data to SQL LITE database as
I want to insert data with special character in oracle tables. My code is
Below is my code ; I cannot insert data on button click as my
I have the following code I use to insert form data into a single
I need to write some code to insert around 3 million rows of data.
I have a JavaScript code segment for loading server's data items, and insert them
I have following code for inserting data into database using PDO. It inserts data
My jQuery code (using ajax) request's data from a local php script (pgiproxy.php). This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.