Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I set My modelView in data context like the code below :
but I get this one error parameter name path cannot be null in this line :
<local:ViewModel/>

I limit the problem in the Text file reader used in my public model view ,
Cause when I changed the configuration management.appsetting with a simple string path the error has disappear .

My question is , how can I set My view model In data context while I'm using app config/setting configurataion management in my view model constructor ?

Thank you for you help and attention.

What I have tried:

<Window x:Class="BancProduction.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:BancProduction"
        Title="MainWindow" Height="432.4" Width="687.4">
      
    <Window.DataContext>
        <local:ViewModel/>
    </Window.DataContext>


This is my first partcode of my Viewmodel:

namespace BancProduction
{
    class ViewModel : INotifyPropertyChanged
    {

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
    

        #endregion
  
        public void RaisePropertyChanged([CallerMemberName] string str = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(str));
            }
        }

 public search SearchCritaire
        {
            get
            {
                return Search;
            }
            set
            {
                if (value != Search)
                {
                    Search = value;
                    RaisePropertyChanged();
                }
            }
        }
     
        private ObservableCollection<search> famille;
   
        public ObservableCollection<search> Famille { get { return famille; }
            set { if (value != famille)
                {
                 famille = value; RaisePropertyChanged(); }
            
            } }

             public ViewModel()
        {   
          //this is the line reader that makes the error   
         string[] lineOfContents = File.ReadAllLines(ConfigurationManager.AppSettings["FiletextPath"]);

            MyList= new ObservableCollection<search>();           
           foreach (var line in lineOfContents)
           {
       string myline = line ;
                SearchCritaire = new search() { banc = line};
               

                MyList.Add(SearchCritaire);

            }

          }



I'm using the the binding method with view model , so I'm not working with the code behind method :

namespace BancProduction
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
}

}
Posted
Updated 19-Sep-19 23:04pm
v7
Comments
jimmson 11-Sep-19 5:52am    
What the class ViewModel looks like?
EM_Y 11-Sep-19 10:15am    
hello Jim ,
I updated my modelView UP ! please check it
jimmson 11-Sep-19 10:31am    
I don't think the error raises anywhere in the code you've posted. There's no "path" parameter in the code and you don't execute any method that requires one. You gonna have to dig deeper. Do you have an exception details? Look for stack trace and inner exceptions. Feel free to update your question with those information.
jimmson 11-Sep-19 7:29am    
There's probably not much one can figure out from what you've posted here. At least for me, the code looks fine and most likely the problem is somewhere else. As I mentioned, posting the class you're setting as your DataContext, or the code behind for given Window, would give much more insight to the problem.
EM_Y 11-Sep-19 11:58am    
Hi Jim I found where the problem was : I have this public view model , when I have masked the code content the error has disappear , I wish you find the cause related between my public viewmodel and the data context .

   public ViewModel()
        {   
            
         string[] lineOfContents = File.ReadAllLines(ConfigurationManager.AppSettings["FiletextPath"]);

            MyList= new ObservableCollection<search>();           
           foreach (var line in lineOfContents)
           {
       string myline = line ;
                SearchCritaire = new search() { banc = line};
               

                MyList.Add(SearchCritaire);

            }

          }

I'm pretty sure the exception is thrown from the line:
string[] lineOfContents = File.ReadAllLines(ConfigurationManager.AppSettings["FiletextPath"]);

Check your app.config file and the FiletextPath app setting. Either it's missing, you've spelled it wrong or the value is empty.
 
Share this answer
 
Comments
EM_Y 12-Sep-19 9:17am    
YEs its related with the app.config ,cause when I replace the configAPPsetting with a simple string path , the error disappear .
foreach (string line in File.ReadAllLines("D:\\All Documents\\Famille.txt"))
            {
             } 


Thank you Jim I'll try to figure out and poste the solution .
public ViewModel()
{
    MyList = new ObservableCollection<search>();
    
    string path = ConfigurationManager.AppSettings["FiletextPath"];
    if (!string.IsNullOrEmpty(path) && File.Exists(path))
    {
        foreach (string line in File.ReadLines(path))
        {
            MyList.Add(new search { banc = line });
        }
    }
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900