CRUD Operations in PostgreSQL Database Using ASP.Net With C#

Introduction

 
 
Image Source: File:Postgresql elephant.svg
 
Last time I provided an overview of the open-source database named PostgreSQL aka Postgres. To learn more click here Overview of Open Source Postgre SQL Database.
 
Note: Now I am assuming that you have the knowledge of Postgres on behalf of the preceding article and I have a table named "employee" in the database named "test".
 
Here is the schema of table and table content (0 rows).
 
 
Now I will explain how to do the CRUD operations in Postgres.
 
CRUD operations mean:
  • C- Create means "Insert the data".
  • R- Read means "Select the data".
  • U- Update means "Update the data".
  • D- Delete means "Delete the data".
Reference: Create, read, update and delete.
 
To do the operations in Postgres using ASP.Net, you need to download the Data Provider named "Npgsql" like "sqlclient", "oledb", "odbc" and so on.
 
You can download it from here .Net Data Provider for Postgresql.
 
After downloading it, you will find the Npgsql.dll file in the folder.
 
To learn more about CRUD operations in postgre using ASP.Net, use the proceeding procedure:
 
Step 1
Create a website named "Test_Website".
 
 
Step 2
Add the reference of the downloaded Npgsql.dll by right-clicking on the website in the Solution Explorer.
 
 
And then browse to the path (of the proceeding sub-steps) where Npgsql.dll was saved and then:
  1. Click on the Browse tab.
  2. Click on the Browse button.
  3. Select the path of Npgsql.dll.
  4. Click on the Add button.
  5. Click on the OK button.
 
After adding the reference of Npgsql.dll to the website, it will look like this:
 
 
Step 3
Now to do the CRUD operations using Npgsql.dll.
 
Inserting: To insert the data into the "Postgres" database, add the following controls on the default page named "Default.aspx" for doing the insert:
  • 1 TextBox for Employee ID.
  • 1 TextBox for Employee First Name.
  • 1 TextBox for Employee Last Name.
  • 1 TextBox for Employee Email ID.
  • 1 button for Submit.
  • 1 label for messages.
  1. <table>  
  2.     <tr>  
  3.         <td colspan="2">  
  4.             <h1>Insertion</h1>  
  5.         </td>  
  6.     </tr>  
  7.     <tr>  
  8.         <td>Employee ID</td>  
  9.         <td>:</td>  
  10.         <td>  
  11.             <asp:TextBox ID="txtEmpID" runat="server"></asp:TextBox>  
  12.         </td>  
  13.     </tr>  
  14.     <tr>  
  15.         <td>Employee First Name</td>  
  16.         <td>:</td>  
  17.         <td>  
  18.             <asp:TextBox ID="txtEmpFname" runat="server"></asp:TextBox>  
  19.         </td>  
  20.     </tr>  
  21.     <tr>  
  22.         <td>Employee Last Name</td>  
  23.         <td>:</td>  
  24.         <td>  
  25.             <asp:TextBox ID="txtEmpLname" runat="server"></asp:TextBox>  
  26.         </td>  
  27.     </tr>  
  28.     <tr>  
  29.         <td>Employee Email-ID</td>  
  30.         <td>:</td>  
  31.         <td>  
  32.             <asp:TextBox ID="txtEmpEmail" runat="server"></asp:TextBox>  
  33.         </td>  
  34.     </tr>  
  35.     <tr>  
  36.         <td colspan="2">  
  37.             <asp:Button ID="btnInsertion" runat="server" Text="Insert" OnClick="btnInsertion_Click" Style="width: 48px" />  
  38.             <asp:Label ID="lblmsg" runat="server" ForeColor="Red"></asp:Label>  
  39.         </td>  
  40.     </tr>  
  41. </table>  
Design View: it will look like.
 
 
Add some namespaces to the .cs code file of the default page.
  1. using System.Data;  
  2. using System.Configuration;  
  3. using Npgsql;  
  4. Add the proceed code on the click event of the button  
  5. protected void btnInsertion_Click(object sender, EventArgs e) {  
  6.     try {  
  7.         /* Insertion After Validations*/  
  8.         using(NpgsqlConnection connection = new NpgsqlConnection()) {  
  9.             connection.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ToString();  
  10.             connection.Open();  
  11.             NpgsqlCommand cmd = new NpgsqlCommand();  
  12.             cmd.Connection = connection;  
  13.             cmd.CommandText = "Insert into employee values(@ID,@Fname,@Lname,@Email)";  
  14.             cmd.CommandType = CommandType.Text;  
  15.             cmd.Parameters.Add(new NpgsqlParameter("@ID", Convert.ToInt32(txtEmpID.Text)));  
  16.             cmd.Parameters.Add(new NpgsqlParameter("@Fname", txtEmpFname.Text));  
  17.             cmd.Parameters.Add(new NpgsqlParameter("@Lname", txtEmpLname.Text));  
  18.             cmd.Parameters.Add(new NpgsqlParameter("@Email", txtEmpEmail.Text));  
  19.             cmd.ExecuteNonQuery();  
  20.             cmd.Dispose();  
  21.             connection.Close();  
  22.             txtEmpEmail.Text = "";  
  23.             txtEmpFname.Text = "";  
  24.             txtEmpID.Text = "";  
  25.             txtEmpLname.Text = "";  
  26.             lblmsg.Text = "Data Has been Saved";  
  27.         }  
  28.     } catch (Exception ex) {}  
  29. }
 
Run the default page that will look like the following after filling in the data.
 
 
After submitting the data:
 
 
You can see the data in the database.
 
 

Selection

 
I have a table named "employee" to select the data from the "Postgres" database, after adding some rows of data into the table.
 
 
Add the following controls on the default page named "Default.aspx" for inserting:
  • 1 button for Submit.
  • 1 Grid view to display the data.
  1. <table>  
  2.     <tr>  
  3.         <td>  
  4.             <h1>Selection</h1>  
  5.         </td>  
  6.     </tr>  
  7.     <tr>  
  8.         <td>  
  9.             <asp:Button ID="btnSelect" runat="server" Text="Select All Data" OnClick="btnSelect_Click" />  
  10.         </td>  
  11.     </tr>  
  12.     <tr>  
  13.         <td>  
  14.             <asp:GridView ID="GridView1" runat="server"></asp:GridView>  
  15.         </td>  
  16.     </tr>  
  17. </table>  
Design View: it will look like:
 
 
These 3 namespaces should be added to the .cs code file of the default page.
  1. using System.Data;  
  2. using System.Configuration;  
  3. using Npgsql;  
  4. Add the proceed code on the click event of the button  
  5. protected void btnSelect_Click(object sender, EventArgs e) {  
  6.     try /* Select After Validations*/  
  7.     {  
  8.         using(NpgsqlConnection connection = new NpgsqlConnection()) {  
  9.             connection.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ToString();  
  10.             connection.Open();  
  11.             NpgsqlCommand cmd = new NpgsqlCommand();  
  12.             cmd.Connection = connection;  
  13.             cmd.CommandText = "Select * from employee";  
  14.             cmd.CommandType = CommandType.Text;  
  15.             NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd);  
  16.             DataTable dt = new DataTable();  
  17.             da.Fill(dt);  
  18.             cmd.Dispose();  
  19.             connection.Close();  
  20.   
  21.             GridView1.DataSource = dt;  
  22.             GridView1.DataBind();  
  23.         }  
  24.     } catch (Exception ex) {}  
  25. }
 
Run the default page that will look like this:
 
 
After selecting the data:
 
 

Updating

 
I have a table named "employee" to update the data from the "Postgres" database, after adding some rows of data into the table.
 
 
Add the following control to the default page named "Default.aspx" for inserting:
 
1 button to update the data in the insertion section
  1. <asp:Button ID="btnUpdation" runat="server" Text="Update" OnClick="btnUpdation_Click" />
Design View: it will look alike
 
 
These 3 namespaces should be added in the .cs code file of the default page.
  1. using System.Data;  
  2. using System.Configuration;  
  3. using Npgsql;  
  4. /*Add the proceed code on the click event of the button*/    
  5. protected void btnUpdation_Click(object sender, EventArgs e) {  
  6.     try /* Updation After Validations*/  
  7.     {  
  8.         using(NpgsqlConnection connection = new NpgsqlConnection()) {  
  9.             connection.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ToString();  
  10.             connection.Open();  
  11.             NpgsqlCommand cmd = new NpgsqlCommand();  
  12.             cmd.Connection = connection;  
  13.             cmd.CommandText = "update employee set firstname=@Fname,lastname=@Lname,emailid=@Email where id=@ID";  
  14.             cmd.CommandType = CommandType.Text;  
  15.             cmd.Parameters.Add(new NpgsqlParameter("@ID", Convert.ToInt32(txtEmpID.Text)));  
  16.             cmd.Parameters.Add(new NpgsqlParameter("@Fname", txtEmpFname.Text));  
  17.             cmd.Parameters.Add(new NpgsqlParameter("@Lname", txtEmpLname.Text));  
  18.             cmd.Parameters.Add(new NpgsqlParameter("@Email", txtEmpEmail.Text));  
  19.             cmd.ExecuteNonQuery();  
  20.             cmd.Dispose();  
  21.             connection.Close();  
  22.             txtEmpEmail.Text = "";  
  23.             txtEmpFname.Text = "";  
  24.             txtEmpID.Text = "";  
  25.             txtEmpLname.Text = "";  
  26.             lblmsg.Text = "Data Has been Updated";  
  27.   
  28.         }  
  29.     } catch (Exception ex) {}  
  30. }
 
Run the default page that will look like the following after filling in the new data that you want to update.
 
 
Then click on the update button.
 
 
You can see the data changes in the database.
 
 

Deletion

 
I have a table named "employee" to delete the data from the "Postgres" database, after adding some rows of data into the table.
 
 
Add the following controls to the default page named "Default.aspx" for inserting:
  • 1 TextBox for employee ID.
  • 1 button for Deletion.
  • 1 label for message.
  1. <table>  
  2.     <tr>  
  3.         <td>  
  4.             <h1>Selection</h1>  
  5.         </td>  
  6.     </tr>  
  7.     <tr>  
  8.         <td>  
  9.             <asp:Button ID="btnSelect" runat="server" Text="Select All Data" OnClick="btnSelect_Click" />  
  10.         </td>  
  11.     </tr>  
  12.     <tr>  
  13.         <td>  
  14.             <asp:GridView ID="GridView1" runat="server"></asp:GridView>  
  15.         </td>  
  16.     </tr>  
  17. </table>  
  18. <table>  
  19.     <tr>  
  20.         <td colspan="3">  
  21.             <h1>Deletion</h1>  
  22.         </td>  
  23.     </tr>  
  24.     <tr>  
  25.         <td>Employee ID</td>  
  26.         <td>:</td>  
  27.         <td>  
  28.             <asp:TextBox ID="txtEmployeeID" runat="server"></asp:TextBox>  
  29.         </td>  
  30.     </tr>  
  31.     <tr >  
  32.         <td colspan="3">  
  33.             <asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_Click" />  
  34.             <asp:Label ID="lblmessage" runat="server" ForeColor="Red"></asp:Label>  
  35.         </td>  
  36.     </tr>  
  37. </table>  
Design View: it will look like:
 
 
These 3 namespaces should be added to the .cs code file of the default page.
  1. using System.Data;  
  2. using System.Configuration;  
  3. using Npgsql;  
  4. /*Add the proceed code on the click event of the button*/ 
  5. protected void btnDelete_Click(object sender, EventArgs e) {  
  6.     try /* Deletion After Validations*/  
  7.     {  
  8.         using(NpgsqlConnection connection = new NpgsqlConnection()) {  
  9.             connection.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ToString();  
  10.             connection.Open();  
  11.             NpgsqlCommand cmd = new NpgsqlCommand();  
  12.             cmd.Connection = connection;  
  13.             cmd.CommandText = "Delete from employee where id=@ID";  
  14.             cmd.CommandType = CommandType.Text;  
  15.             cmd.Parameters.Add(new NpgsqlParameter("@ID", Convert.ToInt32(txtEmployeeID.Text)));  
  16.             cmd.ExecuteNonQuery();  
  17.             cmd.Dispose();  
  18.             connection.Close();  
  19.             txtEmployeeID.Text = "";  
  20.             lblmessage.Text = "Data Has been Deleted";  
  21.         }  
  22.     } catch (Exception ex) {}  
  23. }
 
Run the default page and fill in the id that you want to delete.
 
 
Then click on the delete button.
 
 
You can see the remaining 2 rows in the database and earlier there were 3 rows.