Click here to Skip to main content
15,886,518 members
Articles / Programming Languages / SQL

A CRUD Form using SharpDevelop and PostgreSQL

Rate me:
Please Sign up or sign in to vote.
4.67/5 (5 votes)
21 Oct 2009CPOL5 min read 58.5K   2.3K   15  
This article is a tutorial on how to create a CRUD (Create, Retrieve, Update and Delete) Form using SharpDevelop and PostgreSQL, implementing a BindingNavigator to explore the table used in the application.
/*
 * Created by SharpDevelop.
 * User: wancho
 * Date: 20/10/2009
 * Time: 14:37
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Npgsql;
namespace Music
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void MainFormLoad(object sender, EventArgs e)
		{
			// We fill our DataSet and we set the table in the Dataset as "artists"
			adapter.Fill(mainDS,"artists");
			// We must set the DataMember property in the BindingSource
			// it cannot be set at the designer since our DataSet isn't populated
		   bsource.DataMember="artists";
		   // We add the DataBinding to the TextBox linked to the "Text" property
		   // and linked to the BindingSource
		   t_artistID.DataBindings.Add("Text",bsource,"artistid");
		   t_name.DataBindings.Add("Text",bsource,"name");
          // Now we must set the Relation between the parameters and the fields 
          // in the Database
          cmdInsert.Parameters.Add(new NpgsqlParameter("p_artistID",NpgsqlTypes.NpgsqlDbType.Integer,0,"artistid"));
          cmdInsert.Parameters.Add(new NpgsqlParameter("p_name",NpgsqlTypes.NpgsqlDbType.Varchar,0,"name"));
          cmdDelete.Parameters.Add(new NpgsqlParameter("p_artistID",NpgsqlTypes.NpgsqlDbType.Integer,0,"artistid"));
          cmdUpdate.Parameters.Add(new NpgsqlParameter("p_artistID",NpgsqlTypes.NpgsqlDbType.Integer,0,"artistid"));
          cmdUpdate.Parameters.Add(new NpgsqlParameter("p_name",NpgsqlTypes.NpgsqlDbType.Varchar,0,"name"));
          
		}
		
		void ToolStripButton1Click(object sender, EventArgs e)
		{
			// We call this method to officialy end the editing of the Bindingsource
			bsource.EndEdit();
			// Now we will try to update our table
			try {
				adapter.Update(mainDS.Tables["artists"]);
			}
			// We must catch the Exception, because the user may cause a SQL exception 
			catch (Exception e_exception)
			{
				MessageBox.Show("Error.... "+e_exception.Message,"Error tryng to commit");
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer TuXSTONe Technologies
Colombia Colombia
Colombian, TuXSTONe Technologies developer,
Software Developer since 1996,
Computers Engineer and Network Software Developing Specialist since 2003.

Experience in developing software using PHP,C, C#, Java, Perl.

Comments and Discussions