Click here to Skip to main content
14,456,229 members
Rate this:
Please Sign up or sign in to vote.
See more:
I need an solution for this error"system.argumentnullexception: 'value cannot be null. Parameter name: path'" and can any 1 share me code for getting excel file in listview by using oledb connection

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp2
{
class payment
{



public string ACHTransactionCodeM { get; set; }
public string Control9{ get; set; }


public string DestinationAccountType2 { get; set; }
public string LedgerFolioNumber3 { get; set; }
public string Control15 { get; set; }
public string BeneficiaryAccountHoldersName40 { get; set; }
public string Control7 { get; set; }

}
}

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Shapes;
using Microsoft.Win32;
using WpfApp2;

namespace WpfApp2
{

    public partial class MainWindow : Window
    {
        private string filePath;

        public MainWindow()
        {
            InitializeComponent();

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            // ofd.DefaultExt = "Image files(*.png; *.jpeg)| *.png; *.jpeg | All files(*.*) | *.* ;";
            ofd.Filter = @"All files (*.*)|*.*"; ;
            ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string connetion = string.Format(@"provider= Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\tharun.reddy\Documents\cms1.xlsx ; Extended Properties= 'Excel 12.0 Xml;HDR=YES;IMEX=1;'");
            
            OleDbConnection conector = new OleDbConnection(connetion);
            conector = new OleDbConnection(connetion);
            conector.Open();

            OleDbCommand consulta = default(OleDbCommand);
            consulta = new OleDbCommand("select * from [cms1$]", conector);

            OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter();
            adapter.SelectCommand = consulta;
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            conector.Close();
            if (ofd.ShowDialog() == true)
            {
                string filename = ofd.FileName;
                textbox.Text = filename;
                //txtEditor.Text = File.ReadAllText(filename);
            }
                List<payment> payments = DataTableReader(textbox.Text);
                //lvPayments.ItemsSource = File.ReadAllText(filename);
                lvPayments.ItemsSource = payments ;

            }

        private List<payment> DataTableReader(string filepath)
        {

            List<payment> lstpayment = new List<payment>();
               

            using (StreamReader sr = File.OpenText(filePath))
                
            {
                string strPay = string.Empty;

                string strACHTransactionCodeM = (new FileInfo(filePath)).Name;

                foreach (var payment in sr.ReadToEnd().Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries).AsEnumerable())
                {
                    string[] strCols = payment.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    lstpayment.Add(new payment { ACHTransactionCodeM = strACHTransactionCodeM, Control9 = strCols[1], DestinationAccountType2 = strCols[2] });
                }
            }
              return lstpayment;
            }
    }
}
Posted
Updated 11-Dec-19 4:01am
v5
Comments
F-ES Sitecore 11-Dec-19 5:47am
   
Which line throws the error?
tp09 11-Dec-19 7:40am
   
string strACHTransactionCodeM = (new FileInfo(filePath)).Name;
CHill60 11-Dec-19 7:11am
   
Looks like there is nothing in textbox.Text
CHill60 11-Dec-19 8:31am
   
See the solution from OriginalGriff and put your breakpoint on the line
List<payment> payments = DataTableReader(textbox.Text);
As I said, looks like textbox.Text is empty
tp09 12-Dec-19 2:37am
   
ok
Rate this:
Please Sign up or sign in to vote.

Solution 1

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

As others have said, if the texbox is empty you'll get that error. That textbox will only have a value if "ofd.ShowDialog()" is true, if it is false then the textbox remains empty. You probably want to abort the whole process if the dialog returns false, or only process the file if it is true, so something like below

if (ofd.ShowDialog() == true)
{
    string filename = ofd.FileName;
    textbox.Text = filename;
            
    List<payment> payments = DataTableReader(textbox.Text);
    lvPayments.ItemsSource = payments ;
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100