Finding Records in a Database using a Textbox and Button

随声附和 提交于 2019-12-24 09:58:11

问题


I have been learning from a tutorial on Home and Learn about setting up databases. Currently, I'm learning about finding records in a database. I'm trying to get my GO! button search for a ingredient in my data table, and I"ve followed the tutorial thoroughly and have no errors in my Error list, but this line of code:

returnRows = dataRecipe.Tables["CookBookRecipes"].Select("Ingredients = '" + searchOut + "'");

It stops my program, and brings up this message:

Object reference not set to an instance of an object.

I've searched the meaning, and I guess it means my returnRows variable is null, but I can't be sure. Can someone help me fix this problem?

Here is my full code in my Search button:

System.Data.SqlClient.SqlConnection con;
System.Data.SqlClient.SqlDataAdapter dataAdapt;
DataSet dataRecipe;

private void btnSearch_Click(object sender, EventArgs e)
{

    if (tbSearch.TextLength >= 1)
   {
        MessageBox.Show("This will work when you put it a word!");

        // Search code //

        string searchOut = tbSearch.Text;
        int result = 0;
        DataRow[] returnRows;

        returnRows = dataRecipe.Tables["CookBookRecipes"].Select("Ingredients = '" + searchOut + "'");


        result = returnRows.Length;

        if (result > 0)
        {
            DataRow rowBack;
            rowBack = returnRows[0];
            MessageBox.Show(rowBack[3].ToString());
        }
        else
        {
            MessageBox.Show("No such record");
        }

   }

   else
    {
       MessageBox.Show("Please enter an ingredient to search for!", "Search");
   }
}

Here is the full code of my form:

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;
namespace Cookbook {
    public partial class BrowseIngredients : Form {
       public BrowseIngredients() { InitializeComponent(); }
       private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
           if (MessageBox.Show("Exit Cook Book?", "Exit?", MessageBoxButtons.OKCanc

I am very much a beginner, so forgive me if I don't understand the real problem!


回答1:


Looking at the tutorial you are using I think I can see where you have gone wrong (and I don't blame you the - tutorial is not entirely clear).

Based on this stage : http://www.homeandlearn.co.uk/csharp/csharp_s12p5.html

You will have in your Form Load method a statement something like:

string sql = "SELECT * From CookBookRecipes";
dataAdapt = new System.Data.SqlClient.SqlDataAdapter(sql, con);

However, the tutorial then tells you to add the following to get the data from the data adapter to into the dataset, but doesn't actually show the code in context - and I suspect this is where you have gone wrong.

Immediately after the above lines you need to add this:

dataAdapt.Fill( dataRecipe, "CookBookRecipes" );

Then when you attempt to get the rows from dataRecipe.Tables["CookBookRecipes"] you will not be returning a reference to a null object.




回答2:


Looks like you might not be instantiating your data set. You declare it but you aren't instantiating it, unless you do it elsewhere in your code that you haven't shown us.

DataSet dataRecipe;

You could instatiate it in the page load event and that would be fine.

private void Form_Load(object sender, EventArgs e)
{
    dataRecipe = new DataSet();
}


来源:https://stackoverflow.com/questions/6602079/finding-records-in-a-database-using-a-textbox-and-button

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!