【发布时间】:2011-09-29 22:23:31
【问题描述】:
我一直在向tutorial on Home and Learn 学习有关设置数据库的知识。目前,我正在学习如何在数据库中查找记录。我正在努力让我的 GO!按钮在我的数据表中搜索一种成分,我已经彻底按照教程进行操作,并且我的错误列表中没有错误,但是这行代码:
returnRows = dataRecipe.Tables["CookBookRecipes"].Select("Ingredients = '" + searchOut + "'");
它会停止我的程序,并显示以下消息:
对象引用未设置为 对象的实例。
我已经搜索过含义,我猜这意味着我的 returnRows 变量为空,但我不能确定。有人可以帮我解决这个问题吗?
这是我的“搜索”按钮中的完整代码:
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");
}
}
这是我的表单的完整代码:
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
我是一个初学者,如果我不明白真正的问题,请原谅我!
【问题讨论】:
-
您能否发布将 CookBookRecipes 数据表添加到 dataRecipe 数据集的代码。