0

In order to read data from a Sql server database, I've been told that I must put the readers inside of using statements. There is another question Read data from SqlDataReader similar to this, but it didn't help me much, because I already knew how to read the data. I just didn't know how to put the data in the function. The problem is that I need to use each piece of data that is inside the two different using statements, and I need to put them into the same function SendPushNotification(). Every time one reader is done being used, though, I lose the data. One using statement adds the data to

string readerTest;

And one using statement adds the data to

string uriReadString;

I then proceed to pass both as arguments into my function:

SendPushNotification(uriReadString, readerTest);

By the time they are both able to be put into the function, the readers have ended, and my strings go back to being empty. Plz help.

using (var connection = new SqlConnection(connectionString))
{
    connection.Open();

    DateTime now = DateTime.Now;
    now = now.AddMilliseconds(-now.Millisecond);

    var command = new SqlCommand("SELECT ImageName FROM Images WHERE NotifyDate = @todayDate", connection);
    var paramDate = new SqlParameter("@todayDate", now);
    command.Parameters.Add(paramDate);

    var commandUri = new SqlCommand("SELECT * FROM Uri", connection);
    string readerTest = "";
    string uriReadString = "";
    using (SqlDataReader reader = command.ExecuteReader())
    {
        while (reader.Read())
        {

            readerTest = reader[0].ToString();
            System.IO.File.WriteAllText(@"C:\Users\Nathan\Documents\Visual Studio 2013\Projects\MVCImageUpload\uploads\GetIDNow.txt", readerTest);

        }
    }

    using (SqlDataReader readerUri = commandUri.ExecuteReader())
    {
        while (readerUri.Read())
        {
            uriReadString = readerUri[0].ToString();
            System.IO.File.WriteAllText(@"C:\Users\Nathan\Documents\Visual Studio 2013\Projects\MVCImageUpload\uploads\GetUriNow.txt", uriReadString);

        }

    }
    SendPushNotification(uriReadString, readerTest);

}
Community
  • 1
  • 1
Vigs
  • 331
  • 2
  • 3
  • 14
  • Add them to two different lists respecfully then `SendPushNotification` the values from the two lists. – Adam Jul 28 '15 at 01:55
  • possible duplicate of [Read data from SqlDataReader](http://stackoverflow.com/questions/4018114/read-data-from-sqldatareader) – Alex Netkachov Jul 28 '15 at 01:57
  • Are you sure that the values of uriReadString readTest are not getting set to empty string at the end of the while loop? Since you keep resetting be value of the variables in loop – KnightFox Jul 28 '15 at 02:08

1 Answers1

0

I believe this is what you're attempting to do... If it is not, I'll delete the answer. Add the string manually to a list and then in a parallel fashion, PushSend the notification respectfully.

List<string> readList = new List<string>();
List<string> uriList = new List<string>();
using (var connection = new SqlConnection(connectionString))
{
    connection.Open();

    DateTime now = DateTime.Now;
    now = now.AddMilliseconds(-now.Millisecond);

    var command = new SqlCommand("SELECT ImageName FROM Images WHERE NotifyDate = @todayDate", connection);
    var paramDate = new SqlParameter("@todayDate", now);
    command.Parameters.Add(paramDate);

    string readerTest = "";
    string uriReadString = "";
    using (SqlDataReader reader = command.ExecuteReader())
    {
        while (reader.Read())
        {

            readerTest = reader[0].ToString();
            System.IO.File.WriteAllText(@"C:\Users\Nathan\Documents\Visual Studio 2013\Projects\MVCImageUpload\uploads\GetIDNow.txt", readerTest);
            readList.Add(readerTest);
        }
    }

    using (SqlDataReader readerUri = commandUri.ExecuteReader())
    {
        while (readerUri.Read())
        {
            uriReadString = readerUri[0].ToString();
            System.IO.File.WriteAllText(@"C:\Users\Nathan\Documents\Visual Studio 2013\Projects\MVCImageUpload\uploads\GetUriNow.txt", uriReadString);
            uriList.Add(uriReadString);
        }

    }

    for(int i = 0; i < readList.Count; i++)
        SendPushNotification(uriList[i], readList[i]);
hazzik
  • 13,019
  • 9
  • 47
  • 86
Adam
  • 2,422
  • 18
  • 29