need to retrieve data and convert to int for IF statement
Okay so, i'm currently trying to read some data from my access database file, to convert it to an int and then use it for an IF statement, but right now if i try to load it i get a "no data in row/column" error, and im not quite sure why.
Code:
int Niv = 0;
try
{
conn.Open();
OleDbDataReader reader = null;
OleDbCommand cmd = new OleDbCommand("Select Niveau From Tabel1 where ID=" + txt_userID.Text + "", conn);
reader = cmd.ExecuteReader();
Niv = Convert.ToInt32(reader["Niveau"].ToString());
conn.Close();
MessageBox.Show(Niv.ToString());
}
catch(Exception ex)
{ MessageBox.Show(ex.Message); }
Re: need to retrieve data and convert to int for IF statement
The reader returns a boolean value that says whether any data was returned by your query.
If it returns false, no need to try to use the query results.
Most likely your query needs work (or your database is missing a row).
Re: need to retrieve data and convert to int for IF statement
You can check for a NULL value too.
string field = reader.IsDBNull(0) ? string.Empty : reader.GetString(0);
Re: need to retrieve data and convert to int for IF statement
So to collaborate the two replies:
Code:
if (reader.Read())
{
field = reader.IsDBNull(0) ? string.Empty : reader.GetString(0);
}
Re: need to retrieve data and convert to int for IF statement
since i posted the question i've changed the code a bit, and now i'm not getting the "no data" error, and after reading the other posts i've come to know why i'm getting the responses i am, so heres the current code:
Code:
int Niv = 0;
try
{
conn.Open();
OleDbDataReader reader = null;
OleDbCommand cmd = new OleDbCommand("Select Niveau From Tabel1 where ID=" + txt_userID.Text +"", conn);
reader = cmd.ExecuteReader();
Niv = Convert.ToInt32(reader.Read());
MessageBox.Show(Convert.ToString(reader.Read()));
reader.Close();
conn.Close();
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
finally { conn.Close(); }
what i'm currently getting is 1 and 0, which is the cause of the reader.read getting converted to an int.
so for 1. ashoodin, where would i input that if statement? i tried putting it in and fiddling around a bit but i couldn't get it to work (also field wasn't a variable which VS says it should be so i made it a string)
and 2. how would i get the actual information out and converted to an int (or just use it as it should already be an int)?
Re: need to retrieve data and convert to int for IF statement
disregard question one in the last post there, i missed Tron's message, and i should probably rephrase question 2.
so if reader.read() just returns wether the query is usable or not, how would i take the information from the query and convert it to an int?
Re: need to retrieve data and convert to int for IF statement
SqlDataReader has lots of methods. Change GetString() to the method providing the data type you want.
Re: need to retrieve data and convert to int for IF statement
If you want to use the code we gave you however:
Code:
if (!String.IsNullOrEmpty(field))
{
Niv = Convert.ToInt32(field);
}
Re: need to retrieve data and convert to int for IF statement
I'm not quite sure where to place this code in relation to everything else, currently i've tried setting it up like this
Code:
conn.Open();
OleDbDataReader reader = null;
OleDbCommand cmd = new OleDbCommand("Select Niveau From Tabel1 where ID=" + txt_userID.Text +"", conn);
reader = cmd.ExecuteReader();
if (reader.Read())
{
string field = reader.IsDBNull(0) ? string.Empty : reader.GetString(0);
if (!String.IsNullOrEmpty(field))
{ Niv = Convert.ToInt32(field); } }
MessageBox.Show(Convert.ToString(Niv));
reader.Close();
conn.Close();
(Thats inside the try{} )
Re: need to retrieve data and convert to int for IF statement
Only thing you could make sure you are using "using" in your code
in order to clean up the resources from the OleDbConnection.
Here is a quote from Microsoft.com
Code:
public void InsertRow(string connectionString, string insertSQL)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
// The insertSQL string contains a SQL statement that
// inserts a new row in the source table.
OleDbCommand command = new OleDbCommand(insertSQL);
// Set the Connection to the new OleDbConnection.
command.Connection = connection;
// Open the connection and execute the insert command.
try
{
connection.Open();
command.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
// The connection is automatically closed when the
// code exits the using block.
}
}
https://msdn.microsoft.com/en-us/lib...vs.110%29.aspx
Don't forget to rate us if we helped. We do this for free.
Re: need to retrieve data and convert to int for IF statement
Ah I see your using a try-catch block. No need to use using then I believe.
Re: need to retrieve data and convert to int for IF statement
so i solved my problem, though the solution doesn't exactly include much of the help you've given me i still think a big thanks is in place, as you at least helped me realize what exactly was wrong with my code in the first place, so thank you :)
Re: need to retrieve data and convert to int for IF statement
Quote:
Originally Posted by
ahoodin
Ah I see your using a try-catch block. No need to use using then I believe.
I would still recommend using a using block over explicitly trying to close connections/free resources in a try/catch/finally block.
P.S. OleDbCommand implements IDisposable so it too would benefit from being inside a using block.