Note that there are some explanatory texts on larger screens.

plurals
  1. POData set Not referencing correctly
    primarykey
    data
    text
    <p>I'm having a problem with data set. I'm using one to populate text boxes of my form. The user selects a value to search from, they press a button and then the text boxes are populated. This works fine for the first record but I want to page through them. I've declared a data set to be used by my other methods but when it gets to paging the returned rows I keep getting an Object reference error. Can anyone shed some light on where I'm going wrong? </p> <p>Declaring Variables:</p> <pre><code>//creates a global dataset for the copies table DataSet dataSetHM; //create default row values int MaxRows = 0; int inc = 0; </code></pre> <p>The connection to the database and filling the data set:</p> <pre><code>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(); } </code></pre> <p>The method for filling the text boxes:</p> <pre><code> 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(); } </code></pre> <p>And finally the button for the next record:</p> <pre><code> protected void ButtonNext_Click(object sender, EventArgs e) { if (inc != MaxRows - 1) { inc++; NavigateCopies(); } else { MessageBox.show("No more Rows"); } } </code></pre> <p>Any help with this would be very much appreciated. I honestly can't understand why the data set works for the navigation method initially but not for the second. I've been using this <a href="http://www.homeandlearn.co.uk/csharp/csharp_s12p7.html" rel="nofollow">http://www.homeandlearn.co.uk/csharp/csharp_s12p7.html</a> as a guide, their example displays data on page load where as mine is displayed when a search parameter is selected. I don't know if this is a problem but a bit more information never hurts!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload