views:

101

answers:

2

So I'm trying out a concept tool of mine where I need to be able to read and write data from a database real easy. I've set up the form as I like and spread around different text boxes and dropdownboxes to read the data from the database. And I've got it all to work and all, but there's a small bug I don't fully understand why's there. Some textboxes don't update the text from the database. But it seems as it only occurs if the data in the database is nothing. So the value from the last row is still hanging in the textbox and thus, clicking "Update" actually updates the value from the field from the last row, into the new row. Messing everything up.

Now, what I'm the most interested in is the shear flow of the code. What's the best way of laying out the code to do all this? So far I've got this:

This is the code when clicking on a cell in the datagridview:

Private Sub DataGridView_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView.CellClick

    On Error Resume Next   

    selectedName = Me.DataGridView.CurrentRow.Cells(0).Value
    selectedGenre = Me.DataGridView.CurrentRow.Cells(1).Value
    selectedRhytm = Me.DataGridView.CurrentRow.Cells(2).Value
    selectedLength = Me.DataGridView.CurrentRow.Cells(3).Value
    selectedFinished = Me.DataGridView.CurrentRow.Cells(4).Value
    selectedSoundFile = Me.DataGridView.CurrentRow.Cells(5).Value

    txtBoxName.Text = selectedName
    txtBoxGenre.Text = selectedGenre
    txtBoxRhytm.Text = selectedRhytm
    txtBoxLength.Text = selectedLength
    txtBoxFinished.Text = selectedFinished
    txtBoxSoundFile.Text = selectedSoundFile

End Sub

The "selected"-variables are all declared in a GlobalCode.vb I've got where I create all of them for use later. They are defined like this:

Friend Module GlobalVariables

    Friend selectedName As String = Nothing
    Friend selectedGenre As String = Nothing
    Friend selectedRhytm As String = Nothing
    Friend selectedLength As String = Nothing
    Friend selectedFinished As String = Nothing
    Friend selectedSoundFile As String = Nothing


End Module

I haven't really done anything like this before. I'm more of a designer rather than programmer, but I really need to try out a concept so I'm not sure this is the way of doing this at all. I've found that it works, most of the times. But I reckon skilled programmers have a way of designing the layout of the code so it's efficient, clean and easy to read. So how does this look?

A: 

Try commenting out On Error Resume Next for a bit and seeing what happens. I've managed to confuse myself more times than I can count with that statement.

EDIT

I just realized this is VB.Net. You shouldn't ever be using On Error Resume Next in this case. Use Try Catch structures.

Spencer Ruport
Ok, I'm going to try to do that to just see what happens.Any idea of how to get the code to move on if the value from the cells are nothing?
Kenny Bones
Found out, this works great, thanx! :)
Kenny Bones
+2  A: 

(I can't see anything database related in the question, btw)

Perhaps the best way of laying out this code is... not to. Don't write code for things that the standard data-binding frameworks can handle. For example (sorry it is C#, but it should translate - all of the "good" bits here are provided by the .NET framework, not the language); some UI code - note no code to copy values:

static class Program {
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        // some sample data
        BindingList<Track> tracks = new BindingList<Track>();
        tracks.Add(new Track { Name = "foo", Genre = "Rock", Rhythm = "insane", Length = 180 });
        tracks.Add(new Track { Name = "bar", Genre = "Classic", Rhythm = "sedate", Length = 240 });

        // show the data on a form
        using (Form form = new Form {
            Controls = {
                new DataGridView { DataSource = tracks, Dock = DockStyle.Fill },
                new TextBox { DataBindings = {{"Text", tracks, "Name"}}, Dock = DockStyle.Bottom},
                new TextBox { DataBindings = {{"Text", tracks, "Genre"}}, Dock = DockStyle.Bottom},
                new TextBox { DataBindings = {{"Text", tracks, "Rhythm"}}, Dock = DockStyle.Bottom},
                new TextBox { DataBindings = {{"Text", tracks, "Length"}}, Dock = DockStyle.Bottom},
            }
        }) {
            Application.Run(form);
        }
    }
}

With supporting data entity:

class Track : INotifyPropertyChanged {
    private string name, genre, rhythm;
    private int length;
    public event PropertyChangedEventHandler PropertyChanged;
    private void SetField<T>(ref T field, T value, string propertyName) {
        if (!EqualityComparer<T>.Default.Equals(field, value)) {
            field = value;
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public string Name { get { return name; } set { SetField(ref name, value, "Name"); } }
    public string Genre { get { return genre; } set { SetField(ref genre, value, "Genre"); } }
    public string Rhythm { get { return rhythm; } set { SetField(ref rhythm, value, "Rhythm"); } }
    public int Length { get { return length; } set { SetField(ref length, value, "Length"); } }
}
Marc Gravell
Best tip yet! As you can probably tell, I'm no real programmer. So the questing was really related to - How to really bind data :)
Kenny Bones
Just tried to databind the values of the textboxes to the columns in the database. And it works great, no more bugs :)But, the data isn't stored. Tried to insert the following: DataSourceDataSet.Songs.AcceptChanges() SongsTableAdapter.Update(Me.DataSourceDataSet.Songs)But the data isn't updated in the database
Kenny Bones