Click here to Skip to main content
16,022,060 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an access database JET.4.0 ha table of three columns station, offset & elevation station entered from a textBox1, offset & elevation entered from a dataGridView1 after saving enough records I used button1 to save record to database, button2 & button3 to navigate records back & forward. When I compile the code nothing is displayed.

What I have tried:

public partial class Form1 : Form
    {
        private OleDbConnection connection;
         private string connectionString = @"Provider=Microsoft.JET.OLEDB.4.0;Data Source=..\..\..\Database.mdb;";
        private int currentStationIndex = 0;
        private List<object> uniqueStations;
        private DataTable allData;

        public Form1()
        {
            InitializeComponent();
            Text = "Data Entry";
            //dataGridView1.ColumnCount = 2;
           // dataGridView1.RowCount = 12;
            connection = new OleDbConnection(connectionString);
            LoadAllRecords();

        }
        private void LoadAllRecords()
        {
            try
            {
                using (OleDbCommand cmd = new OleDbCommand("SELECT * FROM TableName ORDER BY Station", connection))
                {
                    OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
                    allData = new DataTable();
                    adapter.Fill(allData);

                    // Get unique stations
                    uniqueStations = allData.AsEnumerable()
                        .Select(row => row.Field<object>("Station"))
                        .Distinct()
                        .ToList();
                }
                DisplayCurrentStation();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error loading records: {ex.Message}\n\nStack Trace: {ex.StackTrace}");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                using (OleDbCommand cmd = new OleDbCommand("INSERT INTO TableName (Station, Offset, Elevation) VALUES (@Station, @Offset, @Elevation)", connection))
                {
                    connection.Open();

                    for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                    {
                        cmd.Parameters.Clear();
                        cmd.Parameters.AddWithValue("@Station", textBox1.Text);
                        cmd.Parameters.AddWithValue("@Offset", dataGridView1.Rows[i].Cells[0].Value ?? DBNull.Value);
                        cmd.Parameters.AddWithValue("@Elevation", dataGridView1.Rows[i].Cells[1].Value ?? DBNull.Value);
                        cmd.ExecuteNonQuery();
                    }

                    MessageBox.Show("Data saved successfully!");

                    // Increase station by the interval
                    double currentStation = double.Parse(textBox1.Text);
                    double interval = double.Parse(textBox2.Text);
                    textBox1.Text = (currentStation + interval).ToString();

                    // Clear the DataGridView
                    dataGridView1.Rows.Clear();

                    // Reload records
                    LoadAllRecords();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error saving data: {ex.Message}\n\nStack Trace: {ex.StackTrace}");
            }
            finally
            {
                if (connection.State == ConnectionState.Open)
                    connection.Close();
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            // Show previous station
            if (currentStationIndex > 0)
            {
                currentStationIndex--;
                DisplayCurrentStation();
            }
            else
            {
                MessageBox.Show("You are at the beginning of the stations.");
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            // Show next station
            if (currentStationIndex < uniqueStations.Count - 1)
            {
                currentStationIndex++;
                DisplayCurrentStation();
            }
            else
            {
                MessageBox.Show("You have reached the end of the stations.");
            }
        }
        private void DisplayCurrentStation()
        {
            if (uniqueStations.Count > 0 && currentStationIndex >= 0 && currentStationIndex < uniqueStations.Count)
            {
                object currentStation = uniqueStations[currentStationIndex];
                textBox1.Text = currentStation.ToString();
                var stationData = allData.AsEnumerable()
                    .Where(row => row.Field<object>("Station") == currentStation)
                    .Select(row => new {
                        Offset = row.Field<double>("Offset"),
                        Elevation = row.Field<double>("Elevation")
                    }).ToList();
                dataGridView1.DataSource = stationData;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // dataGridView1.AutoGenerateColumns = true;
            dataGridView1.Columns[0].Width = 140;
            dataGridView1.Columns[1].Width = 140;
        }
    }
Posted
Comments
M Imran Ansari 3 days ago    
You code looks fine, debugging step-by-step can help you to find out the issue. Ensure LoadAllRecords is being called and if uniqueStations contains data.

1 solution

At a guess - and without your data we can't run your app to check - it's this bit:
SQL
"SELECT * FROM TableName ORDER BY Station"
Did you really call your table "TableName" or did you forget to change that to match your DB?
 
Share this answer
 
Comments
essukki 2 days ago    
Yes this is the real name of my table "TableName":

station offset elevation
1000.0 20.0 450.2
1000.0 30.0 467.9
1000.0 40.0 501.8
1020.0 20.0 556.2
1020.0 30.0 569.3
1020.0 40.0 591.5
1040.0 20.0 610.2
1040.0 30.0 627.7
1040.0 40.0 631.2
1040.0 60.0 661.2
essukki 2 days ago    
Thank you for your interest, also I would like to reassure you that the connection to the database was completed successfully.
Even browsing for station also was completed.
OriginalGriff 2 days ago    
So what does the debugger show you is happening?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900