Greetings,
I have the following code for a button on a Windows form where I am attempting to navigate to the next record. It works, but only if the ID field (of the SQL Server 2005 table) is 1 larger than the previous number. If a record was deleted, however, then the ID field of the next record would not be in sequence. For instance, if the ID field of the current record is 2, then the code below will look for a record with an ID field of 3. If it can't find it, then no record is displayed. [Note: the ID field of the current record is displayed in a text box on the form - during the Load event].
My question is how do I get around this? I'm assuming there is a better way to navigate to the next record than what I am doing, but I'm not sure how to accomplish this.
Any suggestions?
Thanks in advance!
Code:
private void btnNext_Click(object sender, EventArgs e)
{
int intNewID = Convert.ToInt32(this.txtID.Text) + 1;
SqlConnection conn = new SqlConnection("Data Source=HOME-PC\\SQLEXPRESS;Initial Catalog=Contacts;Integrated Security=True");
SqlDataAdapter myAdapter = new SqlDataAdapter("SELECT * FROM tblContacts WHERE ID = " + intNewID, conn);
DataSet dsContacts = new DataSet();
myAdapter.Fill(dsContacts);
this.txtID.Text = dsContacts.Tables[0].Rows[0].ItemArray[0].ToString();
this.txtFirst.Text = dsContacts.Tables[0].Rows[0].ItemArray[1].ToString();
this.txtLast.Text = dsContacts.Tables[0].Rows[0].ItemArray[2].ToString();
this.txtStreet.Text = dsContacts.Tables[0].Rows[0].ItemArray[3].ToString();
this.txtCity.Text = dsContacts.Tables[0].Rows[0].ItemArray[4].ToString();
this.cboState.Text = dsContacts.Tables[0].Rows[0].ItemArray[5].ToString();
this.txtZip.Text = dsContacts.Tables[0].Rows[0].ItemArray[6].ToString();
myAdapter.Dispose();
conn.Close();
...
...
}
I have the following code for a button on a Windows form where I am attempting to navigate to the next record. It works, but only if the ID field (of the SQL Server 2005 table) is 1 larger than the previous number. If a record was deleted, however, then the ID field of the next record would not be in sequence. For instance, if the ID field of the current record is 2, then the code below will look for a record with an ID field of 3. If it can't find it, then no record is displayed. [Note: the ID field of the current record is displayed in a text box on the form - during the Load event].
My question is how do I get around this? I'm assuming there is a better way to navigate to the next record than what I am doing, but I'm not sure how to accomplish this.
Any suggestions?
Thanks in advance!
Code:
private void btnNext_Click(object sender, EventArgs e)
{
int intNewID = Convert.ToInt32(this.txtID.Text) + 1;
SqlConnection conn = new SqlConnection("Data Source=HOME-PC\\SQLEXPRESS;Initial Catalog=Contacts;Integrated Security=True");
SqlDataAdapter myAdapter = new SqlDataAdapter("SELECT * FROM tblContacts WHERE ID = " + intNewID, conn);
DataSet dsContacts = new DataSet();
myAdapter.Fill(dsContacts);
this.txtID.Text = dsContacts.Tables[0].Rows[0].ItemArray[0].ToString();
this.txtFirst.Text = dsContacts.Tables[0].Rows[0].ItemArray[1].ToString();
this.txtLast.Text = dsContacts.Tables[0].Rows[0].ItemArray[2].ToString();
this.txtStreet.Text = dsContacts.Tables[0].Rows[0].ItemArray[3].ToString();
this.txtCity.Text = dsContacts.Tables[0].Rows[0].ItemArray[4].ToString();
this.cboState.Text = dsContacts.Tables[0].Rows[0].ItemArray[5].ToString();
this.txtZip.Text = dsContacts.Tables[0].Rows[0].ItemArray[6].ToString();
myAdapter.Dispose();
conn.Close();
...
...
}