Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Add Current Data Row to an Array or String Variable

string array

  • Please log in to reply
1 reply to this topic

#1 Pierce

Pierce

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 06 March 2012 - 05:52 PM

I have managed to muddle my way into rebuilding my old VB6 app in C#, and it works. Now I want to make it work better, or at least what I feel will be better.

I have a form with two text boxes [txtFilePath & txtFileName] that are bound to the FilePath and FileName fields in the database that I have selected and opened earlier in the code. The process below cycles through the database, building a full file path from the two entries in the database which is then passed to File.Exists to make sure that the referenced file is actually on the hard drive. What I would like to do is remove the dependency on the bound text boxes by having the data which normally appears in them pass from the database directly into a string. I've been scouring the web to no avail for several hours now. Since I am able to advance to the next row by using uniObjBindingSource.MoveNext(); it seems to me that there should be a simple way to read the current row into an array or two string variables. Am I wrong?

private void cmdVerify_Click(object sender, EventArgs e)
        {
            //Progress bar stuff
            barProgress.Visible = true;
            barProgress.Minimum = 1;
            barProgress.Maximum = uniObjBindingSource.Count;
            barProgress.Value = 1;
            barProgress.Step = 1;

            //Get values for record count and current position
            int iRecords = uniObjBindingSource.Count;
            int iCurrentRecord = uniObjBindingSource.Position;

            //txtReport.Text = (1 + iCurrentRecord).ToString() + " of " + iRecords.ToString();

            string rawPath = txtFilePath.Text;
            string driveLetter = rawPath.Substring(1, 1);
            
            //Parse out crappy TD filepath entry
            int iLength;
            iLength = rawPath.Length;
            string trimmedPath;
            trimmedPath = rawPath.Substring(4, (iLength - 4));

            //Give File.Exists something to work with
            lblStatus.Text = driveLetter + ":\\" + trimmedPath + "\\" + txtFileName.Text;
            string checkFile = lblStatus.Text;
            
            //Cycle through entire database
            do 
            if (File.Exists(checkFile))
	        {
                uniObjBindingSource.MoveNext();
                lblStatus.Text = driveLetter + ":\\" + trimmedPath + "\\" + txtFileName.Text;
                checkFile = lblStatus.Text;
                iCurrentRecord = uniObjBindingSource.Position;
                Application.DoEvents();
                barProgress.PerformStep();
                    if(bStop)
                    {
                        break;
                    }
	        }
            else
            {
                txtReport.AppendText("Not Found: " + checkFile + Environment.NewLine);
                uniObjBindingSource.MoveNext();
                lblStatus.Text = driveLetter + ":\\" + trimmedPath + "\\" + txtFileName.Text;
                checkFile = lblStatus.Text;
                iCurrentRecord = uniObjBindingSource.Position;
                Application.DoEvents();
                barProgress.PerformStep();
                    if (bStop)
                    {
                        break;
                    }
            }
            while(iCurrentRecord <= iRecords);
            cmdSaveReport.Enabled = true;
        }

  • 0

#2 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 10 April 2012 - 10:00 AM

It seems you are using BindingSource. So I think you can use the List property to get all the items. Like the following code...

foreach (DataRow row in uniObjBindingSource.List) {
	String firstString = row[0].ToString();
	String secondString = row[1].ToString();
}

This way you don't need to bind that binding source to text boxes.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download