Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Wednesday, July 29, 2015

Visual Studio C# | Converting String to Integer


Values you enter on the text fields are considered strings or series of characters regardless if it is a number or integer.



The sample application above converts string to number and them up then displays it on the label component.

Code:
   The action was triggered upon clicking the 'Add' button.

note:
   variable 'total' is declared as global variable with double type.


Tip:
   set 'FullRowSelect' to true of the list view property so that when you click an item of the list view, it will highlight the entire row.



Visual Studio C# | Allow Integer Input Only


There are certain field that you only want the user to input integer or numbers but there is always that doubt that they will accidentally press a non-numeric characters.

The best way to catch that one is to forbid the user to input a letters, special characters and any other characters that is not needed for your specific field.

For example, you are doing a form for a product and part of it's fields is the 'Price'. We all know  that the price contains numbers and period ONLY.

Add a textfield on your form and named it 'txtPrice'.


Add the following code on your textfield's KeyPress event.



Explanation:

   e.KeyChar : This gets the current value of the key pressed in the keyboard.
   char.IsControl :  returns true if the user presses 'ctrl', 'alt' , etc.
   char.IsDigit : returns true if the user presses a number key.

Logic:

  If pressed key is NOT a control key AND NOT a digit AND NOT a period, then
  SET e.handled to true, which means to cancel the event or that your handling the event in your code instead.

 If pressed key is a period check if the textfields already contains a period, then
 don't let the characted to be pressed. (e.handled = true)


Sources:

  • http://www.homeandlearn.co.uk/csharp/csharp_s8p4.html
  • http://www.dotnetperls.com/char-isdigit
  • http://stackoverflow.com/questions/463299/how-do-i-make-a-textbox-that-only-accepts-numbers

Friday, July 24, 2015

Visual Studio C# | Communicating with MS Access




One of the important thing when developing an application is to have a data storage where you can store all you important data whether it is from the user or for the user.

This post is all about connecting with MS Access as Database with Visual Studio C# and populate the retrieved data to the ListView component.


//initialize database connection
private OleDbConnection dbConn = new OleDbConnection();

//Connection string
string strConnString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Elfe Rain.Cisneros\Documents\Visual Studio 2013\Projects\Rental-System-Tool\Rental-System-Tool\bin\Debug\Rental.accdb;
Persist Security Info=False;";

//establish connection string
dbConn.ConnectionString = strConnString;

//Open database
dbConn.Open();

// establish query string
string strQuery = "Select * from Customer";

//Represents an SQL statement or stored procedure to execute against a data source.
OleDbCommand dbCommand = new OleDbCommand();

//Provides a way of reading a forward-only stream of data rows from a data source. This class cannot be inherited.
OleDbDataReader reader;

//Represents an item in a ListView control.
ListViewItem colItem;

// declaring a string of array with size 4
string[] colDataItem = new string[4];

// as a start, the listview is cleared.
listCustomer.Items.Clear();

            try //catch possible exception occurred.
            {
		//send connection and query
                dbCommand.Connection = dbConn;
                dbCommand.CommandText = strQuery;
                reader = dbCommand.ExecuteReader();
		
		// read all rows returned by the query
                while (reader.Read())
                {
                    colDataItem[0] = reader["CustomerID"].ToString();
                    colDataItem[1] = reader["LastName"].ToString();
                    colDataItem[2] = reader["FirstName"].ToString();
                    colDataItem[3] = reader["MiddleName"].ToString();

                    colItem = new ListViewItem(colDataItem);
                    listCustomer.Items.Add(colItem);
                    listCustomer.GridLines = true;


                } reader.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex);
            }

//close database connection
dbConn.Close();

The Result (listview populated):


It's up to your application flow / logic where to place the codes above. It depends on your needs and purposes.

Sources:
 Microsoft Library : https://msdn.microsoft.com/en-us/library 
 Connectoin String : http://www.connectionstrings.com/access/

Tuesday, July 21, 2015

Visual Studio 2013 ~ C# and Microsoft Access | Add, Edit and Delete Functionality



I am refreshing my knowledge in C# and only to find out that the new Visual Studio is way too far from Visual Studio I used to work on during my college days.

Using Visual Studio 2013, and MS Access 2010, I created a 'Manage Customer Details' form where I can ADD, UPDATE and DELETE customer profile in / from the database.

Before I start my code snippets, here's the quick demo of the application.

Demo:



I'll gonna post some snippets later on this page in a few.