Home c# C # MySQL Read Must Be Called First

C # MySQL Read Must Be Called First

Author

Date

Category

There is a strange problem.
Here is the code:

string [] Ret = New String [3];
STRING SQLTABLENAME = CONFIGDATA (CONFIG) ["SQL"] ["UserTable"];
MySqlConnection Connect = New MySqlConnection (SQLCONNSTR);
Connect.open ();
String SQLCMD = String.Format ("Select * from` {0} `WHERE` ID` = {1} ", sqltablename, ID);
Mysqlcommand command = new mysqlcommand (SQLCMD, CONNECT);
MySqldataReader Reader = Command.ExecuteReader ();
reader.read ();
RET [0] = Reader ["id"]. Tostring ();
RET [1] = Reader ["Name"]. Tostring ();
RET [2] = Reader ["SELECTMEMBER"]. Tostring ();
Reader.Close ();
Connect.Close ();
RETURN RET;

It works fine when launched debugging, but it is worth a collect project (Netcore project), then the error

falls out

16-12-2020 20:37:15: Read Must Be Called First.
16-12-2020 20:37:16: at mysqlconnector.core.ResultSet.GetcurrentRow () in /_/src/mysqlconnector/core/resultset.cs:line 420
  at mysqlconnector.met_item (String Name) in /_/src/mysqlconnector/mysqldatareader.cs:line 196
  AT Palpadile.getUserinfo (String Config, Uint64 ID)

Error appears when you try to execute this code

PassAfile This is the way to a function. I just hid it


Answer 1

An error is that the database on request returned null for no entry was suitable under the condition. Error Read Must Be Called First was because I tried to read Reader in which nothing was.
This can be avoided by adding the check if (reader.read ()) . As a result, the code will look like this

string [] Ret = New String [3];
STRING SQLTABLENAME = CONFIGDATA (CONFIG) ["SQL"] ["UserTable"];
MySqlConnection Connect = New MySqlConnection (SQLCONNSTR);
Connect.open ();
String SQLCMD = String.Format ("Select * from` {0} `WHERE` ID` = {1} ", sqltablename, ID);
Mysqlcommand command = new mysqlcommand (SQLCMD, CONNECT);
MySqldataReader Reader = Command.ExecuteReader ();
if (reader.read ()) {
  // Receive data from Reader
  RET [0] = Reader ["id"]. Tostring ();
  RET [1] = Reader ["Name"]. Tostring ();
  RET [2] = Reader ["SELECTMEMBER"]. Tostring ();
  Reader.Close ();
  Connect.Close ();
  RETURN RET;
}
ELSE {
  // If Reader turned out to be empty
  RETURN NULL;
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions