VB.NET database SMS example

See how to add SMS functionality to your VB.NET application. For the solution you will need a database server that stores sent and received messages. MS SQL, MS SQL Express, MySQL, Access, Oracle is accepted. The source code is provided. You can download and edit it.

How to add SMS functionality to VB.NET (Rapid steps)

To add SMS functionality to VB.NET:

  1. Setup Ozeki SMS Gateway
  2. Test SMS sending
  3. Download Microsoft SQL Express
  4. Install Microsoft Visual Studio
  5. Create database user and tables
  6. Configure Database user in Ozeki
  7. Modify the VB.NET code
  8. Insert SMS into the database

Download: vb.net-sms-example-sql.zip (70 Kb)

Introduction

The solution consists of 3 different parts (Figure 1). Ozeki SMS Gateway, the SQL database and your VB.NET application. Check if Ozeki SMS Gateway is connected to the SQL database. You can find plenty of connection tutorials on the SMS from SQL database page. If it is done, please check if VB.NET can read and write database records.

send and receive sms messages with vb net
Figure 1 - Send and receive SMS messages with VB.NET

The figure shows exactly what was described in the last paragraph. You can send SMS messages by inserting a new record by using the VB.NET application. Check the SQL table to see if new records were placed into it. These messages should be automatically sent. Check if the VB.net application works with the database both by reading or inserting records. Reading records are important since this is the way to read incoming messages.

Prerequisites

The main software of the operation is Ozeki SMS Gateway which you can get to your Ozeki 10 application. This software can connect your computer to the mobile network as it can connect your PC to any of the following database server:

You will also need a C# or Visual Basic development IDE, such as Microsoft Visual Studio. If you haven't done yet, please download VB sms example project. To sum it up, here is a full list of the required software:

How to Install and Configure VB.NET

Use the following steps to create a working solution. In this example you will see a Microsoft SQL Express solution with database examples ready to copy-paste. Please download and install Ozeki 10 before going forward with these steps. Manually sms sending test if you can send and receive SMS message with Ozeki 10. If the test was successful, you will be able to send and receive SMS messages from the graphical user interface of the VB.net application. Check if it was sent by using the event logs of Ozeki 10.

If you are not using SQL Express, please copy the table creation code from one of these SQL server options.

Step 1 - Create database user and tables

Please install Microsoft SQL Express and Microsoft Visual Studio so you can follow these steps. Create the database tables on your Microsoft SQL Express database server by copy-pasting code 1.

create database ozeki
GO

use ozeki
GO

CREATE TABLE ozekimessagein (
 id int IDENTITY (1,1),
 sender varchar(30),
 receiver varchar(30),
 msg varchar(160),
 senttime varchar(100),
 receivedtime varchar(100),
 operator varchar(30),
 msgtype varchar(30),
 reference varchar(30),
);

CREATE TABLE ozekimessageout (
 id int IDENTITY (1,1),
 sender varchar(30),
 receiver varchar(30),
 msg varchar(160),
 senttime varchar(100),
 receivedtime varchar(100),
 operator varchar(100),
 msgtype varchar(30),
 reference varchar(30),
 status varchar(30),
 errormsg varchar(250)
);
GO
    
sp_addLogin 'ozekiuser', 'ozekipass'
GO
  
sp_addsrvrolemember 'ozekiuser', 'sysadmin'
GO

Code 1 - The code that creates the table structure

Step 2 - Connect Ozeki SMS Gateway to your database

Now it is time to create a Database user in Ozeki 10. After the user has been created select 'SQL server' and copy-paste the following connection string (Code 2). Do not forget to use your own Database name, user ID and password.

Server=.\SQLEXPRESS; Database=ozeki; UID=ozekiuser; PWD=ozekipass;

Code 2 - Connection string. Change parameters if necessary

Step 3 - Modify the VB.NET code to connect with Ozeki 10

Open example project in Visual Studio and edit source code if necessary. In code 3 you should provide the same connection parameters used above in code 2. The VB.NET parameters are added to 'myConnection' connection string (Code 3).


Dim dbUsername As String = "ozekiuser"
Dim dbPassword As String = "ozekipass"
Dim database As String = "ozeki"

Dim myConnection As _
New SqlConnection( _
"Server=.\SQLEXPRESS;User ID=" _
& dbUsername _
& ";password=" _
& dbPassword _
& ";Database=" _
& database _
& ";Persist Security Info=True")

Code 3 - Check if the connection parameters are correct

Step 4 - Insert SMS record into database

The following code inserts textbox content from the VB.NET example GUI to the database (Code 4).


Dim mySqlQuery As String = _ 
"INSERT INTO ozekimessageout " _
& "(receiver,msg,status) " _
& "VALUES ('" & tbReceiver.Text _
& "', '" & tbMessage.Text & "', 'send');"

Dim mySqlCommand As New _ 
SqlCommand(mySqlQuery, myConnection)

Code 4 - Inserts new SMS message record into the database

This code can only execute if the VB.NET application can connect to the database. The insertable values can be provided in the application GUI (Figure 2). After typing the recipient's number and message text, click 'Insert' and Ozeki 10's Database User will forward the inserted message to the recipient after finding the record in the database.

the user gui of this vb net example
Figure 2 - The user GUI of this VB.NET example

Finally the code sequence on the bottom of the class will run (Code 5). These 3 methods will be called. They will connect to the database and insert the message record of the SMS to send and close the connection.


myConnection.Open()

mySqlCommand.ExecuteNonQuery()

myConnection.Close()

Code 5 - Inserts message record

Full VB.NET example code

The full code you can see below (Code 6) is built up from segments described above (Code 3 - 5). You can freely use and modify the example code as you wish. The method can drop an exception if it cannot INSERT the SMS message to send.


Imports System
Imports System.Data
Imports System.Data.SqlClient

Public Class Form1

Private Sub bSend_Click( _ 
  ByVal sender As System.Object, _ 
  ByVal e As System.EventArgs)
    
  Handles bSend.Click
  Try
  Dim dbUsername As String = "ozekiuser"
  Dim dbPassword As String = "ozekipass"
  Dim database As String = "ozeki"
 	
  Dim myConnection As New SqlConnection( _
	    "Server=.\SQLEXPRESS;User ID=" _
		& dbUsername _
		& ";password=" _
		& dbPassword _
		& ";Database=" _
		& database _
		& ";Persist Security Info=True")

Dim mySqlQuery As String = "INSERT INTO " _
& "ozekimessageout(receiver,msg,status) " _
& "VALUES('" & tbReceiver.Text & "', '" _
& tbMessage.Text & "', 'send');"
	
  Dim mySqlCommand As New _ 
  SqlCommand(mySqlQuery, myConnection)
	
  myConnection.Open()
	
  mySqlCommand.ExecuteNonQuery()

  myConnection.Close()


  Catch ex As Exception
    MessageBox.Show(ex.Message)
  End Try
End Sub
End Class
Code 6 - The whole VB.NET example code

With this solution you won't have to worry about queuing outgoing messages, since the queue will be handled by Ozeki 10's SMS Gateway application. It will also ad a timestamp and modify the message status register attribute to show successful or unsuccessful delivery towards the recipient.

Incoming messages

They will be received and stored on the database server as well. Although the VB.NET application doesn't need to be online when receiving a message, since it can view the 'ozekimessagein' table anytime it goes online by running a SELECT statement over the table.

Summary

This article showed you how to add SMS managing functionality to your VB.NET application with the help of the Ozeki SMS Gateway. This solution helps you organize your messages with a database server that stores the SMS messages, and the VB.NET application that classifies these messages. If you have followed the steps above carefully, then you are now able to set up the presented system, which will make communication more convenient for you.

The information you have obtained is valuable, there are more useful documents on the Ozeki website, so keep on reading. Learn about the VB.NET HTTP SMS example in the next tutorial.

Do not waste your precious time, download the Ozeki SMS Gateway now!

More information