none
C# put data from database into an array RRS feed

  • Question

  • how would i make an array that stores the idnumbers and give them 3 attempts each?

    i started off like int[] attemps = new int[5]; but i havent used array for a long time and i never used them to store data from a database before.

    hope someone can guide me on this :)

    Thursday, March 1, 2012 4:51 PM

Answers

  • Hello Welshy,

    For your scenario, I prefer you use Dictionary instead of arrays.

    It provides a better and convenient way for storing and manipulating key-value pairs.

    Your id could be the key and attempt could be the value.

    For example:

                Dictionary<int, int> dict = new Dictionary<int, int>();
    
                // Init
                dict.Add(1234, 3);
                dict.Add(4456, 3);
    
                // On attempts, reduce the count based on id
                dict[1234] -= 1;
    
                // Check if dict has a key
                bool exists = dict.ContainsKey(1111);
    

    Anyways.. you can have an array implementation as given below:

                int[,] array = new int[10,1];
                array[0,0] = 1234; 
                array[0,1] = 3;
    
                array[1, 0] = 4456;
                array[1, 1] = 3;


    Resolving n Evolving in C# (http://jeanpaulva.com)

    • Marked as answer by Welshy10 Wednesday, March 7, 2012 6:24 PM
    Friday, March 2, 2012 3:59 PM

All replies

  • Probably your question is not clear.

    What do you really mean by attempts?

    Can you give a sample data for id numbers and attempts?

    Thanks.


    Resolving n Evolving in C# (http://jeanpaulva.com)

    Friday, March 2, 2012 6:46 AM
  • Welshy,

    in Net there are created many ways which makes that you don't have to create the arrays yourself. 

    For SQL Server those are

    DataSets, Linq to SQL and Linq to ET. 

    The arrays of data are with a designer created for you if you use those.

    For other databases you can always use the DataSet in .Net with C#


    Success
    Cor

    Friday, March 2, 2012 7:29 AM
  • at the moment i got a variable called int attempt = 3; that gves all my users just 3 attempts instead i want to give each user 3 attempts each.

    so for example i like to give user 1234 3 attempts, 4456 user 3 attempts ect

    the userid are stored in a database called accounts

    Friday, March 2, 2012 8:02 AM
  • Hello Welshy,

    For your scenario, I prefer you use Dictionary instead of arrays.

    It provides a better and convenient way for storing and manipulating key-value pairs.

    Your id could be the key and attempt could be the value.

    For example:

                Dictionary<int, int> dict = new Dictionary<int, int>();
    
                // Init
                dict.Add(1234, 3);
                dict.Add(4456, 3);
    
                // On attempts, reduce the count based on id
                dict[1234] -= 1;
    
                // Check if dict has a key
                bool exists = dict.ContainsKey(1111);
    

    Anyways.. you can have an array implementation as given below:

                int[,] array = new int[10,1];
                array[0,0] = 1234; 
                array[0,1] = 3;
    
                array[1, 0] = 4456;
                array[1, 1] = 3;


    Resolving n Evolving in C# (http://jeanpaulva.com)

    • Marked as answer by Welshy10 Wednesday, March 7, 2012 6:24 PM
    Friday, March 2, 2012 3:59 PM
  • this is the code i am using to count the attempts and display message box and when 3 attempts are up the idnumber is confiscated

    int attempts =3;
    sqlCommandUpdateConfiscated.Parameters["@cardNumber"].Value = cardNo.Text;
                        sqlCommandUpdateConfiscated.Parameters["@confiscated"].Value = true;
    
                        try
                        {
                            if (!pin.Equals("...") && attempts > 1)
                            {
                                attempts--;
                                MessageBox.Show("Wrong pin number! " + attempts + " attempts left");
                            }
                            else if (!pin.Equals("...") && attempts == 1)
                            {
                                MessageBox.Show("wrong password! your account been blocked");
                                sqlCommandUpdateConfiscated.Connection.Open();
                                int changed = sqlCommandUpdateConfiscated.ExecuteNonQuery();
                                MessageBox.Show("Number of rows changed = " + changed.ToString());
                                sqlCommandCheckLogin.Connection.Close();
                            }

    Friday, March 2, 2012 4:38 PM
  • Blocked for how long time?

    If use reopens the application, what will happen then? Will he start all over again?

    If you really want to stop him, you will have to write these data some place, like into some file on hdd.


    Mitja

    Friday, March 2, 2012 5:43 PM
  • Blocked for how long time?

    If use reopens the application, what will happen then? Will he start all over again?

    If you really want to stop him, you will have to write these data some place, like into some file on hdd.


    Mitja

    Be blocked until i changed true to false and i will be placing code into the program to the userid not to appear til i change from true to false
    Friday, March 2, 2012 6:22 PM
  • And if he closes and start the program again? Then all your variables resets!

    Mitja

    Friday, March 2, 2012 6:25 PM
  • And if he closes and start the program again? Then all your variables resets!

    Mitja

    yes
    Friday, March 2, 2012 6:30 PM
  • So there is no point in making limits or restrictions with number of login tries.

    At least I wouldn`t do it. If restriction isn`t permanent (or until retreiving new password or something) its pointeless.


    Mitja

    Friday, March 2, 2012 6:47 PM
  • Hi...

    I really can't follow you, what do you want to put in an array? the attempts? how have you imagine it?

    but however an array declaration is like that:

    int[] attempts = new int[3]{1,2,3};

    ... however I can't imagine what do you want to do with that array..

    hope it helps  if not reply :)

     


    If some code doesn't work, don't worry help is on the way.. don't forget to mark your thread as solved when done...

    Friday, March 2, 2012 8:21 PM
  • Hi Welshy,

    I am not very clear about your requirements. Could you please let me know if you mean that every user will be blocked after three attempts until you change true to false? Or the users will be blocked until they restart the program?

    For the first scenario, I think the array is not necessary. Since we must store the value of attempts to avoid resetting the value of attempts via restarting the program, I think you can create a table in database to save the attempts information and block information of each user.

    For second scenario, I think you need not to store attempts information to database, because you allow it re-set after restart. You can keep it in your program as a variable.

    If you have any other problems, please feel free to let me know.

    Best Regards,


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, March 6, 2012 7:51 AM