-4

I wanna execute a Query for 10 times to get random values, but my code never stops,
it keeps looping and gives no response.

This is my code

int j = 0;
do {
    DataSet AVATARLINE = new DataSet();
    SqlDataAdapter AVATARLINE_1 = new SqlDataAdapter("DECLARE
            @Random INT,@Upper INT,
            @Lower INT SET @Lower = (select MIN(ID)from MastryID)SET
            @Upper = (select MAX(ID)from MastryID)+1 SELECT
            @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)SELECT @Random", connection);
    AVATARLINE_1.Fill(AVATARLINE);
    foreach (DataRow row in AVATARLINE.Tables[0].Rows)
    {
        for (int i = 0; i < AVATARLINE.Tables[0].Columns.Count; i++)
        richTextBox1.Text += row[i].ToString() + Environment.NewLine;
    }
}
while (j < 10); // 0-9

Any idea how to make it work for ONLY 10 times.

Uttara
  • 2,496
  • 3
  • 24
  • 35
user2010164
  • 47
  • 1
  • 5
  • 2
    what's not working here ? – Tigran Jan 25 '13 at 07:55
  • So what *doesn't* work? Please describe your entire problem and question. – lc. Jan 25 '13 at 07:56
  • 3
    Do you really need to use a query? Why not use plain random from c#? – GeorgeVremescu Jan 25 '13 at 07:56
  • 9
    you dont seem to incrementing `j` in your loop – Raab Jan 25 '13 at 07:57
  • this is a poorly written question. please read the FAQ. – Sam Axe Jan 25 '13 at 07:57
  • 3
    Using SQL for random values, smells like a huge overkill – RvdK Jan 25 '13 at 07:58
  • this hurts my eyes to read.. overkill to say the least should learn to use some built in functionality that .NET offers.. – MethodMan Jan 25 '13 at 08:00
  • 1
    Do not do downvote further. The question is clear now, and the code exhibits the problem OP described.. @RabNawaz pls make it an answer! :) – nawfal Jan 25 '13 at 08:01
  • i need it via sql for another propose – user2010164 Jan 25 '13 at 08:01
  • Use `command.ExecuteScalar` to retrieve your single `int` value instead of using loops and a `DataSet`. – Tim Schmelter Jan 25 '13 at 08:12
  • @DJKRAZE still i can raise my concern, thats not playing moderator. This question in the current format is so fit for SO. May be its too obvious what OP is missing, but that shouldnt be a reason in my opinion to downvote. Just not being harsh on someone new to programming, we all started from there.. That said, this can be closed as its not being helpful for future visitors.. – nawfal Jan 25 '13 at 08:14

2 Answers2

4

Here you go :)... you dont seem incrementing j in your loop

int j = 0;
    do {
            DataSet AVATARLINE = new DataSet();
            SqlDataAdapter AVATARLINE_1 = new SqlDataAdapter("DECLARE @Random INT,@Upper INT,@Lower INT SET @Lower = (select MIN(ID)from MastryID)SET @Upper = (select MAX(ID)from MastryID)+1 SELECT @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)SELECT @Random", connection);
            AVATARLINE_1.Fill(AVATARLINE);
            foreach (DataRow row in AVATARLINE.Tables[0].Rows)
            {
                for (int i = 0; i < AVATARLINE.Tables[0].Columns.Count; i++)
                    richTextBox1.Text += row[i].ToString() + Environment.NewLine;
            } 
        j++; 
        }
    while (j < 10); // 0-9
Raab
  • 34,778
  • 4
  • 50
  • 65
0

Add ++:

int j = 0;
do {
        DataSet AVATARLINE = new DataSet();
        SqlDataAdapter AVATARLINE_1 = new SqlDataAdapter("DECLARE @Random INT,@Upper INT,@Lower INT SET @Lower = (select MIN(ID)from MastryID)SET @Upper = (select MAX(ID)from MastryID)+1 SELECT @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)SELECT @Random", connection);
        AVATARLINE_1.Fill(AVATARLINE);
        foreach (DataRow row in AVATARLINE.Tables[0].Rows)
        {
            for (int i = 0; i < AVATARLINE.Tables[0].Columns.Count; i++)
                richTextBox1.Text += row[i].ToString() + Environment.NewLine;
        }  
    }
while (++j < 10); // 0-9

Any other method of incrementing j will do. A more conventional approach would be a for loop:

for (int j = 0; j < 10; j++)
{
    DataSet AVATARLINE = new DataSet();
    SqlDataAdapter AVATARLINE_1 = new SqlDataAdapter("DECLARE @Random INT,@Upper INT,@Lower INT SET @Lower = (select MIN(ID)from MastryID)SET @Upper = (select MAX(ID)from MastryID)+1 SELECT @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)SELECT @Random", connection);
    AVATARLINE_1.Fill(AVATARLINE);
    foreach (DataRow row in AVATARLINE.Tables[0].Rows)
    {
        for (int i = 0; i < AVATARLINE.Tables[0].Columns.Count; i++)
            richTextBox1.Text += row[i].ToString() + Environment.NewLine;
    }  
}

Finally, you might want to read about the Random class: http://msdn.microsoft.com/en-us/library/system.random.aspx

phoog
  • 42,068
  • 6
  • 79
  • 117