ok I have a Windows app thats hooked up to an access database and I want the user to type in a football players name in a textbox, push a button and have it select the players info from the database (first name, last name, number, etc) right now i can see the whole table with all the info filled in on the datagrid when I run the app. I can get everything else to work except the select statement.
Loading

Scott LysleRank #49 · 31K reputationPosted Mar 12, 2007, 3:36 PM EDT
What does your code look like (including the select statement you are using)?
Bryan Natusch24 reputationPosted Mar 12, 2007, 5:33 PM EDT
private void button1_Click(object sender, EventArgs e)
{
if (player.Text == "b"){
pic.Visible =
true;bio.Visible =
true;pic.Image =
Image.FromFile("C:\\Documents and Settings\\Brandi\\Desktop\\Ben.jpg");Select * from player where number =
'7';}
}
and its looking for semicolons after player and number
Scott LysleRank #49 · 31K reputationPosted Mar 12, 2007, 6:23 PM EDT
You could give this a shot (correcting the connection string and doing what you want to do with the values returned) -- this is a pretty simple example:
(add something like this in your event handler) --
int playerNum = 7; //set this to whatever number you are looking for
this
.Cursor = Cursors.WaitCursor;OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\YourFolder\YourAccessDb.mdb"); try
{
conn.Open(); string sSql = "SELECT * FROM players WHERE (number='" + playerNum,ToString() + "')";
OleDbCommand cmd = new OleDbCommand(sSql, conn);
OleDbDataReader dr = cmd.ExecuteReader(); if (dr.HasRows == true)
{
while (dr.Read())
{
try
{
// assumes dropping values into a textboxes on a form
playerFirstName.Text = dr.GetValue(0).ToString());
playerLastName.Text = dr.GetValue(1).ToString());
playerHomeTown.Text = dr.GetValue(2).ToString());
}
catch
{
// go on to the next
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString() + " : " + ex.StackTrace.ToString(), "Load Error");
}
finally
{
conn.Close();
this.Cursor = Cursors.Default;
}
Bryan Natusch24 reputationPosted Mar 13, 2007, 4:07 AM EDT
This is the direction I'm headed in-I will have to try it. Do you think I should do away with the datagrid on my form and just use textboxes for the players information to come up? I thought somehow I could just show the particular players row in the grid. I'll try it and get back to you.
Bryan Natusch24 reputationPosted Mar 13, 2007, 7:35 AM EDT
I want it to show his whole row with his first name, last name, experience, date of birth, height, weight number, college and position. I just want to get the basics first and then build on it.
Bryan Natusch24 reputationPosted Mar 13, 2007, 8:05 AM EDT
using
System;using
System.Collections.Generic;using
System.ComponentModel;using
System.Data;using
System.Drawing;using
System.Text;using
System.Windows.Forms;using
System.Data.Sql;namespace
WindowsApplication1{
public partial class SB : Form{
public SB(){
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e){
// TODO: This line of code loads data into the 'steelersDataSet.Player' table. You can move, or remove it, as needed. this.playerTableAdapter.Fill(this.steelersDataSet.Player);}
private void button1_Click(object sender, EventArgs e){
int playerNum = 7; if (player.Text == "b"){
pic.Visible =
true;bio.Visible =
true;pic.Image =
Image.FromFile("C:\\Documents and Settings\\Brandi\\Desktop\\Ben.jpg"); this.Cursor = Cursors.WaitCursor;OleDbConnection conn =
new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\My Documents\Steelers.mdb");try
{
conn.Open();
string
sSql = "SELECT * FROM player WHERE (number='" + playerNum,ToString() + "')";OleDbCommand cmd =
new OleDbCommand(sSql, conn);OleDbDataReader dr = cmd.ExecuteReader();
if (dr.HasRows == true){
while (dr.Read()){
try{
// assumes dropping values into a textboxes on a formplayerFirstName.Text = dr.GetValue(0).ToString());
playerLastName.Text = dr.GetValue(1).ToString());
playerHomeTown.Text = dr.GetValue(2).ToString());
}
catch{
// go on to the next}
}
}
}
catch
(Exception ex){
MessageBox.Show(ex.Message.ToString() + " : " + ex.StackTrace.ToString(), "Load Error");}
finally
{
conn.Close();
this.Cursor = Cursors.Default;}
}
}
private void clear_Click(object sender, EventArgs e){
bio.Visible =
false;pic.Visible =
false;}
}
}
heres the errors:
Error 1 Expected ; or = (cannot specify constructor arguments in declaration) //after playerNum, ToString()
Error 2 ; expected //where + is after ToString
Error 3 ; expected//after ToString on playerFirstName line
Error 4 Invalid expression term ')' //same line
Error 5 ; expected//same errors for all three textboxes
Error 6 Invalid expression term ')'
Error 7 ; expected
Error 8 Invalid expression term ')'
Scott LysleRank #49 · 31K reputationPosted Mar 13, 2007, 2:24 PM EDT
Check the sql string:
string sSql = "SELECT * FROM player WHERE (number='" + playerNum,ToString() + "')";
it should be a period after playerNum (e.g., playerNum.ToString())
You should also have this import:
using
System.Data.OleDb;Scott LysleRank #49 · 31K reputationPosted Mar 13, 2007, 2:25 PM EDT
Also on these:
// assumes dropping values into a textboxes on a form
playerFirstName.Text = dr.GetValue(0).ToString());
playerLastName.Text = dr.GetValue(1).ToString());
playerHomeTown.Text = dr.GetValue(2).ToString());
I just made up the names of the textboxes (playerFirstName.Text, etc). Do you actually have these text boxes in the form?
Bryan Natusch24 reputationPosted Mar 13, 2007, 3:19 PM EDT
Bryan Natusch24 reputationPosted Mar 13, 2007, 3:26 PM EDT
playerFirstName.Text = dr.GetValue(0).ToString());
playerLastName.Text = dr.GetValue(1).ToString());
playerHomeTown.Text = dr.GetValue(2).ToString());
yes i made the text boxes and using these exact names for them.
I appreciate the help Scott.
Bryan Natusch24 reputationPosted Mar 20, 2007, 7:57 AM EDT
It compile and runs fine..but i get a load error when trying to query the database.
Any additional feedback on what direction to take next is appreciated.
Bryan Natusch24 reputationPosted Mar 24, 2007, 8:50 PM EDT
in Number = 7; queries Ben Roethlisberber and so on
whats the proper code to query whatever name is typed in?