i almost got my program working- i can query the database but it only comes up with the information that its set to. for example:
int number = 7; will give me the information for the player with the number 7. int number =86; will give me the info for that player so i know its grabbing from the access database and giving me the information back.
what i want it to do is be able to read the players full name from the text box and grab the information that way. what would be the syntax?help is very very much appreciated.
thanks in advance.
heres all my code:
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;
using System.Data.OleDb;
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 number = 43;
if (player.Text == "b")
{
pic.Visible = true;
helmet.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:\Documents and Settings\Brandi\My Documents\Steelers.mdb");
try
{
conn.Open();
string sSql = "SELECT * FROM player WHERE number=" +number.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
bio.Text = "Name: " + dr.GetValue(0).ToString() + " " + dr.GetValue(1).ToString() + " \nNumber: " + dr.GetValue(2).ToString() + "\nPosition: " + dr.GetValue(3).ToString() + "\nDate Of Birth: " + dr.GetValue(4).ToString() + " \nExperience: " + dr.GetValue(5).ToString() + " \nCollege:" + dr.GetValue(6).ToString() + " \nHeight: " + dr.GetValue(7).ToString() + " \nWeight:" + dr.GetValue(8).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)
{
pic.Visible = false;
player.Clear();
bio.Visible = false;
helmet.Visible = false;
}
}
}
Loading
Bryan NatuschPosted Mar 26, 2007, 10:45 AM
here how i have my code:
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;using
System.Data.OleDb;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);}
string mFirstName = string.Empty; string mLastName = string.Empty; string mSql = string.Empty; private void button1_Click(object sender, EventArgs e){
if (player.Text != string.Empty){
string[] arrName = player.Text.Split(' ');mFirstName = arrName[0].ToString();
mLastName = arrName[1].ToString();
this.Cursor = Cursors.WaitCursor; OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Brandi\My Documents\Steelers.mdb"); try{
conn.Open();
string mSql = "SELECT * FROM player WHERE firstName='" + mFirstName +"' AND lastName='" + mLastName + "'";
OleDbCommand cmd = new OleDbCommand(mSql, conn); OleDbDataReader dr = cmd.ExecuteReader(); if (dr.HasRows == true){
while (dr.Read()){
try{
// assumes dropping values into a textboxes on a formbio.Text =
"Name: " + dr.GetValue(0).ToString() + " " + dr.GetValue(1).ToString() + " \nNumber: " + dr.GetValue(2).ToString() + "\nPosition: " + dr.GetValue(3).ToString() + "\nDate Of Birth: " + dr.GetValue(4).ToString() + " \nExperience: " + dr.GetValue(5).ToString() + " \nCollege:" + dr.GetValue(6).ToString() + " \nHeight: " + dr.GetValue(7).ToString() + " \nWeight:" + dr.GetValue(8).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){
pic.Visible =
false;player.Clear();
bio.Visible =
false;helmet.Visible =
false;}
}
}
Scott LyslePosted Mar 26, 2007, 1:49 AM
Set up some variables to hold the values collected:
mFirstName = string.Empty;string
string mLastName = string.Empty;
string mSql = string.Empty;
Split the content of the textbox text holding the player's name into a string array and then format your SQL statement:
You might want to use a like instead of an equal to in the WHERE clause in case the spelling does not match. Better yet, use a first name and a separate last name textbox because if the player has a name like "Chi Chi" the first name will be set to "Chi" and the last name will be set to "Chi".
if
(textBox1.Text != string.Empty){
string[] arrName = textBox1.Text.Split(' ');
mFirstName = arrName[0].ToString();
mLastName = arrName[1].ToString();
mSql =
"SELECT * FROM player WHERE firstName='" + mFirstName +"' AND lastName='" + mLastName + "'";
}
else
{
MessageBox.Show("Empty name field");
}
Bryan NatuschPosted Mar 25, 2007, 10:39 PM
Scott LyslePosted Mar 25, 2007, 10:30 PM
You'd said you want to query by the player's full name; in your database is the name stored as the first name, last name, and maybe the middle name or is it all in one column? When you say that you want to get the player's full name, is that from a single textbox or is from a text box for the first name, middle, and last names?