C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Open File (database) using OpenFileDialog & Save (New) File using SaveFileDialog component


Posted Date: 14 Jun 2004    Resource Type: Articles    Category: .NET Framework

Posted By: Prajeesh.Kottarath.S       Member Level: Bronze
Rating:     Points: 15



1. Create an MDI parent form and insert menu with Open and New.
2. Create another form as MDIChild with a TreeView control in it.(Refer my previous article of How to populate a TreeView control from an Access database....! )
3. On menuOpen_Click event add the given codes....


private void menuFileOpen_Click(object sender, System.EventArgs e)
{
OpenFileDialog openDlg = new OpenFileDialog();
openDlg.Filter = "DataBase Files (*.mdb)|*.mdb";
openDlg.FileName = "" ;
openDlg.DefaultExt = ".mdb";
openDlg.CheckFileExists = true;
openDlg.CheckPathExists = true;

DialogResult res = openDlg.ShowDialog ();

if(res == DialogResult.OK)
{
if ( ! ( openDlg.FileName ).EndsWith(".mdb") && ! ( openDlg.FileName ).EndsWith (".MDB") )
MessageBox.Show( "Invalid file format...!" ,
"Prajeesh" , MessageBoxButtons.OK , MessageBoxIcon.Exclamation ) ;

else
{
TreeView tree = new TreeView();
tree.Close(); //ActiveMdiChild.Close();

DatabaseName = openDlg.FileName;

//Initialises the connection with the database with the datasource as "DatabaseName"
TreeViewLoad( DatabaseName );
}
}
::--::--::::--::--::::--::--::::--::--::::--::--::::--::--::
public void TreeViewLoad(string dbName)
{
TreeView tree = new TreeView();

//passes the DatabaseName to the TreeView class.
tree.DatabaseName = dbName;
tree.MdiParent = this;
tree.Show();
}


::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::::--::

5. Create a template database as a source.
4. On menuNew_Click event add the given codes....



SaveFileDialog saveFileDlg = new SaveFileDialog();
saveFileDlg.Filter = "Database File (*.mdb)|*.mdb" ;
saveFileDlg.Title = "Create New Database";
saveFileDlg.RestoreDirectory = false;
if(saveFileDlg.ShowDialog() == DialogResult.OK)
{
TreeView tree = new TreeView();

// Template database path
string sourceFile = @"C:\prajeesh\My Documents\\SampleProject\Template.mdb";
DatabaseName = saveFileDlg.FileName;
try
{
File.Copy ( sourceFile , DatabaseName ) ;
TreeViewLoad ( DatabaseName );
}
catch ( Exception ex )
{
MessageBox.Show( ex.Message ) ;
}
}


:--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::::--::--::
In the TreeView class you can do the following on form load event
Initialises the database connection

private string Provider = "Microsoft.Jet.OLEDB.4.0";
private string User = "Admin";
private string Password = "";
private string Mode = "ReadWrite";
public string DatabaseName;

private void TreeView_Load(object sender, System.EventArgs e)
{

this.dbConnection = new System.Data.OleDb.OleDbConnection();

StringBuilder strBuild = new StringBuilder();
strBuild.AppendFormat( "Provider={0};Password=\"{1}\";User ID={2};Data Source={3};Mode={4}",
Provider, Password, User, DatabaseName, Mode );

dbConnection2.ConnectionString = strBuild.ToString();

}


Refer my previous article to continue with the populating part!!....

More articles coming up soon.....
Until then....Happy Coding
Regards,
Prajeesh :-)





Responses

Author: critic    14 Jun 2004Member Level: Bronze   Points : 0
I is a reasonably good article.. but in the space provided to enter all used .NET classes, you have simply given the namespaces !!


Author: SAM     14 Jul 2004Member Level: Bronze   Points : 0
Hi Prajeesh:

Do you have this code in VB.NET?

RGDS
Sam


Author: zxn    12 Apr 2005Member Level: Bronze   Points : 0
thanks


Author: Biju Samuel    13 Apr 2005Member Level: Bronze   Points : 0
I want to save a file to pdf format. pls give me the code.in c#


Author: Varsha    27 Aug 2008Member Level: Bronze   Points : 0
Can you send this code in Vb.net


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
SaveFileDialog  .  OpenFileDialog  .  File Open  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How the .NET Framework helps to get rid of the DLL Hell Problem?
Previous Resource: Parsing of XML data [DOM traversal]
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

fax server

Contact Us    Privacy Policy    Terms Of Use