Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Hangman

Empfohlene Antworten

Veröffentlicht

Hallo FI-Users

Ich habe ein Problem.

Ich wollte ein Hangman Programmiere mit C# in ASP.NET

Dann habe ich eine Lokale DB und ein Server, auf der DB sind die Wörter.

Nun habe ich einen Service erstellt, welchen diese Daten "abholt".

Jedoch bekomme ich immer ein Fehler:

No current query in data reader

Methode im Service


[WebMethod]

        public String[] getRandomWord()

        {

            String currentWord = "";


            MySqlConnection connection = new MySqlConnection(myConnectionString);

            MySqlCommand command = connection.CreateCommand();

            command.CommandText = "SELECT COUNT(*) AS COUNT FROM hangman_woerter";

            MySqlDataReader Reader;

            connection.Open();

            Reader = command.ExecuteReader();


            if (Reader.HasRows)

            {

                Reader.Read();

                maxValue = Reader.GetInt32(Reader.GetOrdinal("COUNT"));


            }

            else

            {

                maxValue = 10;

            }

            //Random zufall = new Random();

            //int randomID = zufall.Next(Reader);

            connection.Close();

            connection.Open();


            command.CommandText = "SELECT Wort FROM hangman_woerter";

            Reader = command.ExecuteReader();


            Random zufall = new Random();

            int randomID = zufall.Next(maxValue);


            if (Reader.HasRows)

            {


                for (int i = 0; i < randomID; i++)

                {

                    Reader.NextResult();

                }

                Reader.Read();

                [COLOR="Red"]currentWord = Reader.GetString("Wort");[/COLOR]


            }




            String[] wortArray = new String[currentWord.Length];


            //wortArray = currentWord.Split();

            for (int i = 0; i < currentWord.Length; i++)

            {

                wortArray[i] = currentWord.Substring(i, 1);

            }

            connection.Close();

            return wortArray;


        }

Was machh ich falsch?

Danke für Antworten

Lg JL

Hi,

vielleicht solltest Du anders vorgehen:

1. Hole alle Wörter aus der DB



Reader = command.ExecuteReader();


List<string> alleWoerter = new List<string>;


while (reader.Read())

{

     alleWoerter.Add(Reader.GetString("Wort"));

}                         

2. Dann Zufallszahl bestimmen...und aus Wort aus der Liste holen

int max = alleWoerter.Count;


Random zufall = new Random();

int randomID = zufall.Next(max);


string wort = alleWoerter[randomID];

Die Verarbeitung ist einfacher und nicht so Fehleranfällig...

Gruß Ron

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.