2

So I have a GUI using MSVS 2010 and I am creating many forms.

My main form opens up my text file "database" and concats the proper data to a different opened file. If the opened file has data that does not match in the database file then I would like to add that data to the database file.

With that being said, when a button "dataBaseSaveButton_Click" is clicked a new form will pop up that has textboxes reading: Machine, Name, PTP Name, CMP Name, T-Width, Feeder Pitch. Most of the fields will be auto-populated and others the user will have to enter in.

Also in the form there is another "save" button. Now, on this form, the save button will actually take the data in the textboxes and open up the proper file (the database file) and save it (update the database) in a certain format.

The "Save To Data Base" form looks like this:

Save To Data Base Form

My code from the main form looks like this (when the button is clicked):

private void dataBaseSaveButton_Click(object sender, EventArgs e)
{
    int unknownCP4Counter = theCP4UnknownList.Distinct().Count();

    foreach (var line in theCP4UnknownList.Distinct())
    {
        var splitUnknowns = line.Split(' ');

        KTS_Save saveForm = new KTS_Save("CP4", splitUnknowns[0], splitUnknowns[1], splitUnknowns[3], splitUnknowns[4], openDataBase2File.FileName, unknownCP4Counter);

        saveForm.ShowDialog();

        unknownCP4Counter--;
    }
}

The code for the Save To Data Base form looks like:

private string _Machine;
private string _Name;
private string _PtpName;
private string _TapeWidth;
private string _FeederPitch;
private string _DataBaseFileName;
public KTS_Save()
{
    InitializeComponent();
}

public KTS_Save(string Machine, string Name, string PtpName, string TapeWidth, string FeederPitch, string DBFileName, int Counter)
{
    InitializeComponent();

    _Machine = Machine;
    _Name = Name;
    _PtpName = PtpName;
    _TapeWidth = TapeWidth;
    _FeederPitch = FeederPitch;
    _DataBaseFileName = DBFileName;

    machineTextBox.Text = _Machine;
    nameTextBox.Text = _Name;
    ptpNameTextBox.Text = _PtpName;
    tapeWidthTextBox.Text = _TapeWidth;
    feederPitchTextBox.Text = _FeederPitch;
    counterLabel.Text = Counter.ToString();
}

private void dataBaseSaveButton_Click(object sender, EventArgs e)
{
    //This is where I need help.
}

What I need help with is actually printing out all of the textbox data into the file that already exists.

I know this is probably not the most efficient way to do things, so if you have any recommendations please let me know! :)

(Oh yeah..)

I would like the program to run like so (or similar):

  • From the main form, click on the button and the Save To Data Base form will pop up.
  • The user will enter in the appropriate fields (textboxes).
  • The user will click the "Save To Data Base" button and the data will be written the the textfile that is passed in through the main form.
    • Or else, the user will click the upper right "X" (close) to not save to the data base.
theNoobGuy
  • 1,636
  • 6
  • 29
  • 45
  • after the user clicks the save button you could create an enum that holds the structure of the database fields in the order you want.. then what ever data is captured store that data in a List or a Dictionary it's fairly simple process.. – MethodMan Dec 13 '11 at 17:32
  • Do you want to append to the file or insert somewhere in the middle? Furthermore, if you already think of this to be a database file, then have you considered an embedded database like SQLite (it's entirely file-based)? – Kiril Dec 13 '11 at 17:33
  • @Lirik: I would like to insert in the middle. If the machine is labeled "CP4" in the textbox, I would like to append after the line "***CP4***" is found in the textfile. Other machines could be: CP6, CP7, IP2A. I have considered doing it with an ACTUAL database but I have no knowledge whatsoever with them :( – theNoobGuy Dec 13 '11 at 17:37
  • 1
    +1 for specific details and images. – theNoobGuy Dec 13 '11 at 17:55

1 Answers1

1

You can "technically" insert into a file, but you basically have to copy the entire file over every time you insert something. To be more precise: it's not a real insert, because the file system does not support inserts. There are multiple examples online, but I would actually recommend that you avoid the whole debacle and either use an embedded database or XML files.

I would highly recommend that you use an SQLite database and take advantage of the ADO .NET Entity Framework, but if that's a big hassle for you then you can do simple XML files.

Here are some examples:

Good luck.

Community
  • 1
  • 1
Kiril
  • 39,672
  • 31
  • 167
  • 226