none
DataSet.Load closes dataReader RRS feed

  • Question

  •             while (reader.Read())
                {
                    
                    thisphoto = Guid.Empty;
                    photo = "";
                    //OriginalPrice = Convert.ToDouble(reader.GetValue(8));
                    //FinalPrice = Convert.ToDouble(reader.GetValue(9));
                    // ektpwsi = ((OriginalPrice - FinalPrice) / OriginalPrice) * 100;
                    CouponPrice = Double.MinValue;
                    ekptwsiCoupon = "";
                    //Win = 0;
                    CouponCode = 0;
                    WinEws = false;
                    labelEkptwsis = "";
                    couponUntilDate = "";
                    UntilDate = null;
                    DateBig = "";
                    ExtraMovText = "";
                    int availableCoupons = 0;
                    int purchases = 0;
    
                    if (reader.GetValue(16) != DBNull.Value)
                        availableCoupons = Convert.ToInt32(reader.GetValue(16));
    
                    if (reader.GetValue(23) != DBNull.Value)
                        purchases = Convert.ToInt32(reader.GetValue(23));
    
                    if (availableCoupons == 0 || availableCoupons - purchases > 0)
                    {
                        if (reader.GetValue(15) != DBNull.Value)
                            CouponPrice = Convert.ToDouble(reader.GetValue(15));
    
                        if (reader.GetValue(25) != DBNull.Value)
                            internetDiscount = Convert.ToDouble(reader.GetValue(25));
    
                        //MSEN 21/5/2012
                        //For development/debugging purpuses, I must render special offers that cost only 1Euro. In this way we dont spend 30Euros
                        //everytime I perform tests in the order procedure. In the if clause below I check:
                        // If the offer is a regular one and if so I render it
                        //If the offer is a special one AND if the client is one of the developers and if so I render it
                        if ((CouponPrice - internetDiscount > 1)
                            || ((CouponPrice - internetDiscount == 1) && (HttpContext.Current.Request.UserHostAddress.StartsWith(ConfigurationManager.AppSettings["testIP1"])
                            ||  HttpContext.Current.Request.UserHostAddress.StartsWith(ConfigurationManager.AppSettings["testIP"]))))
                        {
                            //MSEN 27/4/2012 add subtitle
                            subTitle = "";
    
                            if (reader.GetValue(22) != DBNull.Value)
                                subTitle = reader.GetValue(22).ToString();
    
                            if (reader.GetValue(4) != DBNull.Value)
                            {
                                thisphoto = new Guid(reader.GetValue(4).ToString());
                                photo = ThumbMaker.GetThumb(GetFilePath(thisphoto), 910, 270, true, false);
                            }
    
                            if (reader.GetValue(19) != DBNull.Value)
                                CouponCode = Convert.ToInt32(reader.GetValue(19));
    
                            if (reader.GetValue(17) != DBNull.Value)
                            {
                                labelEkptwsis = "ΕΚΠΤΩΣΗ";
                                ekptwsiCoupon = reader.GetValue(17).ToString() + "%";
                            }
                            else if (reader.GetValue(20) != DBNull.Value)
                            {
                                labelEkptwsis = "ΚΕΡΔΙΖΕΤΕ";
    
                                string thisekptwsi = Convert.ToDouble(reader.GetValue(20).ToString()).ToString();
                                ekptwsiCoupon = Decimal.Parse(thisekptwsi, System.Globalization.NumberStyles.AllowDecimalPoint, CultureInfo.GetCultureInfo("el-GR")).ToString("C", CultureInfo.CreateSpecificCulture("el-GR"));
                                ekptwsiCoupon = "€" + ekptwsiCoupon.Replace(",00", "").Replace(" €", "");
    
                                if (reader.GetValue(18) != DBNull.Value)
                                {
                                    WinEws = Convert.ToBoolean(reader.GetValue(18));
                                    if (WinEws)
                                        labelEkptwsis += " ΕΩΣ";
                                }
                            }
    
                            if (reader.GetValue(7) != DBNull.Value)
                            {
                                date1 = Convert.ToDateTime(reader.GetValue(7));
    
                                DateBig = date1.ToString("D", CultureInfo.CreateSpecificCulture(CurrentCulture.CultureCode));
                                UntilDate = DateBig.Split(',');
                                couponUntilDate = UntilDate[1];
                                //MSEN
                                //tsExpiration = calculateExpirationHours(date1);
    
    
                            }
                            if (reader.GetValue(21) != DBNull.Value)
                            {
                                ExtraMovText = GetBodyContentWithImages(reader.GetValue(21).ToString());
                            }
    
                            title = reader.GetValue(2).ToString();
                            smalldescription = GetBodyContentWithImages(reader.GetValue(3).ToString());
                            string[] smalldescription_home = null;
                            string smallArticledescription = "";
                            if (smalldescription.Contains("###"))
                            {
                                smalldescription_home = smalldescription.Split(new string[] { "###" }, StringSplitOptions.None);
                                smallArticledescription = smalldescription_home[0];
                            }
                            else
                            {
                                smallArticledescription = smalldescription;
                            }
    
                            ContentId = new Guid(reader.GetValue(0).ToString());
    
                            string expirationTextForCountDown = string.Format("until: {0},format:'HMS'", date1);
                            //MSEN 27/4/2012 add title, subTitle, ekptwsi, contentID
                            sb.AppendFormat(template, smallArticledescription, photo, ContentId, ConfigurationManager.AppSettings["SiteDomain"],
                                                lat, lon, CouponPrice, labelEkptwsis, ekptwsiCoupon, couponUntilDate, CouponCode, ExtraMovText,
                                                    title, subTitle, ("-" + ekptwsiCoupon), Convert.ToDateTime(reader.GetValue(7)).Year.ToString(),
                              (Convert.ToDateTime(reader.GetValue(7)).Month - 1), Convert.ToDateTime(reader.GetValue(7)).Day.ToString(), counter,
                               Convert.ToDateTime(reader.GetValue(7)).Hour.ToString(), Convert.ToDateTime(reader.GetValue(7)).Minute.ToString(),
                               Convert.ToDateTime(reader.GetValue(7)).Millisecond.ToString());
                            
                        }
    
                    }
                    counter++;
                    ds.Load(reader, LoadOption.Upsert, dt);
                }
    When the data set Load method runs , in the second loop I get "Invalid attmpt to call Read when reader is closed. If I dont include
    ds.Load(reader, LoadOption.Upsert, dt); everything works fine
    
    
    Wednesday, June 13, 2012 8:31 AM

Answers

  • Yes it does. It closes the reader when operation completes. I didn't found any documentation about this behavior, but by looking the source code of the 4th overload of Load method that is used by operation, the provided reader is closed at the end if reader.IsClosed is false and NextResults returns false. 
    Wednesday, June 13, 2012 7:09 PM

All replies

  • Yes it does. It closes the reader when operation completes. I didn't found any documentation about this behavior, but by looking the source code of the 4th overload of Load method that is used by operation, the provided reader is closed at the end if reader.IsClosed is false and NextResults returns false. 
    Wednesday, June 13, 2012 7:09 PM
  • Hi how_you_doing,

    I would like to agree with MasaSam's view, the DataSet.Load() method would close the reader after doing something inside and you could prove this by Reflector tool.

    Best Regards,


    Tony Xiao [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, June 14, 2012 6:50 AM
    Moderator
  • Correct me if I am wrong:

    1. While reader.Read and I step into the first record of my result set.

    2.dataset.Load the reader without changing the datasets.Tables[0] records

    3. reader.Read the second record of my result set

    4. dataset.Load the reader without changing the datasets.Tables[0] records

    But if the reader is closed it cant read so an exception is thrown. So what is the point of closing the reader in the load method of the dataset???

    Thursday, June 14, 2012 1:08 PM
  • Possibly releasing the expensive resources because otherwise DataSet, which usually has longer lifecycle than IDataReader, might hold on the resources that has short lifecycle or at least should be. Really don't know why there is not option to tell DataSet that reader should be stay open after the Load, that is something only people from Microsoft can answer.
    Thursday, June 14, 2012 4:58 PM
  • Yes it does. It closes the reader when operation completes. I didn't found any documentation about this behavior, but by looking the source code of the 4th overload of Load method that is used by operation, the provided reader is closed at the end if reader.IsClosed is false and NextResults returns false. 
    How did you view the source code of the fourth overload?
    Friday, June 15, 2012 10:49 AM
  • Please try to use Reflector tool to get the source code for this method.

    Best Regards,


    Please mark this as answer if it helps with this issue!

    Friday, June 15, 2012 1:53 PM
  • Yes it does. It closes the reader when operation completes. I didn't found any documentation about this behavior, but by looking the source code of the 4th overload of Load method that is used by operation, the provided reader is closed at the end if reader.IsClosed is false and NextResults returns false. 

    How did you view the source code of the fourth overload?

    As adviced by Kuacci, I used Reflector.NET pro version. Same is possible if you get Microsoft debug symbols.
    Friday, June 15, 2012 5:18 PM
  • I ran into this issue, too, and had to refactor my code to not use the load method.

    In my opinion this is a bug.

    If the DataTable didn't open it, it shouldn't close it.

    At the very least they could provide us a parameter on the method to not close the reader.

    Wednesday, October 21, 2015 5:33 PM