0

I have a form which is linked to a database and the buttons which control the form are not working, I'm not getting any errors just simply nothing is happening.

DisplayRow class

private void DisplayRow(int rowIndex)
    {
        // Check that we can retrieve the given row
        if (myDataTable.Rows.Count == 0)
            return; // nothing to display
        if (rowIndex >= myDataTable.Rows.Count)
            return; // the index is out of range

        // If we get this far then we can retrieve the data
        try
        {
            DataRow row = myDataTable.Rows[rowIndex];
            textBox1.Text = row["FilePath"].ToString();
            textBox2.Text = row["Subject"].ToString();
            textBox3.Text = row["Title"].ToString();
            textBox4.Text = row["Keywords"].ToString();
            textBox5.Text = row["MediaType"].ToString();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error in DisplayRow : \r\n" + ex.Message);
        }

    }

The button which should move on to the next record

private void button1_Click(object sender, EventArgs e)
    {
        if (currentRecord < myDataTable.Rows.Count -1)
        {
            currentRecord++;
            DisplayRow(currentRecord);

        }

But like i say when i run the app nothing happens, no errors just nothing.

EDIT: as asked the coded for MyDataTable

private void Form1_Load(object sender, EventArgs e)
    {
        {
            String command = "SELECT * FROM Media";
            try
            {
                myConnection = new OleDbConnection(access7ConnectionString);
                myAdapter = new OleDbDataAdapter(access7ConnectionString, myConnection);
                myCommandBuilder = new OleDbCommandBuilder(myAdapter);
                myDataTable = new DataTable();
                FillDataTable(command);
                DisplayRow(currentRecord);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

full code what I am running..

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace MediaPlayer
{
public partial class Media : Form
{

    // Use this connection string if your database has the extension .accdb
    private const String access7ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\MediaDatabase.accdb";
    // Use this connection string if your database has the extension .mdb
    private const String access2003ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\MediaDatabase.mdb";
    // Data components
    private DataTable myDataTable;

    // Index of the current record
    private int currentRecord = 0;

    private void FillDataTable(string selectCommand)
    {
        currentRecord = 0;
        OleDbConnection myConnection = new OleDbConnection(access7ConnectionString);
        OleDbDataAdapter myAdapter = new OleDbDataAdapter(selectCommand, myConnection);
        myDataTable = new DataTable();


        try
        {
            myConnection.Open();
            myAdapter.SelectCommand.CommandText = selectCommand;
            myAdapter.Fill(myDataTable);
            myConnection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error in FillDataTable : \r\n" + ex.Message);
        }
        DisplayRow(currentRecord);

    }

    private void DisplayRow(int rowIndex)
    {
        // Check that we can retrieve the given row
        if (myDataTable.Rows.Count == 0)
            return; // nothing to display
        if (rowIndex >= myDataTable.Rows.Count)
            //resets the index to 0 when you get past the last record
            rowIndex = 0;
        //if rowIndex is less then 0 set it to the last row
        if (rowIndex < 0)
            rowIndex = myDataTable.Rows.Count - 1;


        // If we get this far then we can retrieve the data
        try
        {
            DataRow row = myDataTable.Rows[rowIndex];
            textBox1.Text = row["FilePath"].ToString();
            textBox2.Text = row["Subject"].ToString();
            textBox3.Text = row["Title"].ToString();
            textBox4.Text = row["Keywords"].ToString();
            textBox5.Text = row["MediaType"].ToString();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error in DisplayRow : \r\n" + ex.Message);
        }

    }




    public Media()
    {
        InitializeComponent();
        string command = "SELECT * FROM Media";
        //the try catch is in the FillDataTable method
        FillDataTable(command);
    }




    private void label2_Click(object sender, EventArgs e)
    {

    }

    private void button2_Click(object sender, EventArgs e)
    {
        //assuming this cycles through the data
        currentRecord++;
        DisplayRow(currentRecord);
    }

    private void button6_Click(object sender, EventArgs e)
    {
        //assuming this resets the data
        currentRecord = 0;
        this.DisplayRow(currentRecord);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //assuming this cycles through the data
        currentRecord++;
        DisplayRow(currentRecord);
    }
}

}

13
  • 1
    Is button1_click actually being fired? Commented May 9, 2012 at 10:23
  • 1
    Well did you try placing breakpoints maybe the if is evaluating to false ? Commented May 9, 2012 at 10:24
  • did you put some breakpoint somewhere ? Commented May 9, 2012 at 10:24
  • 1
    Put a breakpoint on your button1_click code. Your code should stop there when you click the button. If it doesn't, then your button isn't wired to the event. Commented May 9, 2012 at 10:28
  • 1
    What are your values for currentRecord and myDataTAble.Rows.Count in if (currentRecord < myDataTable.Rows.Count -1)? Commented May 9, 2012 at 10:34

4 Answers 4

1

Place a debugger bookmark on private void DisplayRow(int rowIndex) and see if bookmark get highlighted while debugging the application. if not then check if your loop for calling display row is correct

Sign up to request clarification or add additional context in comments.

Comments

0

The problem is your numbering. I assume that your currentRecord starts with 1,2,3, etc if that's the case you need to use

if (currentRecord <= myDataTable.Rows.Count -1) 

That way, when your currentRecord is 1 it will be less OR EQUAL than (totalcount 2 - 1 = 1)

1 Comment

just tried that then and still nothing. is there anyway to check my databse is loading up all the records, i have opened access and the 2 records are there could it be the database is not updating in visual studio?
0

Here you go I wrote the entire thing for you as discussed in another thread. Hope it works.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace MediaPlayer
{
public partial class Media : Form
{

// Use this connection string if your database has the extension .accdb
private const String access7ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\MediaDatabase.accdb";
// Use this connection string if your database has the extension .mdb
private const String access2003ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\MediaDatabase.mdb";
// Data components
private DataTable myDataTable;

// Index of the current record
private int currentRecord = 0;

private void FillDataTable(string selectCommand)
{
    currentRecord=0;    
    OleDbConnection myConnection = new OleDbConnection(access7ConnectionString);
    OleDbDataAdapter myAdapter = new OleDbDataAdapter(selectCommand, myConnection);
    myDataTable = new DataTable();


    try
    {
        myConnection.Open();
        myAdapter.SelectCommand.CommandText = selectCommand;
        myAdapter.Fill(myDataTable);
        myConnection.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error in FillDataTable : \r\n" + ex.Message);
    }
    DisplayRow(currentRecord);

}

private void DisplayRow(int rowIndex)
{
    // Check that we can retrieve the given row
    if (myDataTable.Rows.Count == 0)
        return; // nothing to display
    if (rowIndex >= myDataTable.Rows.Count)
        //resets the index to 0 when you get past the last record
        rowIndex=0;
    //if rowIndex is less then 0 set it to the last row
    if (rowIndex<0) 
        rowIndex = myDataTable.Rows.Count-1;


    // If we get this far then we can retrieve the data
    try
    {
        DataRow row = myDataTable.Rows[rowIndex];
        textBox1.Text = row["FilePath"].ToString();
        textBox2.Text = row["Subject"].ToString();
        textBox3.Text = row["Title"].ToString();
        textBox4.Text = row["Keywords"].ToString();
        textBox5.Text = row["MediaType"].ToString();
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error in DisplayRow : \r\n" + ex.Message);
    }

}




public Media()
{
    InitializeComponent();
    string command = "SELECT * FROM Media";
    //the try catch is in the FillDataTable method
    FillDataTable(command);
}




private void label2_Click(object sender, EventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{
   //assuming this cycles through the data
    currentRecord++;
    DisplayRow(currentRecord);
}

private void button6_Click(object sender, EventArgs e)
{
   //assuming this resets the data
    currentRecord=0;
    this.DisplayRow(currentRecord);
}
}

1 Comment

No more rolling debug sessions.
0

I added a bunch of MessageBox's to help with debugging, try this and tell me what messages you get?

namespace MediaPlayer
{
public partial class Media : Form
{

    // Use this connection string if your database has the extension .accdb
    private const String access7ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\MediaDatabase.accdb";
    // Use this connection string if your database has the extension .mdb
    private const String access2003ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\MediaDatabase.mdb";
    // Data components
    private DataTable myDataTable;

    // Index of the current record
    private int currentRecord = 0;

    private void FillDataTable(string selectCommand)
    {
        currentRecord = 0;
        OleDbConnection myConnection = new OleDbConnection(access7ConnectionString);
        OleDbDataAdapter myAdapter = new OleDbDataAdapter(selectCommand, myConnection);
        myDataTable = new DataTable();
        try
        {
            myConnection.Open();
            myAdapter.SelectCommand.CommandText = selectCommand;
            myAdapter.Fill(myDataTable);
            myConnection.Close();
            MessageBox.Show("We filled the dataTable");
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error in FillDataTable : \r\n" + ex.Message);
        }
        MessageBox.Show(myDataTable.Rows.Count.ToString());
        DisplayRow(currentRecord);

    }

    private void DisplayRow(int rowIndex)
    {
        // Check that we can retrieve the given row
        if (myDataTable.Rows.Count == 0)
        {
            MessageBox.Show("No rows to Display");
            return; // nothing to display
        }
        if (rowIndex >= myDataTable.Rows.Count)
            //resets the index to 0 when you get past the last record
            rowIndex = 0;
        //if rowIndex is less then 0 set it to the last row
        if (rowIndex < 0)
            rowIndex = myDataTable.Rows.Count - 1;


        // If we get this far then we can retrieve the data
        try
        {
            DataRow row = myDataTable.Rows[rowIndex];
                        textBox1.Text = row["FilePath"].ToString();
                        textBox2.Text = row["Subject"].ToString();
                        textBox3.Text = row["Title"].ToString();
                        textBox4.Text = row["Keywords"].ToString();
                        textBox5.Text = row["MediaType"].ToString();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error in DisplayRow : \r\n" + ex.Message);
        }

    }
    public Media()
    {
        InitializeComponent();
        string command = "SELECT * FROM Media";
        //the try catch is in the FillDataTable method
        FillDataTable(command);
    }


    private void button2_Click(object sender, EventArgs e)
    {
        //assuming this cycles through the data
        currentRecord++;
        DisplayRow(currentRecord);
    }

    private void button6_Click(object sender, EventArgs e)
    {
        //assuming this resets the data
        currentRecord = 0;
        this.DisplayRow(currentRecord);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //assuming this cycles through the data
        currentRecord++;
        DisplayRow(currentRecord);
    }
}

}

4 Comments

CasperOne I understand why you closed my other answer, but I can't access the chat system. It is blocked by my internet provider, if you have another idea I am all ears. If I am breaking a rule by trying to assist this gentleman please let me know and I will stop.
we filled the data table and then 1. guess this means 1 record even though there should be 4 but it is filling the data table.
Yes it is still only retrieving 1 record. so there is still a problem wither with your request or with how you fill your adapter. as I said earlier I don't know ole which is access correct?. I wonder if we can move away from the adaptor and use a DataReader instead? let me play with this for a moment
I have looked this over many times it makes no sense that you are only getting 1 row. I suggest you change the first row in your database( the one you keep getting) and see if it is also changes in your program when you run it. There is something else at play here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.