Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So I am programming a vending machine and the query is for a button to select a product from the vending machine, any help is appreciated. Price, products and productid are all part of the database.

C#
private void runQurey(String query)
       {
           OleDbCommand cmd = new OleDbCommand(query, conn);
           OleDbDataReader cusReader = cmd.ExecuteReader();

           lblprice.Text = "";
           lblquantity.Text = "";

           while (cusReader.Read())
           {
               lblprice.Text = cusReader.GetValue(0).ToString();
               lblquantity.Text = cusReader.GetValue(1).ToString();
           }
       }

       private void btnselect1_Click(object sender, EventArgs e)
       {
           runQurey("SELECT Price FROM Products WHERE ProductID = 1");

       }
Posted
Updated 14-Oct-14 21:38pm
v2

Well...you are requesting one column from your DB:
C#
runQurey("SELECT Price FROM Products WHERE ProductID = 1");

And then trying to process two columns:
C#
lblprice.Text = cusReader.GetValue(0).ToString();
lblquantity.Text = cusReader.GetValue(1).ToString();

I suspect you want to return more columns, so try something like:
C#
runQurey("SELECT Price, Quantity FROM Products WHERE ProductID = 1");
 
Share this answer
 
Comments
CPallini 15-Oct-14 3:38am    
5.
Member 11154485 15-Oct-14 3:53am    
Well I did what you told me too, except now I get an error for this:
private void runQurey(String query)
{
OleDbCommand cmd = new OleDbCommand(query, conn);
OleDbDataReader cusReader = cmd.ExecuteReader(); <----------

It says "No value given for one or more required parameters"

BTW I am really new to this, I don't even know most of this, I am extremely sorry for the hassle as I am a really really new newbie
OriginalGriff 15-Oct-14 4:01am    
"No value given for one or more required parameters"
Means you typed it wrong! :laugh:
Check the runQuery line and make sure you know exactly what is in the string you are passing to sql: chances are you typed a "@" in there somewhere.
If you can't see it, paste your code fragment here.
Member 11154485 15-Oct-14 4:47am    
help me plzzzz i can't find :'(

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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
OleDbConnection conn = new OleDbConnection();
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
+ "D:\\akhan38\\My Documents\\School\\IPT\\VendingMachine.accdb;");

try
{
conn.Open();
}
catch (Exception ex)
{
MessageBox.Show("Failed to connect to data source");
}
}
public double money;

private void button14_Click(object sender, EventArgs e)
{
money = money + 5.00;
label1.Text = money.ToString();
}

private void button15_Click(object sender, EventArgs e)
{
money = money + 10.00;
label1.Text = money.ToString();
}

private void btnvalue1_Click_1(object sender, EventArgs e)
{
money = money + 1.00;
label1.Text = money.ToString();
}

private void btnvalue2_Click(object sender, EventArgs e)
{
money = money + 2.00;
label1.Text = money.ToString();
}

private void runQurey(String query)
{
OleDbCommand cmd = new OleDbCommand(query, conn);
OleDbDataReader cusReader = cmd.ExecuteReader();

lblprice.Text = "";
lblquantity.Text = "";

while (cusReader.Read())
{
lblprice.Text = cusReader.GetValue(0).ToString();
lblquantity.Text = cusReader.GetValue(1).ToString();
}
}

private void btnselect1_Click(object sender, EventArgs e)
{
runQurey("SELECT Price FROM Products WHERE ProductID = 1");

}
}
}


OriginalGriff 15-Oct-14 4:58am    
That's your original, unmodified code: it still reads one column (Price) from your Products table and tried to access two different values per row (Price and Quantity).
Check your column names in the DB, and make the change exactly as I show above.
your query is not correct,bcoz u didn't take quantity value from ur table but u r bindinding quantity value from reader .Ur datareader is holding only price alone not quantity value
 
Share this answer
 
Your query is not right.Add 2 columns name if you want two value in select query.
 
Share this answer
 

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