Results 1 to 11 of 11

Thread: Help: Save Solution

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2011
    Posts
    65

    Help: Save Solution

    Im trying to save a file in XML and offer the user a save dialog to appear so they can name the file. The file will save using a custom file extention. The problem is I keep getting off the wall suggestions and Im looking for a simple solution. Here is what I have, please only show examples using my syntax based code. Im still a bit new to Vb so posting off the wall examples will not help in any way shape or form thank you.

    Code:
     Private FileDialog As SaveFileDialog
        Public Sub New()
            InitializeComponent()
            FileDialog = New SaveFileDialog()
            FileDialog.Filter = "UOK Application | *.App"
            FileDialog.DefaultExt = "app"
        End Sub
    
        Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
            Dim AppSerializer As Xml.Serialization.XmlSerializer = New Xml.Serialization.XmlSerializer(GetType(Storage))
            Dim fileStream As System.IO.Stream = FileDialog.OpenFile()
            Dim SW As New System.IO.StreamWriter(FileDialog)
    
            SaveFileDialog1.ShowDialog()
            AppSerializer.Serialize(SW, Storage)
            SW.Close()
            Me.Update()
        End Sub

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Help: Save Solution

    Is there a problem with the code itself ? Is it not saving or something ?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2011
    Posts
    65

    Re: Help: Save Solution

    Dim SW As New System.IO.StreamWriter(FileDialog)

    Syntax error on the SW

    Error 1 Overload resolution failed because no accessible 'New' can be called with these arguments:
    'Public Sub New(path As String)': Value of type 'System.Windows.Forms.SaveFileDialog' cannot be converted to 'String'.
    'Public Sub New(stream As System.IO.Stream)': Value of type 'System.Windows.Forms.SaveFileDialog' cannot be converted to 'System.IO.Stream'. C:\Users\Admin\documents\visual studio 2010\Projects\UOK Software Intern Application\UOK Software Intern Application\MainForm.vb 35 13 UOK Software Intern Application

    If you remove the "New" is generates more errors so Im lost and Im sure there is a easy way to accomplish what I need as described above. Im just too newb to figure it out.

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Help: Save Solution

    Whoa whoa, I just really looked at your code and you got a lot of things wrong. You really are new at this. You were mismatching data types left and right. However it was a great effort for someone so new. Here is what your code should look like:-
    vbnet Code:
    1. Public Class Form1
    2.     Private g_data As New Storage With {.SampleData = "Hello"}
    3.  
    4.     Private FileDialog As SaveFileDialog
    5.     Public Sub New()
    6.         InitializeComponent()
    7.         FileDialog = New SaveFileDialog()
    8.         FileDialog.Filter = "UOK Application | *.app"
    9.         FileDialog.DefaultExt = "app"
    10.     End Sub
    11.  
    12.     Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    13.         Dim dr As DialogResult = FileDialog.ShowDialog()
    14.  
    15.         Dim fileName As String = FileDialog.FileName
    16.         Dim AppSerializer As Xml.Serialization.XmlSerializer = New Xml.Serialization.XmlSerializer(GetType(Storage))
    17.  
    18.         Dim fileStream As New System.IO.FileStream(fileName, IO.FileMode.Create)
    19.         Dim SW As New System.IO.StreamWriter(fileStream)
    20.  
    21.         AppSerializer.Serialize(SW, g_data)
    22.  
    23.         SW.Close()
    24.     End Sub
    25.  
    26. End Class
    27.  
    28. Public Class Storage
    29.     Public SampleData As String
    30. End Class
    Now I dont know what your storage class looks like so I had to make my own to help me to work this out for you so use this code as a template so you can get your own working right.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2011
    Posts
    65

    Re: Help: Save Solution

    Thanks man, thats way better then what the MSDN kept trying to suggest after I told them a million times I wanted something I could understand. Storage is a class I created so I could save the data to a XML document. Because my form uses multipal text boxes and various pages, I needed to make a class to store it all. Ill post a video demo of the software once I get this plugged in. Again though thanks, I hope this works lol

    Edit:
    Tested it and it works. Awsome stuff guys.
    Last edited by mholmes_3038; Feb 7th, 2012 at 10:27 AM.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jul 2011
    Posts
    65

    Re: Help: Save Solution

    Arg now the load does nto want to work. I had it working bit now its throwing a exception. Here is the load code.

    Code:
     Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
            'Load Action
            Dim AppSerializer As Xml.Serialization.XmlSerializer = New Xml.Serialization.XmlSerializer(GetType(Storage))
            Dim AppFileStream As FileStream = New FileStream(".App", FileMode.Open)
            OpenFileDialog1.Title = "Windows Explorer"
            OpenFileDialog1.ShowDialog()
            Storage = CType(AppSerializer.Deserialize(AppFileStream), Storage)
            FormUpdate()
            AppFileStream.Close()
        End Sub
    System.IO.FileNotFoundException was unhandled
    FileName=C:\Users\Admin\documents\visual studio 2010\Projects\UOK Software Digital Application\UOK Software Digital Application\bin\Debug\.App
    Message=Could not find file 'C:\Users\Admin\documents\visual studio 2010\Projects\UOK Software Digital Application\UOK Software Digital Application\bin\Debug\.App'.
    Source=mscorlib
    StackTrace:
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
    at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
    at System.IO.FileStream..ctor(String path, FileMode mode)
    at UOK_Software_Digital_Application.Page_1.OpenToolStripMenuItem_Click(Object sender, EventArgs e) in C:\Users\Admin\documents\visual studio 2010\Projects\UOK Software Digital Application\UOK Software Digital Application\Page_1.vb:line 13
    at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
    at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
    at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
    at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
    at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
    at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
    at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
    at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
    at System.Windows.Forms.Control.WndProc(Message& m)
    at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
    at System.Windows.Forms.ToolStrip.WndProc(Message& m)
    at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
    at UOK_Software_Digital_Application.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    at System.Threading.ThreadHelper.ThreadStart()
    InnerException:

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Help: Save Solution

    vbnet Code:
    1. Dim AppFileStream As FileStream = New FileStream(".App", FileMode.Open)
    You didnt provide a proper file name here.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jul 2011
    Posts
    65

    Re: Help: Save Solution

    Ahhh
    New FileStream(filename, FileMode.Open)

    work?

  9. #9
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Help: Save Solution

    yep.....

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jul 2011
    Posts
    65

    Re: Help: Save Solution

    getting a error on the load action

    Could not find file 'C:\Users\Admin\documents\visual studio 2010\Projects\UOK Software Digital Application\UOK Software Digital Application\bin\Debug\.app'.

    Code:
    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
            'Load Action
            Dim AppSerializer As Xml.Serialization.XmlSerializer = New Xml.Serialization.XmlSerializer(GetType(Storage))
            Dim AppFileStream As FileStream = New FileStream(".app", FileMode.Open)
            OpenFileDialog1.Filter = "UOK Application | *.App"
            OpenFileDialog1.DefaultExt = "app"
            OpenFileDialog1.Title = "Windows Explorer"
            OpenFileDialog1.ShowDialog()
            Storage = CType(AppSerializer.Deserialize(AppFileStream), Storage)
            FormUpdate()
            AppFileStream.Close()
        End Sub
    Edit:
    well i got it to work but not a fan of the result. I have to have a file called .app inside there or it will crash. suggestions?
    Last edited by mholmes_3038; Feb 19th, 2012 at 07:26 PM.

  11. #11
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Help: Save Solution

    vbnet Code:
    1. Dim AppFileStream As FileStream = New FileStream(".app", FileMode.Open)
    You made the same mistake again. ".app" is not a proper file name.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width