Problem comparing database values with textbox values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sharma Neha
    New Member
    • Jul 2008
    • 22

    Problem comparing database values with textbox values

    Code:
    SqlConnection cn = new SqlConnection();
    cn.ConnectionString = "Data Source= ;Database= ;uid= ;pwd= ";
    cn.Open();
    string sqt = "Select status from tracking where no=@o";
    SqlCommand cmr = new SqlCommand(sqt, cn);
    cmr.Parameters.AddWithValue("@o", Convert.ToInt32(textBox1.Text));
    status = Convert.ToString(cmr.ExecuteScalar());
    if (status == "abcd")
        MessageBox.Show("Record updated");
    else
         MessageBox.Show("Record not updated");
    cn.Close();
    I hav one table named tracking with no,status as its feild.the data type of status is varchar.
    I want that when status is abcd then it should show the mess. record updated else the other one mess.
    But when i m debugging it,even if i m giving the no for which status is abcd,it is showing the else mess.
    The main problem is that if condition always evaluates to false even if status in database is abcd.
    plzzzz reply soon.thanks for support.
    Last edited by Curtis Rutland; Jul 27 '08, 10:10 PM. Reason: Added code tags -- Please use the # button
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Code tags are a requirement.

    Please read and follow the Posting Guidelines.

    MODERATOR

    Perhaps the value you are getting from the DB has spaces or something.

    Comment

    • vanc
      Recognized Expert New Member
      • Mar 2007
      • 211

      #3
      Originally posted by Sharma Neha
      SqlConnection cn = new SqlConnection() ;
      cn.ConnectionSt ring = "Data Source= ;Database= ;uid= ;pwd= ";
      cn.Open();
      string sqt = "Select status from tracking where no=@o";
      SqlCommand cmr = new SqlCommand(sqt, cn);
      cmr.Parameters. AddWithValue("@ o", Convert.ToInt32 (textBox1.Text) );
      status = Convert.ToStrin g(cmr.ExecuteSc alar());
      Couldn't see any problems but your queries. Try to make some straight queries to check status data to see if there is any problem and try different type of data execution. Like below, I'll use DataReader to get data.

      Code:
      cmd.CommandText = "Select status from tracking where no=" + int.Parse(textBox1.Text);
      SqlDataReader reader = cmd.ExecuteQuery(); //recheck for this statement since I can't recall the right command
      reader.Read();
      status = reader["status"].ToString();
      reader.Close();
      Cheers.

      Comment

      • Sharma Neha
        New Member
        • Jul 2008
        • 22

        #4
        Originally posted by vanc
        Couldn't see any problems but your queries. Try to make some straight queries to check status data to see if there is any problem and try different type of data execution. Like below, I'll use DataReader to get data.

        Code:
        cmd.CommandText = "Select status from tracking where no=" + int.Parse(textBox1.Text);
        SqlDataReader reader = cmd.ExecuteQuery(); //recheck for this statement since I can't recall the right command
        reader.Read();
        status = reader["status"].ToString();
        reader.Close();
        Cheers.
        This code is not working.plz suggest something else.Error is coming in this code.

        Comment

        • Sharma Neha
          New Member
          • Jul 2008
          • 22

          #5
          Originally posted by insertAlias
          Code tags are a requirement.

          Please read and follow the Posting Guidelines.

          MODERATOR

          Perhaps the value you are getting from the DB has spaces or something.
          Thanks for help.
          You r talking about spaces in DB,so can u plz tell me what should i do so that my code works.
          plz reply soon....

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by Sharma Neha
            Thanks for help.
            You r talking about spaces in DB,so can u plz tell me what should i do so that my code works.
            plz reply soon....
            If the DB values contain spaces then you can use the TRIM function to get rid of them.

            Comment

            • Sharma Neha
              New Member
              • Jul 2008
              • 22

              #7
              Originally posted by r035198x
              If the DB values contain spaces then you can use the TRIM function to get rid of them.
              In database there is no problem of space,but the code is not working.Its not the problem of space in database,even Datareader is giving same output as before (executing else part),plzzz help.

              Comment

              • Sharma Neha
                New Member
                • Jul 2008
                • 22

                #8
                Originally posted by Sharma Neha
                This code is not working.plz suggest something else.Error is coming in this code.
                Datareader is same output as before.

                Comment

                • Curtis Rutland
                  Recognized Expert Specialist
                  • Apr 2008
                  • 3264

                  #9
                  OK...try this. After you do your select, instead of doing a comparison, show a MessageBox like this:
                  Code:
                  MessageBox.Show("Status Value: -" + status + "-");
                  That way, you can see exactly what the value is that your program is getting (I added the "-" so you can see if there are any leading/trailing spaces). This should help you debug the problem further.

                  Comment

                  • Sharma Neha
                    New Member
                    • Jul 2008
                    • 22

                    #10
                    Originally posted by insertAlias
                    OK...try this. After you do your select, instead of doing a comparison, show a MessageBox like this:
                    Code:
                    MessageBox.Show("Status Value: -" + status + "-");
                    That way, you can see exactly what the value is that your program is getting (I added the "-" so you can see if there are any leading/trailing spaces). This should help you debug the problem further.
                    The only change i made now as compared to before is that i changed the data type of status as text.but it was not working then also.
                    Then I only added this line(u send me) in my code n kept the type as text.The mess.box is showing status: -abcd-.and then i added the if condition same as before and it started working.But still now i dont know the correct reason why it was not executing before.Thanks a lot for the support.
                    thank u all for the support n timely response.

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by Sharma Neha
                      ..But still now i dont know the correct reason why it was not executing before....
                      Perhaps it was just build issue then. (i.e your new code wasn't being automatically picked).
                      Good luck with the rest of it.

                      Comment

                      Working...