我的数据集有问题。我正在使用一个来填充表单的文本框。用户选择要搜索的值,按下按钮,然后填充文本框。这适用于第一条记录,但我想翻阅它们。我已经声明了一个数据集供我的其他方法使用,但是当它对返回的行进行分页时,我不断收到一个对象引用错误。谁能阐明我哪里出错了?
声明变量:
//creates a global dataset for the copies table
DataSet dataSetHM;
//create default row values
int MaxRows = 0;
int inc = 0;
与数据库的连接和填充数据集:
protected void ExecuteSelectCopies(string sName)
{
SqlConnection connection = new SqlConnection(GetConnectionString());
string sql =
"SELECT tblCopies.CopyID, tblSoftware.SoftwareName, tblCopies.AssetName, tblCopies.Location,"
+ " tblCopies.LicenceKey, tblCopies.OEM, tblCopies.State, tblCopies.InstallationDate"
+ " FROM tblCopies"
+ " INNER JOIN tblSoftware ON tblCopies.SoftwareID = tblSoftware.SoftwareID"
+ " WHERE tblSoftware.SoftwareName = @SoftwareName"
+ " ORDER BY tblCopies.CopyID";
SqlDataAdapter adapterHM = new SqlDataAdapter();
SqlCommand select = new SqlCommand(sql, connectionHM);
adapter.SelectCommand = select;
select.Parameters.Add(new SqlParameter("@SoftwareName", sName));
//open the connection
connectionHM.Open();
dataSetHM = new DataSet();
adapterHM.Fill(dataSetHM, "Copies");
NavigateCopies();
MaxRows = dataSetHM.Tables["Copies"].Rows.Count;
connectionHM.Close();
}
填充文本框的方法:
private void NavigateCopies()
{
DataRow dRow = dataSetHM.Tables["Copies"].Rows[inc];
TextBoxCopyID.Text = dRow.ItemArray.GetValue(0).ToString();
TextBoxSoftwareName.Text = dRow.ItemArray.GetValue(1).ToString();
DropDownListAssetName.SelectedItem.Text = dRow.ItemArray.GetValue(2).ToString();
DropDownListLocation.SelectedItem.Text = dRow.ItemArray.GetValue(3).ToString();
TextBoxLicence.Text = dRow.ItemArray.GetValue(4).ToString();
DropDownListType.SelectedItem.Text = dRow.ItemArray.GetValue(5).ToString();
DropDownListState.SelectedItem.Text = dRow.ItemArray.GetValue(6).ToString();
TextBoxInstall.Text = dRow.ItemArray.GetValue(7).ToString();
}
最后是下一条记录的按钮:
protected void ButtonNext_Click(object sender, EventArgs e)
{
if (inc != MaxRows - 1)
{
inc++;
NavigateCopies();
}
else
{
MessageBox.show("No more Rows");
}
}
对此的任何帮助将不胜感激。老实说,我无法理解为什么数据集最初适用于导航方法,但不适用于第二种。我一直在使用此http://www.homeandlearn.co.uk/csharp/csharp/csharp_s12p7.html作为指南,他们的示例在选择搜索参数时显示在页面加载上,在页面加载中显示数据。我不知道这是否是一个问题,但更多的信息永远不会受到伤害!