Results 1 to 4 of 4

Thread: SaveFileDialog question

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    SaveFileDialog question

    I have a file stored on a SQL server. I want the user to click a button and get asked where they would like to store that file and then save it there. I thought a savefiledialog would be the best but have never used it before. Currently I have this code and it saves the file but always in the same location.

    id = the selected primary key in a listview
    FileName = the filename stored in the SQL database

    Code:
    SqlConnection con = new SqlConnection("Server=Darkover;uid=<username>;pwd=<strong password>;database=northwind");
    SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages where DocID = ' " + id + "'", con);
    SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
    DataSet ds = new DataSet("MyImages");
    
    byte[] MyData= new byte[0];
    			
    da.Fill(ds, "MyImages");
    DataRow myRow;
    myRow=ds.Tables["MyImages"].Rows[0];
               
    MyData =  (byte[])myRow["imgField"];
    int ArraySize = new int();
    ArraySize = MyData.GetUpperBound(0); 
    String Filename;
    Filename = (string)myRow["FileName"];
    FileStream fs = new FileStream(@"C:\temp\" + Filename, FileMode.OpenOrCreate, FileAccess.Write);
    fs.Write(MyData, 0,ArraySize);
    fs.Close();
    }

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: SaveFileDialog question

    The SaveFileDialog is merely an interface to allow the user to select a location for saving a file. It's then up to you to do the saving. The path selected by the user is returned by the SFD's FileName property, so you use that as the path to which your code saves the file.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Re: SaveFileDialog question

    But they can choose the filename or can you lock that out so the cannot enter any info. I know I could just take the path and add my filename but if they are expecting the filename they put then it could be confusing for them.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: SaveFileDialog question

    If you want the user to select a folder then use the FolderBrowserDialog, not the SaveFileDialog.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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