none
Console Application- Exception while SAVE Button click in C# WPF RRS feed

  • Question

  •  Im using Windows Application in WPF C# Lang. and Console window is only for Print Statements. Writting a Program a Load XML value to Listbox and SAVE Listbox items to XML flie. Listbox take items from TextBox. when Add button click textbox item added to Listbox. 

    now my problem is :  when im click on SAVE Button im getting some exception and application will close Automatically. Getting Exception Like This :

    at System.IO.Filestream.Init(String Path, FileMode mode, FileAccess Access, Int32 rights, boolean useRights, FileStream Share, int32 buffersize, fileoption option, SECURITY_ATTRIBUTES secattrs, string msgPath, boolean bFfromProxy, boolean uselongpath, boolean checkhost)
    at System.IO.FileStream..cattr(StringPath, FileMode, fileacces access, FileShare share, Int32 bufferSize)
    at System.XML.xmldownloadmanager.getstream(Uri uri,ICredentials credentials, IWebProxy Proxy, REquestCachePolicy Cachepoliy)at System.XML.xmlURLResolver.getentity(Uri absoluteuri, string role, type ofobjectToReturn)
    at System.Xml.xmltextReaderImpl.FinishInItUriString()
    at System.Xml.xmlREaderSettings.CreateReader(String input uri, XmlParserContext Inputcontext)
    at System.Xml.Linq.xElement.Load(String uri.LoadOption LoadOption)at MainWindow.Button_Click(object sender, Routed EventArgs e)
    at System.Windows.EventRoute.InvokehandlerImpl(Objext Source, RoutedEventArgs Args, Boolean reRaise)
    at System.Windows.UIElement.RaiseEventImpl(DependencyObject Sender, RoutedEventArgs Args)
    at System.Windows.Control.Premitives.buttonbase.OnClick()at System.Windows.Control.Button.OnClick()
    at System.Windows.Control.Primitives.ButtonBase. OnmouseLeftButtonUp(MouseButtonEventArgs E)
    at System.Windows.RoutedEventArgs.InvokeHandlers(Deleget Handler, ObjectTarget)
    at System.Windows.EventRoute.InvokeHandlerImpl(Object Source, RoutedEventArgs Args, Boolean ReRaised)
    at System.Windows.UIElement.ReRaiseEventAs(DependencyObject Sender, RoutedEventArgs Args, RoutedEvent NewEvent)
    at System.Windows.UIElement.OnMouseUPThunk(Object Sender, MouseButtonEventArgs e)
    at System.Windows.RoutedEventArgs.InvokeHandler(Delegete Handler, Object Target)
    at System.Windows.RoutedEventHandlerInfo invokeHandler(Object target, RoutedEventArgs RoutedEventargs)
    at System.Windows.EventRoute.InvokeHandlerImpl(Object Source, RoutedEventArgs Args, Boolean Reraised)
    at System.Windows.UIElement.RaiseEventImpl(DependencyObject Sender, RoutedEventArgs Args)
    at System.Windows.UIElement.RaiseTrustedEvent (RoutedEventArgs Args)
    at System.Windows.Input.InputManager.ProcessStageingArea()
    at System.Windows.Input.InputManager.ProcessInput (InoutEventArgs Input)
    at System.Windows.Input.InputProviderSite.ReportInput (InputReport inputreport)

    My Code is :

    private void LoadXML_Click(object sender, RoutedEventArgs e)
            {
                XElement element = XElement.Load("WH_Num.xml");
                foreach (XElement item in element.Elements("Number1"))
                    ListBox_PhoneNumber.Items.Add(item.Value);
                foreach (XElement item in element.Elements("Number2"))
                    ListBox_PhoneNumber.Items.Add(item.Value);
                foreach (XElement item in element.Elements("Number3"))
                    ListBox_PhoneNumber.Items.Add(item.Value);
                foreach (XElement item in element.Elements("Number4"))
                    ListBox_PhoneNumber.Items.Add(item.Value);
                foreach (XElement item in element.Elements("Number5"))
                  
                MessageBox.Show("Number Loaded to XML");
            }
    
            private void SAVEXML_Click(object sender, RoutedEventArgs e)
            {
                try
                   {
                    information info = new information();
                    if (ListBox_PhoneNumber.Items.Count > 0) info.Number1 = ListBox_PhoneNumber.Items[0].ToString();
                    if (ListBox_PhoneNumber.Items.Count > 1) info.Number2 = ListBox_PhoneNumber.Items[1].ToString();
                    if (ListBox_PhoneNumber.Items.Count > 2) info.Number3 = ListBox_PhoneNumber.Items[2].ToString();
                    if (ListBox_PhoneNumber.Items.Count > 3) info.Number4 = ListBox_PhoneNumber.Items[3].ToString();
                    if (ListBox_PhoneNumber.Items.Count > 4) info.Number5 = ListBox_PhoneNumber.Items[4].ToString();
                  
    
                    SaveXML.savedata(info, "WH_Num.xml");
                    MessageBox.Show("Data Saved");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }      

    Where im missing?? Why this exception is coming?? Anyone help me

    SaveXML Function is as follows :

     class SaveXML
        {
            public static void savedata(object obj, string filename)
            {
                XmlSerializer sr = new XmlSerializer(obj.GetType());
                TextWriter writer = new StreamWriter(filename);
                sr.Serialize(writer, obj);
                writer.Close();
            }
        }


    Thursday, May 21, 2020 7:45 AM

All replies

  • Hello,

    What type of object is SaveXML, also it seems odd that savedata is not SaveData.

    Have you tried setting a breakpoint on SaveXML line and then examine the contents via the debugger of info ?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, May 21, 2020 9:47 AM
  • SaveXML Function is Updated in my Question. Please check the Question.
    Thursday, May 21, 2020 10:43 AM
  • Put a breakpoint on the highlighted line, step through the code, examine values of variable until the exception is thrown.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, May 21, 2020 10:48 AM
  • Debugging is not Possible for me here. any other method u know? 

    Thursday, May 21, 2020 3:50 PM
  • The details does not seem to indicate a problem in your program and does not seem a faithful copy of exception details. How did you get this stack trace? Is it complete?   

    • Marked as answer by Sahana M Friday, May 22, 2020 4:08 AM
    • Unmarked as answer by Sahana M Friday, May 22, 2020 4:08 AM
    Thursday, May 21, 2020 6:23 PM
  • What is the Message in the exception?

    Where is the error occurring? I assume in  SaveXML.savedata; what line? It might be that the program does not have write access for the current directory during execution and if so then the error message would help. Otherwise there could be a problem with the information class and if so then again the error message would help.



    Sam Hobbs
    SimpleSamples.Info

    Thursday, May 21, 2020 11:50 PM
  • Hi Sahana M,
    Based on your description, it is mostly related to WPF. So it is recommended to ask the questions in Microsoft Q&A forum and you can get more professional answer.
    Thank you for your understanding.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 22, 2020 1:20 AM
  • data is Saved in XML file after that issue is coming and application will corrupting.
    Friday, May 22, 2020 4:09 AM
  • how to give write access for directory??

    Friday, May 22, 2020 4:12 AM
  • We do not know what the directory is; you did not tell us.

    Nearly always the solution is to specify a different directory to write to. We do not know what the requirements are for your application so we cannot be sure what would be the best way to specify a directory.



    Sam Hobbs
    SimpleSamples.Info

    Friday, May 22, 2020 4:20 AM