How to extract a specific row information using OleDBReader and in Datagrid?

shyy

Member
Joined
Dec 12, 2013
Messages
6
Programming Experience
Beginner
Hi guys,

I am having trouble extracting particular row information. My database consists of over 60 rows but everytime I try, it shows a particular row information.

What am I doing wrong?

Btn1.Text = reader["MenuDescription"].ToString(); I want this pull information from say row 99

Thanks

C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;


namespace Menu
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();


        }


        private void Form1_Load(object sender, EventArgs e)
        {
            //dataGridView1.AllowUserToAddRows = false;


            dataGridView1.Columns.Add("MenuDescription", "Description");
            dataGridView1.Columns.Add("MenuPrice", "Price");


            OleDbConnection connect = new OleDbConnection(); //Creates the connection
            connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\chewj\Documents\Visual Studio 2012\Projects\Menu\Menu\POS.accdb;Persist Security Info=False;";
            connect.Open();


            OleDbCommand command = new OleDbCommand();
            command.Connection = connect;
            command.CommandText = "SELECT * FROM MenuInventory";


            OleDbDataReader reader = command.ExecuteReader();


            while (reader.Read())
            {               
                Btn1.Text = reader["MenuDescription"].ToString();
                Btn1.Image = Image.FromFile("C:\\Users\\chewj\\Documents\\Visual Studio 2012\\Projects\\Menu\\Menu\\Olive Garden\\Appetizer\\Caprese Flatbread.png");
                
                dataGridView1.Rows.Add();


                dataGridView1.Rows[0].Cells[0].Value = reader["MenuDescription"].ToString();
                dataGridView1.Rows[0].Cells[1].Value = reader["MenuPrice"].ToString();                
            }


            reader.Close();
            connect.Close();
            //MessageBox.Show("Connection Opened");  //Checks to see if the connection is established
        }
    }
}
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,272
Location
Sydney, Australia
Programming Experience
10+
You are setting the Text of the Button for every row. That means that, at the end of the loop, the Text of the Button will contain the data from the last row. If you want data from a specific row then don't take it from every row.

Are you saying that you only want to retrieve one row from the database or that you want to retrieve them all and then display just one? The fact that you're using a grid suggests the latter, in which case you should be using a DataTable. Do you need to edit the data and save changes? If so then you shouldn't be using a data reader at all; you should be using a data adapter and calling Fill to populate a DataTable. If you don't need to save changes then you can stick with a data reader and call Load on a DataTable to populate it from the data reader. You can then index the Rows collection of the DataTable to get the row you want.
 

shyy

Member
Joined
Dec 12, 2013
Messages
6
Programming Experience
Beginner
Thanks for the reply,

The menu form doesn't need to modify the database in any way, just as data source to use. Can you provide me a sample of how the code should look? Mines keep pulling row #4 information (Caprese Flatbread)




Snapshot of how my access table looks




Uploaded with ImageShack.us
 

shyy

Member
Joined
Dec 12, 2013
Messages
6
Programming Experience
Beginner
Thanks! I actually got it to work, however I have new problem. For the menu items that I added, I need to reduce the inventory quantitiy by one for that item everytime I click on a specific button. I will look into the data fill adapter.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,272
Location
Sydney, Australia
Programming Experience
10+
Thanks! I actually got it to work, however I have new problem. For the menu items that I added, I need to reduce the inventory quantitiy by one for that item everytime I click on a specific button. I will look into the data fill adapter.

New problem = new thread
 
Top Bottom