没有数据存在oledbdatareader行列no data exists for the row column in oledbdatareader

- 此内容更新于:2015-01-06
主题:

原文:

Im try to load excel data to list here im using oledbdatareader to load the data here is my code

          [HttpPost]
    public ActionResult GetExcelData()
    {
        List<ExcelData> exData = new List<ExcelData>();

         string status;
        string fileparth;
        string json;
        using (var reader = new StreamReader(Request.InputStream))
        {
            json = reader.ReadToEnd();
        }
        JObject jo = (JObject)JsonConvert.DeserializeObject(json);
        fileparth = jo.Value<string>("uplaodFile");
        string conString = string.Empty;

        string extension = Path.GetExtension(fileparth);

        switch (extension)
        {
            case ".xls": //Excel 97-03
                conString = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES'", fileparth);

                break;
            case ".xlsx": //Excel 07 or higher
                conString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR=YES'", fileparth);

                break;

        }


        using (OleDbCommand cmd = new OleDbCommand())
            {
                excel_con.Open();
                string sheet1 = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["TABLE_NAME"].ToString();
                cmd.CommandText = "SELECT * FROM [" + sheet1 + "]";
                cmd.Connection = excel_con;
                using (OleDbDataReader reader = cmd.ExecuteReader())
                {
                    int nFields = reader.FieldCount;
                    ExcelData curLine = new ExcelData();
                    curLine.Actualkm = Convert.ToDecimal(reader[1]);
                    curLine.TRRequestID = reader[0].ToString();

                    curLine.Amount = Convert.ToDecimal(reader[2]);
                    exData.Add(curLine);
                }
                excel_con.Close();
                return Json(new { result = "success", data = exData });
          //  excel_con.Close();
        }
    }

i get error in curLine.Actualkm = Convert.ToDecimal(reader1);

here is my getters and setters

      public class ExcelData
    {
        private string _TRRequestID;

        public string TRRequestID
        {
            get { return _TRRequestID; }
            set { _TRRequestID = value; }
        }
        private decimal _Actualkm;

        public decimal Actualkm
        {
            get { return _Actualkm; }
            set { _Actualkm = value; }
        }
        private decimal _Amount;

        public decimal Amount
        {
            get { return _Amount; }
            set { _Amount = value; }
        }
    }

here is my excel file

enter image description here

please help me thank you

Ehsan Sajjad的回复:你们# 39;错误?

(原文:what's the error?)

mayowa ogundele的回复:你调试,什么时候?

(原文:Did you debug, and at what point?)

ISURU的回复:在这行curLine。读者Actualkm = Convert.ToDecimal([1]);

(原文:in this line curLine.Actualkm = Convert.ToDecimal(reader[1]);)

ISURU的回复:InvalidOperationExecption是execption

(原文:InvalidOperationExecption is the execption)

Jason Faulkner的回复:把线的断点,看看读者[1]。我# 39;m猜测这不是一个数字或空间。你可能想尝试十进制。解析相反(后来的)。

(原文:Put a breakpoint at the line and the see what reader[1] is at that point. I'm guessing it is not a number or has spaces. You may want to try Decimal.Parse instead (in the case of the later).)

解决方案:
试图读取的代码行11。 我认为你不需要这样使用空行 如果(! string.IsNullOrEmpty(读者[1].ToString()){ } 在你使用代码。
原文:

The code is trying to read the row 11.

I suppose you don't need the empty rows so use this

if(!string.IsNullOrEmpty(reader[1].ToString()) {}

in your using code.