+ 1

Stuck on storing individual rows from the sqlDataReader for later use

I am using visual studio, winforms and the integrated SQL server, The win form is simple it contains 1 x textbox & 3 x buttons i have two sql tables Questions & Answers i would like for a question from the question table to be loaded into the textbox, and i want the 3 answers from the answers table to be loaded into the button text i have been able to get this working using the sqlDataReader and using while Datareader.read() { txtBox.text = DataReader.getValue() etc however i have been unable to stop the dataReader from reading up until the last result, i call dataReader in a form onload() event so i need a way of saving the results stream from the dataReader to a object or list that i can access to make a simple question and answer form, also in the form there is no right/wrong answers, simply choosing an option should display the next row from the sql tables mentioned above, any help or advice is greatly appreciated, sorry if this does not make sense.

6th Dec 2019, 2:31 AM
Gary
Gary - avatar
2 Antworten
+ 1
Oh damm, thank you so much i just missed the deadline but i couldnt find this anywhere and i crawled through stack overflow on my knees
6th Dec 2019, 9:29 PM
Gary
Gary - avatar
0
https://stackoverflow.com/questions/4018114/read-data-from-sqldatareader using(SqlDataReader rdr = cmd.ExecuteReader()) { while (rdr.Read()) { var myString = rdr.GetString(0); //The 0 stands for "the 0'th column", so the first column of the result. // Do somthing with this rows string, for example to put them in to a list listDeclaredElsewhere.Add(myString); } } The read method returns true if the are more rows and false if all rows have been read. Therefore it can be use in a with a while-loop Columns can be accessed rdr.GetString(0); // to get first column rdr["column-name"]; // to get data of column using columnname Define 3 text variable outside the read-loop to assign the data to. or use a list Use these variables to assign the text to the buttons. Hope this helps
6th Dec 2019, 9:15 PM
sneeze
sneeze - avatar