CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Oct 2015
    Posts
    5

    Question 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); }

  2. #2
    Join Date
    Mar 2001
    Posts
    2,529

    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).
    Last edited by ahoodin; October 1st, 2015 at 07:52 AM.
    ahoodin
    To keep the plot moving, that's why.

  3. #3
    Join Date
    May 2002
    Posts
    511

    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);

  4. #4
    Join Date
    Mar 2001
    Posts
    2,529

    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); 
    }
    Last edited by ahoodin; October 1st, 2015 at 11:38 AM. Reason: fix quote to be code
    ahoodin
    To keep the plot moving, that's why.

  5. #5
    Join Date
    Oct 2015
    Posts
    5

    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)?

  6. #6
    Join Date
    Oct 2015
    Posts
    5

    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?

  7. #7
    Join Date
    May 2002
    Posts
    511

    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.

  8. #8
    Join Date
    Mar 2001
    Posts
    2,529

    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);
    }
    ahoodin
    To keep the plot moving, that's why.

  9. #9
    Join Date
    Oct 2015
    Posts
    5

    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{} )

  10. #10
    Join Date
    Mar 2001
    Posts
    2,529

    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.
    ahoodin
    To keep the plot moving, that's why.

  11. #11
    Join Date
    Mar 2001
    Posts
    2,529

    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.
    ahoodin
    To keep the plot moving, that's why.

  12. #12
    Join Date
    Oct 2015
    Posts
    5

    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

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: need to retrieve data and convert to int for IF statement

    Quote Originally Posted by ahoodin View Post
    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured