Autoincrementing a value in textbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sureshreddy
    New Member
    • Feb 2007
    • 5

    Autoincrementing a value in textbox

    Hi everyone,

    I am working with image uploading using C# having an id and an image. I am using a textbox for id. I want to increment the value in the textbox automatically. So any one tell me the code for it. I have tried code as below.

    try
    {
    int p;
    p=p+1;
    textbox1.text=p .ToString();
    }
    catch(Exception e)
    {
    p="200";
    }


    But this code is not working.

    Thanks in advance for the solution.
  • Sandeep akhare
    New Member
    • May 2006
    • 29

    #2
    You want increament id oi textbox by one if this is the case
    int id=Parse.Conver t.Int32(textbox .value);
    id +=1 ;

    And convert again into string

    Comment

    • moiashvin
      New Member
      • Sep 2006
      • 16

      #3
      Originally posted by sureshreddy
      Hi everyone,

      I am working with image uploading using C# having an id and an image. I am using a textbox for id. I want to increment the value in the textbox automatically. So any one tell me the code for it. I have tried code as below.

      try
      {
      int p;
      p=p+1;
      textbox1.text=p .ToString();
      }
      catch(Exception e)
      {
      p="200";
      }


      But this code is not working.

      Thanks in advance for the solution.

      Hello buddy,

      I hope this code above is not your real code, since I think it won't even compile.
      Let's me explain. Pls note also that in the catch blog, you are assigning a string to an integer, which is going to result in an error. Also, the variable is only in the scope of the try block and not in the catch block. So the compile will not find the variable in the catch block. And don't forget to initialize the variable p before using it, if it is a method variable.

      Now, concerning the problem of not incrementing correctly, it may be the case that each time the event of incrementing the textbox is occuring, this code is being executed. But since you are declaring the integer p in this block itself, each time it resets and will never have the previous value to increment succesfully.

      The idea is simply to store the variable p outside the blog, may be by making it an instance variable or static variable within the class (don't use a variable). And then incrementing it. It gonna work.

      Hope it helps!

      Comment

      • nmsreddi
        Contributor
        • Jul 2006
        • 366

        #4
        Hello suresh

        Isthe given is working first if its works also it is meaning less to use for auto incrementing

        no where you are using any loop for incrementing the value of the variable.

        i hope it is better to use a timer to increment the variable ,it is not at all the

        problem just try with any loop or timer that will help you


        Regards

        NMsreddi

        Comment

        • sureshreddy
          New Member
          • Feb 2007
          • 5

          #5
          Thanks for the persons who have answered to my question.

          I want the value of the text box to be incremented automatically not just with number. For ex: Prd01 is the first value of the textbox. Next time it should autoincrement Prd02 and so on. Sorry for not sending the question clear. So please send a solution to my problem. Prd is the product and it should be incremented automatically.

          Once again thanks in advance
          Suresh


          Originally posted by nmsreddi
          Hello suresh

          Isthe given is working first if its works also it is meaning less to use for auto incrementing

          no where you are using any loop for incrementing the value of the variable.

          i hope it is better to use a timer to increment the variable ,it is not at all the

          problem just try with any loop or timer that will help you


          Regards

          NMsreddi

          Comment

          • moiashvin
            New Member
            • Sep 2006
            • 16

            #6
            Originally posted by sureshreddy
            Thanks for the persons who have answered to my question.

            I want the value of the text box to be incremented automatically not just with number. For ex: Prd01 is the first value of the textbox. Next time it should autoincrement Prd02 and so on. Sorry for not sending the question clear. So please send a solution to my problem. Prd is the product and it should be incremented automatically.

            Once again thanks in advance
            Suresh
            Hi buddy,

            The logic is the same. Before setting the value in the text box, just concatenate the prefix "Prd" with the number and then set it in the Text property.

            Ashvin

            Comment

            Working...