我遇到數據集問題。我使用一個填充表單的文本框。用戶選擇要搜索的值,然後按下按鈕,然後填充文本框。這適用於第一條記錄,但我想通過它們進行翻頁。我已經聲明瞭一個數據集供其他方法使用,但是當它分頁返回的行時,我不斷收到一個Object引用錯誤。任何人都可以闡明我要去哪裏的錯誤?數據集未正確引用
聲明變量:
//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_s12p7.html作爲指導,他們的示例在頁面加載時顯示數據,在選擇搜索參數時顯示我的地址。我不知道這是否是一個問題,但更多的信息從未受到傷害!
之間的區別是什麼,你在做什麼文章顯示的是,隨着的WinForms,物品放置「活着',並可用於表單的持續時間,而對於asp.net他們不是。考慮使用會話或緩存來保持對象。考慮這篇文章的更多細節http://msdn.microsoft.com/en-us/magazine/cc300437.aspx – Ric
好的,謝謝里克。我想這是有道理的。你會碰巧知道他們的任何資源嗎? – Tinydan
這是一篇詳細介紹有關在會話中存儲對象的文章:http://msdn.microsoft.com/en-us/library/ms178581(v=vs.100).aspx它包括讀取和保存對象到會話。 – Ric