I'm having some issues with my code. I have 2 buttons, when a user inputs into a textbox, the first button searches the database for a matched name, and then does a SELECT statement with a LIKE clause and pulls up all the records that match what the user entered. It then sets the various column names such as first name, last name, etc to various text boxes to be displayed. The first record is set to the labels as I just said, and any proceeding records are written to their individual list strings, which correspond to their respective columns of the tables. The issue is when I click the 2nd button, which is supposed to allow me to cycle through the remaining records. When I reference the list strings though and set their index to i, they don't contain any data and I get an array out of bounds exception. Is there any easier way to go about viewing the other records that have been matched? The only thing I've been able to do is when you click the second button, I copied most of the connection code from the first button, and have been messing with the first list string but I can't get it to change to the next record. At the bottom of the code 'Fname' is the first list string which corresponds with all of the first names of the records that have been matched. |
<%@ Page Title="Home Page" Language="C#"%><script runat = "server" > List<string> Fname = new List<string>(); List<string> Lname = new List<string>(); List<string> Mname = new List<string>(); List<string> Add1 = new List<string>(); List<string> Add2 = new List<string>(); List<string> sCity = new List<string>(); List<string> sState = new List<string>(); List<string> sZip = new List<string>(); protected void btnFirst_Click(object sender, EventArgs e) { txtFirst.Text = ""; txtMiddle.Text = ""; txtLast.Text = ""; txtAddress1.Text = ""; txtAddress2.Text = ""; txtCity.Text = ""; txtState.Text = ""; txtZip.Text = ""; using (var conn = new System.Data.SqlClient.SqlConnection(@"Server=IDEA-PC\LOCALHOST;Database=Student;Trusted_Connection=True;")) { try { conn.Open(); } catch (Exception ex) { lblSearch.Text = "Connection Error! " + ex; } using (var cmd = new System.Data.SqlClient.SqlCommand("SELECT FirstName, MiddleName, LastName, AddressLine1, AddressLine2, City, State, PostalCode from dbo.Person INNER JOIN dbo.Address on dbo.Person.StudentID = dbo.Address.StudentID " +"where dbo.Person.FirstName LIKE @Name order by FirstName", conn)) { cmd.Parameters.AddWithValue("@Name", "%" + txtSearch.Text + "%"); using (System.Data.SqlClient.SqlDataReader dReader = cmd.ExecuteReader()) { String s = ""; if (txtSearch.Text == s) { lblSearch.Text = "Please enter a name!"; } else{ if (dReader.HasRows) { dReader.Read(); txtFirst.Text = dReader["FirstName"].ToString(); txtMiddle.Text = dReader["MiddleName"].ToString(); txtLast.Text = dReader["LastName"].ToString(); txtAddress1.Text = dReader["AddressLine1"].ToString(); txtAddress2.Text = dReader["AddressLine2"].ToString(); txtCity.Text = dReader["City"].ToString(); txtState.Text = dReader["State"].ToString(); txtZip.Text = dReader["PostalCode"].ToString(); int count = 10; for (int i = 0; count > i; i++) { while (dReader.Read()) { Fname.Add(dReader["FirstName"].ToString()); Lname.Add(dReader["LastName"].ToString()); Mname.Add(dReader["MiddleName"].ToString()); Add1.Add(dReader["AddressLine1"].ToString()); Add2.Add(dReader["AddressLine2"].ToString()); sCity.Add(dReader["City"].ToString()); sState.Add(dReader["State"].ToString()); sZip.Add(dReader["PostalCode"].ToString()); } } } else { lblSearch.Text = "No Data Found!"; } } { } } } } } protected void btnNext_Click(object sender, EventArgs e) { using (var conn = new System.Data.SqlClient.SqlConnection(@"Server=IDEA-PC\LOCALHOST;Database=Student;Trusted_Connection=True;")) { try { conn.Open(); } catch (Exception ex) { lblSearch.Text = "Connection Error! " + ex; } using (var cmd = new System.Data.SqlClient.SqlCommand("SELECT FirstName, MiddleName, LastName, AddressLine1, AddressLine2, City, State, PostalCode from dbo.Person INNER JOIN dbo.Address on dbo.Person.StudentID = dbo.Address.StudentID " +"where dbo.Person.FirstName LIKE @Name order by FirstName", conn)) { cmd.Parameters.AddWithValue("@Name", "%" + txtSearch.Text + "%"); using (System.Data.SqlClient.SqlDataReader dReader = cmd.ExecuteReader()) { { while (dReader.Read()) { Fname.Add(dReader["FirstName"].ToString()); Lname.Add(dReader["LastName"].ToString()); Mname.Add(dReader["MiddleName"].ToString()); Add1.Add(dReader["AddressLine1"].ToString()); Add2.Add(dReader["AddressLine2"].ToString()); sCity.Add(dReader["City"].ToString()); sState.Add(dReader["State"].ToString()); sZip.Add(dReader["PostalCode"].ToString()); int counter = 0; txtSearch.Text = Fname[counter]; counter++; } } } } } }</script>