-1

I have a problem with my SQL datareader, i want to make an external class and make the code in my xaml.cs as short as possible, because there are a lot of sqldatareaders needed in my program. for this I want to pass following two strings to the datareader class:

public void refreshcombobox()
{
   cbGebruiker.Items.Clear();
   database = new DataBase();
   string sqlrdr = "(rdr.GetString(1).ToString().Trim())";
   List<string> reader = database.ReaderRdr("Select * from Gebruikers", ref sqlrdr);
      foreach (String str in reader)
        {
            cbGebruiker.Items.Add(str);
        }
 }

however, when I do this this is the result in my program instead of the actual results that are stored in the database:

http://i58.tinypic.com/301j2vo.jpg (I can't post images)

can somebody help me with this? I've searched everywhere... I don't know how to pass the rdr.GetString(1).ToString().Trim() to make it actually look stuff up in the db. Instead of just copying the string directly into the list.

This is the class:

namespace ClassLib
  {
 public class DataBase
  {
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["kassadatabase"].ConnectionString);
    public object ScalarObject(string sql)
    {
        object value = null;
        try
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            value = cmd.ExecuteScalar();

        }
        catch (SqlException ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            if (conn != null) conn.Close();
        }
        return value;
    }
    public List<string> ReaderRdr(string sql)
    {
            SqlDataReader rdr = null;
            List<string> reader = new List<string>();
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn); 
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    //reader.Add(rdr.GetString(1).ToString().Trim());
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (rdr != null) rdr.Close();
                if (conn != null) conn.Close();
            }
            return reader;
    }
    //public List<string> ReaderRdr(string sql, ref string str)
    //{
    //    SqlDataReader rdr = null;
    //    List<string> reader = new List<string>();
    //    try
    //    {
    //        conn.Open();
    //        SqlCommand cmd = new SqlCommand(sql, conn);
    //        rdr = cmd.ExecuteReader();
    //        while (rdr.Read())
    //        {
    //            //MessageBox.Show(str.ToString());
    //            //var strRdr = str;
    //            //MessageBox.Show(strRdr.ToString());
    //            //reader.Add(rdr.GetString(1).ToString().Trim());
    //            reader.Add(str);
    //            Console.WriteLine(String.Format("{0}", rdr[0]));
    //        }
    //    }
    //    catch (SqlException ex)
    //    {
    //        MessageBox.Show(ex.Message);
    //    }
    //    finally
    //    {
    //        if (rdr != null) rdr.Close();
    //        if (conn != null) conn.Close();
    //    }
    //    return reader;
    //}
    public void ExecuteNQuery(string insertString)
    {
        try
        {
            conn.Open();
            SqlCommand cmd2 = new SqlCommand(insertString, conn);
            cmd2.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            if (conn != null) conn.Close();
        }
    }
}

}

public List<string> ReaderRdr(string sql)
{
        SqlDataReader rdr = null;
        List<string> reader = new List<string>();
        try
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn); 
            rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                //reader.Add(rdr.GetString(1).ToString().Trim());
            }

.....

now in the methode public list i want to replace the //reader.Add(rdr.GetString(1).ToString().Trim()); part(wich works fine)

with a string that is passed to the method.

public List<string> ReaderRdr(string sql, string strRdr)
{
        SqlDataReader rdr = null;
        List<string> reader = new List<string>();
        try
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn); 
            rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                //reader.Add(strRdr);
            }
maxim
  • 1
  • 2
  • You actually adding the String that you passed to a function to your reader `reader.Add(str);` You get the response from SQL I your `rdr`. – Andrew Nov 01 '14 at 07:41
  • yes i know, but i don't know how to do it correctly. – maxim Nov 01 '14 at 07:43

2 Answers2

1

I'm not 100% sure what you're trying to do, but I can tell you right now that this is what you're doing wrong - the string (str) you're passing to ReaderRdr is just a string literal of C# code. There's super hacky (super inadvisable) things you can do to mimic what exists in other languages as eval(), but there's no built-in way to do that in C#. Nothing (sensible) you do to "(rdr.GetString(1).ToString().Trim())" is ever going to get a string, or cast it to string, or trim anything.

Within your ReaderRdr function, all you're accomplishing is just to add the string str to your List<string> reader. This accomplishes nothing and has no bearing whatsoever on the results you get from your database query in your SqlDataReader rdr. If you want to store the data you actually get from your database, use rdr, not the (useless) string argument str.

Also, I feel like you must have left something out of your code - you're instantiating your SqlCommand cmd with conn as your second argument, but I don't see that defined anywhere within your ReaderRdr method, and it's not an argument passed to ReaderRdr. You don't have an SqlConnection object as a field or property within your class, do you?

As far as what you should maybe do, despite lacking much of any context in terms of your actual aims - if you want to get any given column of the result for each row returned by your SqlDataReader:

rdr = cmd.ExecuteReader();
while (rdr.Read())
{
     var yourDataCell = rdr[yourColumnIndex];
     // or:
     var yourDataCellOtherWay = rdr["YourColumnName"];
}

Alternately, you can just iterate through each of the cells in any given row produced by your SqlDataReader like so:

 for(int i = 0 ; i < numberOfColumns; i++) {
    // do something with rdr[i] here
}

I'm not sure if there's anything you can do establish numberOfColumns based on the state of your SqlDataReader, but others might know better.

furkle
  • 5,019
  • 1
  • 15
  • 24
  • I am trying to make a class that i can use for every sqldatareader in my program(i need to use this code like 20 times) all the code has to do is get lists of data from my database and pass it to comboboxes and listboxes. – maxim Nov 01 '14 at 07:48
  • @maxim I've included the proper syntax to get any given cell within the rows returned by your SqlDataReader. – furkle Nov 01 '14 at 07:53
0

You actually adding the String that you passed to a function to your reader reader.Add(str); You get the response from SQL I your rdr.

This item will show you something from your database:

Console.WriteLine(String.Format("{0}", rdr[0]));

Andrew
  • 7,619
  • 13
  • 63
  • 117