CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Mar 2005
    Posts
    18

    Problem with a find button

    I'm have a problem with a find button. Here is my code for the button

    Code:
    		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

  2. #2
    Join Date
    Feb 2005
    Location
    Canada
    Posts
    204

    Re: Problem with a find button

    Should be
    Code:
    "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.
    Will Rate Posts for help!

  3. #3
    Join Date
    Mar 2005
    Posts
    18

    Re: Problem with a find button

    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"]);
    Last edited by cmore; March 13th, 2005 at 06:26 PM.

  4. #4
    Join Date
    Feb 2005
    Location
    Canada
    Posts
    204

    Re: Problem with a find button

    Thats because you only have and if not found, no else MessageBox.Show("Record Found"); blah blah
    Will Rate Posts for help!

  5. #5
    Join Date
    Mar 2005
    Posts
    18

    Re: Problem with a find button

    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.

  6. #6
    Join Date
    Mar 2005
    Posts
    18

    Re: Problem with a find button

    Ok I fix the problem with the message box thing here it the code.

    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.

  7. #7
    Join Date
    Feb 2005
    Location
    Canada
    Posts
    204

    Re: Problem with a find button

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

    Your missing your table name?
    Will Rate Posts for help!

  8. #8
    Join Date
    Mar 2005
    Posts
    18

    Re: Problem with a find button

    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.

    Code:
    				//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();
    Last edited by cmore; March 13th, 2005 at 07:23 PM.

  9. #9
    Join Date
    Feb 2005
    Location
    Canada
    Posts
    204

    Re: Problem with a find button

    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.
    Will Rate Posts for help!

  10. #10
    Join Date
    Mar 2005
    Posts
    18

    Re: Problem with a find button

    Ok i checked
    this.oleDbDataAdapter1.Fill(this.dataSet11.Tables["Table1"];

    is it oleDb not OleDb

  11. #11
    Join Date
    Mar 2005
    Posts
    18

    Re: Problem with a find button

    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.

  12. #12
    Join Date
    Feb 2005
    Location
    Canada
    Posts
    204

    Re: Problem with a find button

    Ok good
    Will Rate Posts for help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured