Click to See Complete Forum and Search --> : Problem with a find button


cmore
March 13th, 2005, 04:06 PM
I'm have a problem with a find button. Here is my code for the button


private void btnFind_Click(object sender, System.EventArgs e)
{
try
{
this.oleDbDataAdapter1.SelectCommand.CommandText =
"SELECT * FROM Table1 WHERE firstname = "+ this.txtFirstName.Text;

//clear the dataset from the last operation
dataSet11.Clear();

this.oleDbDataAdapter1.Fill(this.dataSet11.Tables["Table1"]);

}

catch(System.Data.OleDb.OleDbException exp)
{
MessageBox.Show(exp.ToString());
}

//copy the dataset in datatable object
DataTable dataTable = dataSet11.Tables[ 0 ];

//if the row count = 0 then the qurey return nothing
if ( dataTable.Rows.Count == 0 )
MessageBox.Show("Record not founded");

}



The error im getting is
System.Data.OleDb.OleDbExepetion: No value given one or more required parameters.
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(int32hr)
at Sytem.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResults)
***bunch more things like this***
and the last line says this
at windowsApplication4.Form1.btnFind_Click(Object sender, EventArgs e) in C:\documents and settings\owner\my documents\visual studio projects\windowsapplication4\windowsaplication4\form1.cs : line 294

Ok the line 294 is:
this.oleDbDataAdapter1.Fill(this.dataSet11.Tables["Table1"]);

If any one can help thanks

Ctwizzy
March 13th, 2005, 04:55 PM
Should be
"SELECT * FROM Table1 WHERE firstname = '"+ this.txtFirstName.Text + "'";

Your missing your ' '

Because if first name was Joe Shmoe, then your entering in more than 1 value hence the exception. Encase it in ' ' and your fine.

Hope this helped.

cmore
March 13th, 2005, 05:12 PM
Ok i put in the ' ' and now if i type a name that is in my database it does not fill my other fill. If i put some name that is not in the database i get my messagebox saying Record dont found. I think the problem now it the Fill command:

this.oleDbDataAdapter1.Fill(this.dataSet11.Tables["Table1"]);

Ctwizzy
March 13th, 2005, 05:26 PM
Thats because you only have and if not found, no else MessageBox.Show("Record Found"); blah blah

cmore
March 13th, 2005, 05:44 PM
As you can tell im a newbie to this programming so could you tell my how to write a if else statement. I've tried this:

if (dataTable.Rows.Count == 1)
MesssageBox.Show ("Record Found")
else (dataTable.Rows.Count == 0)
MessageBox.Show ("Record Not Found");

but this does not work.

cmore
March 13th, 2005, 05:50 PM
Ok I fix the problem with the message box thing here it the code.

if (dataTable.Rows.Count == 0)
{
MessageBox.Show("Record not founded");
}
else
{
MessageBox.Show("Record Found");
}


But it does not fill in the fill with the info that is found.

Ctwizzy
March 13th, 2005, 06:05 PM
this.oleDbDataAdapter1.Fill(this.dataSet11.Tables["Table1"]), "Tablename";

Your missing your table name?

cmore
March 13th, 2005, 06:14 PM
this.oleDbDataAdapter1.Fill(this.dataSet11.Tables["Table1"]),

My table name is Table1 so should the code look like this

this.oleDbDataAdapter1.Fill(this.dataSet11.Tables["Table1"]), "Table1";

Look at the code above this i dont think it is work either.


//clear the dataset from the last operation
dataSet11.Clear();

this.oleDbDataAdapter1.Fill(this.dataSet11.Tables["Table1"]);





should the first line of code be some thing like this
this.dataSet11.Clear();

Ctwizzy
March 13th, 2005, 06:36 PM
this.oleDbDataAdapter1.Fill(this.dataSet11, "Table1");

This will fill in your query into the dataset into Table1
check the name of the DataAdapter something is telling me it should be
OleDbDataAdapter1 assuming you used the toolbox.

The clear line is fine.

cmore
March 13th, 2005, 06:47 PM
Ok i checked
this.oleDbDataAdapter1.Fill(this.dataSet11.Tables["Table1"];

is it oleDb not OleDb

cmore
March 13th, 2005, 06:57 PM
Ok i fixed the problem i forgot to go in the property in the form to bind the txtFirstName to my firstname in my database. It work great thanks for all the help.

Ctwizzy
March 13th, 2005, 07:22 PM
Ok good :)