locked
Code Not Functioning Properly RRS feed

  • Question

  • User1402230106 posted

    It is probably a coding issue on my end, but here is the deal.  Any time the Next button is pressed it should pull the next avaliable records from the database and display works great!  The issue is when my previous button is pressed, it skips an ID then displays incorrect information.  So for example, let's say Joe Jones is ID 133, Mike Hale is ID 135, Jack Black is ID 137.  Pressing the Next button iterates correctly, going 133, 135, 137, but if I am on record 137, and I press previous, the ID field changes to ID 135, but the data doesn't.  So I press the previous button again, and the ID again changes this time to 133, but the data is showing for Mike Hale!  So the ID Count is off.  To counter-act this when I press the Next button the ID changes to 135, then if I press next again, it changes to 137, but showing accurate information (Jack Black).  What in the world did I blow in this coding?  (BTW, if someone see's a better way to code this let me know as well as I am newbie, and have gotten much help from the people here)

    private void Previous_Click(object sender, EventArgs e)
    		{			
    			using (SqlConnection connection = new SqlConnection(connectionString))
    			{
    				string queryString = "SELECT TOP 1 ID, firstName, lastname, Phone, from AdminInfo where ID > '" + txtID.Text + "' ORDER BY ID ASC";
    				SqlCommand cmd = new SqlCommand(queryString, connection);
    				connection.Open();
    				SqlDataReader dr = cmd.ExecuteReader();
    				if (dr.HasRows)
    				{
    					while (dr.Read())
    					{
    						txtFName.Text = dr["FirstName"].ToString();
    						txtLName.Text = dr["LastName"].ToString();
    						txtPhone.Text = dr["Phone"].ToString();
    						int intID = Convert.ToInt32(txtID.Text);
    						int intID = Convert.ToInt32(txtID.Text);
    						intID = intID - 1;
    						txtID.Text = Convert.ToString(intID);
    					}
    				}                
    				else
    				{
    					string MINQuery = "SELECT MIN(ID) FROM tbl_HP_Reporting";
    					SqlCommand myCommand = new SqlCommand(MINQuery, connection);
    					int minID = Convert.ToInt32(myCommand.ExecuteScalar());
    					if (minID >= Convert.ToInt32(txtID.Text))
    					{
    						MessageBox.Show("This is the first record in the database", "Out of Records");
    						txtID.Text = txtID.Text;
    					}
    				}
    					dr.Close();
    					connection.Close();
    			}		
    		}
    		
    		private void Next_Click(object sender, EventArgs e)
    		{
    			using (SqlConnection connection = new SqlConnection(Global.Variables.GlobalVariables.connectionString))
    			{
    				string queryString = "SELECT TOP 1 ID, firstName, lastname, Phone, from AdminInfo where ID > '" + txtID.Text + "' ORDER BY ID ASC";
    				SqlCommand cmd = new SqlCommand(queryString, connection);
    				connection.Open();
    				SqlDataReader dr = cmd.ExecuteReader();
    				if (dr.HasRows)
    				{
    					while (dr.Read())
    					{
    						txtFName.Text = dr["FirstName"].ToString();
    						txtLName.Text = dr["LastName"].ToString();
    						txtPhone.Text = dr["Phone"].ToString();
    						int intID = Convert.ToInt32(txtID.Text);
    						intID = intID + 1;
    						txtID.Text = Convert.ToString(intID);
    					}
    				}
    				else
    				{
    					string MAXQuery = "SELECT MAX(ID) FROM tbl_HP_Reporting";
    					SqlCommand myCommand = new SqlCommand(MAXQuery, connection);
    					int maxId = Convert.ToInt32(myCommand.ExecuteScalar());
    					if (maxId >= Convert.ToInt32(txtID.Text))
    					{						
    						MessageBox.Show("This is the last record in the database", "Out of Records");
    						txtID.Text = txtID.Text;
    					}
    				}
    					dr.Close();
    					connection.Close();
    			}	
    		}



    Thursday, February 28, 2013 10:52 AM

Answers

  • User1808797856 posted

    hello dear,

    First u should define the condition in side the while or you can use the if instead of while...like this example

    try
                    {
                        string id = Page.Request.QueryString["ID"].ToString();  //Your Query String

                        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CS"].ConnectionString);
                        con.Open()

                  SqlCommand cmSQL = new SqlCommand("SELECT  firstName, lastname from AdminInfo  where ID='" + id.ToString() + "'", con);
                        SqlDataReader drSQL = cmSQL.ExecuteReader();
                        if (drSQL.Read() == true)
                        {
                            txtFName.Text = dr["FirstName"].ToString();
                            txtLName.Text = dr["LastName"].ToString();
                       
                        }
                        cmSQL.Dispose();
                        drSQL.Dispose();
                        con.Close();
                        con.Dispose();

                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }

    define your code in this way.....

    this will help you

    Happy programming Cool

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 1, 2013 7:30 PM

All replies

  • User-1590642642 posted

    Seems to me in the previous_click event, the querystring should be this:

     string queryString = "SELECT TOP 1 ID, firstName, lastname, Phone, from AdminInfo where ID < '" + txtID.Text + "' ORDER BY ID DESC";

    Thursday, February 28, 2013 11:43 AM
  • User1402230106 posted

    That was a simple slip of a finger when typing the code into here, but good catch mate.

    Thursday, February 28, 2013 4:50 PM
  • User1808797856 posted

    hello dear,

    First u should define the condition in side the while or you can use the if instead of while...like this example

    try
                    {
                        string id = Page.Request.QueryString["ID"].ToString();  //Your Query String

                        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CS"].ConnectionString);
                        con.Open()

                  SqlCommand cmSQL = new SqlCommand("SELECT  firstName, lastname from AdminInfo  where ID='" + id.ToString() + "'", con);
                        SqlDataReader drSQL = cmSQL.ExecuteReader();
                        if (drSQL.Read() == true)
                        {
                            txtFName.Text = dr["FirstName"].ToString();
                            txtLName.Text = dr["LastName"].ToString();
                       
                        }
                        cmSQL.Dispose();
                        drSQL.Dispose();
                        con.Close();
                        con.Dispose();

                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }

    define your code in this way.....

    this will help you

    Happy programming Cool

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 1, 2013 7:30 PM