Ozeki NG SMS Gateway logo OZEKI NG SMS Gateway
"The World's most reliable SMS gateway software!"
info@ozekisms.com
Tel.: 00 36 52 532 731
SMS Gateway to send SMS via GSM modem or IP SMS connection Overview SMS Gateway to send SMS via GSM modem or IP SMS connection SMS Gateway to send SMS via GSM modem or IP SMS connection Quick start SMS Gateway to send SMS via GSM modem or IP SMS connection SMS Gateway to send SMS via GSM modem or IP SMS connection Download SMS Gateway to send SMS via GSM modem or IP SMS connection SMS Gateway to send SMS via GSM modem or IP SMS connection Manual SMS Gateway to send SMS via GSM modem or IP SMS connection SMS Gateway to send SMS via GSM modem or IP SMS connection How to buy SMS Gateway to send SMS via GSM modem or IP SMS connection SMS Gateway to send SMS via GSM modem or IP SMS connection FAQ SMS Gateway to send SMS via GSM modem or IP SMS connection SMS Gateway to send SMS via GSM modem or IP SMS connection Contact Us SMS Gateway to send SMS via GSM modem or IP SMS connection
OZEKI NG SMS Gateway - Product Guide

SMS API functions Contents | SearchData field change

Home > Product Manual > Developers Guide > C Sharp SMS API > C sharp and MS SQL Server

SMS Gateway SMS Gateway Home

  Product Manual
  Introduction
  SMS technology
  Installation Guide
  User Guide
  Developers Guide
  Tutorials
  ASP SMS API
  PHP SMS API
  HTTP SMS API
  SQL SMS API
  C Sharp SMS API
  SMS API functions
  C sharp and MS SQL Server
  Data field change
  Run the example
  Registering remote objects
  C Sharp SMS Gateway
  AJAX SMS API
  Delphi SMS API
  Cpp SMS API
  Java SMS API
  VB.NET SMS API
  Python SMS API
  Perl SMS API
  TCL/TK SMS API
  Coldfusion SMS API
  VB 6 SMS API
  Examples and Solutions
  Appendix
  FAQ
  Feature list
  Commercial Information
  Search
  Cookie policy
 


Automated phone calls?
Ozeki Phone System XE VoIP PBX software is an advanced PBX built for automated voice calls and 2 way SMS messaging. It has outstanding APIs for software developers. It can be used for:

Phone calls from SQL
Phone calls from HTTP
Voice and SMS applications

Callcenter developers
If you are working on telephone solutions, please check out the Ozeki VoIP SIP SDK.
It can be used to create VoIP client and server software.

Contact Us!
If you wish to get further information, do not hesitate to contact us!

E-mail: info@ozekisms.com

If you have a technical question, please submit a support request on-line.

Sending SMS using C#.NET through MS SQL Server

Download source code

download full project (54.1 kB)

Introduction

This article is about sending SMS using C#.NET through MS SQL Server. It is intended for web developers with a basic knowledge of the C#.NET and SQL technologies. The reader should be familiar with Microsoft Visual Studio and the C# programming language. The page contains a downloadable source code (see above), a list of aims you can achieve using the code, a list of prerequisites, a description and depiction of the architecture and operation of the system, step-by-step instructions about how to use the code and a detailed interpretation. It also includes a brief summary, answers to frequently asked questions, as well as links to related information.

When to use this code

This code is particularly useful for those who wish to

  • add SMS functionality to an application.
  • create automated SMS notifications.
  • increase application security by adding SMS login.

Prerequisites

The following table lists the software requirements of the system. Note that you can download Ozeki NG - SMS Gateway and .NET Framework 3.5 on the download page.

Operating system: Windows XP, Windows 2003, Windows Vista, Windows 2008, Windows 2008 R2, Windows 7, Windows 2012 R2, windows 2016, Windows 8 or Windows 10
Other software requirements: .NET Framework 4.0
Ozeki NG-SMS gateway
Microsoft SQL Server 2005 (Express Edition) or newer Microsoft SQL Server
Development platform: Microsoft Visual Studio .NET

How it works

To send SMS messages from C#.NET applications, you need to install Ozeki NG - SMS Gateway and Microsoft SQL Server on your computer. Ozeki NG - SMS Gateway will use a GSM phone/modem attached to your PC (with a phone-to-PC data cable) or an IP SMS connection to send messages. Your C#.NET application will insert a row into the database to send messages using the Ozeki NG program. For a better understanding of how it works, please look at the following diagram (Figure 1).



Figure 1 - Sending SMS from an C#.NET application

In the diagram you can see a user with the C#.NET application, an SQL Server, Ozeki NG - SMS Gateway, a mobile phone attached to the server computer and a mobile user receiving the message. Wherever the user is, if their C# application is appropriately configured (the configuration requires information related to the MS SQL server), they can compose and send messages to any recipients. After the user's action, the C#.NET application inserts a row with the data of the SMS to be sent, which will be selected by Ozeki NG - SMS Gateway from the database. Ozeki NG will forward the message to the GSM network through a mobile phone attached to the PC with a data cable, and the mobile user will receive the SMS message.

Configure the MS SQL Server

Step 1: To configure Microsoft SQL Server 2005, first install SQL Server Management Studio Express.
Step 2: Next, start SQL Server Management Studio Express and log in.
Step 3: If you would like to use a username and password to log in, you have to set the authentication mode. Picture help
Step 4: Now create the user and the database with tables. Follow this picture help.

Create a Database user in Ozeki NG - SMS Gateway

Step 1: Open Ozeki NG's graphical user interface (Start menu -> All Programs -> OzekiNG -> Manage OzekiNG). Picture help
Step 2: Create the database user in Ozeki NG with the necessary data following the instructions.

Using the code

To use the downloaded C# code, follow these steps:

Step 1: Unpack the downloaded zip file.
Step 2: Set/Change the fixed data in the DatabaseHandling.cs file (e.g., server name, user name, password). Picture help
Step 4: Start the Ozeki NG - SMS Gateway Server (if not running). Picture help
Step 5: Build and run it by MS Visual Studio .NET
Step 6: Click the Compose message button, fill in the necessary fields, and click the "Send" button. Picture help

Understanding the code

The downloadable code inserts the message to be sent into the ozekimessageout database table. Ozeki NG - SMS Gateway periodically checks the table, and if it finds a row where the status is send, it will try to send it. In the script you have to set the server name to where the MS SQL Server is running, the username (specifying who can log in to the MS SQL Server) with the password and the type of the SMS message.

The source code of the example application is structured in the following way:

MainForm.cs:
Shows and handles a "Compose message" button and 2 ListView items for incoming and outgoing messages.

ComposeMessageForm.cs:
Shows and handles the necessary data fields for creating a new message.

DatabaseHandling.cs:
Handles the database and refreshes the 2 ListView items.

Description of the process depicted in Figure 1 above:

Step 1: Creating the form
Through MainForm.cs in the ComposeMessageForm.cs file, you create the form that requests the sms data. The User fills in the necessaries. The user will be asked to fill in the Recipient and Message text fields. A pop-up messagebox is used to inform the User about error(s) or result(s) of sending the message.
In MainForm.cs file you set the 2 ListView items (view mode and names of columns). In the buttonComposeMessage_Click(...) procedure the ComposeMessageForm is called to compose a new message. After closing that box the 2 ListView items will be refreshed.

MainForm.cs
...
public MainForm()
{
    InitializeComponent();

    listViewOut.View = View.Details;
    listViewOut.ShowItemToolTips = true;
    listViewOut.Columns.Add("ID", 50);
    listViewOut.Columns.Add("Sender", 100);
    listViewOut.Columns.Add("Receiver", 100);
    listViewOut.Columns.Add("Sent time", 100);
    listViewOut.Columns.Add("Received time", 100);
    listViewOut.Columns.Add("Operator", 50);
    listViewOut.Columns.Add("Status", 50);
    listViewOut.Columns.Add("Message type", 50);
    listViewOut.Columns.Add("Message text", 100);

    listViewIn.View = View.Details;
    listViewIn.ShowItemToolTips = true;
    listViewIn.Columns.Add("ID", 50);
    listViewIn.Columns.Add("Sender", 100);
    listViewIn.Columns.Add("Receiver", 100);
    listViewIn.Columns.Add("Sent time", 100);
    listViewIn.Columns.Add("Received time", 100);
    listViewIn.Columns.Add("Operator", 50);
    listViewIn.Columns.Add("Message type", 50);
    listViewIn.Columns.Add("Message text", 100);
}

private void buttonComposeMessage_Click(object sender, EventArgs e)
{
    ComposeMessageForm cmf = new ComposeMessageForm();
    cmf.ShowDialog(this);
    updateOutgoingMessages();
    updateIncomingMessages();
}
...
            

Step 2: Processing data coming from the form
After filling in the fields and clicking the Send button, the application receives the information about the form. In the ComposeMessageForm.cs file, the CheckAndSendMessage() procedure is called by "buttonSend_Click(...)". At the beginning of the procedure, check the data of the textbox fields. The Recipient box is mandatory. If it is empty, the processing will be aborted, and the User will be informed about the error. If the checking is successful, you will insert a new row into the ozekimessageout database table (in DatabaseHandling.insertMessage(...)).
We need the following: the address of the computer running the Microsoft SQL Server [the default address is (local)\SQLExpress ((local) means that MS SQL Server is installed on the same computer on which the C#.NET application is running)], the username (who is authorized to log in to the MS SQL Server and to insert messages, the user's password, the message type (the default is SMS:TEXT), the recipient and the message data.

ComposeMessageForm.cs
...
private void buttonSend_Click(object sender, EventArgs e)
{
    CheckAndSendMessage();
}

private void CheckAndSendMessage()
{
    if (textBoxRecipient.Text == "")
    {
        MessageBox.Show("Recipient field mustn't be empty!",
                    "Incorrect field value");
        return;
    }

    string errorMsg = "";
    DatabaseHandling.insertMessage(textBoxRecipient.Text,
                textBoxMessageText.Text, out errorMsg);

    MessageBox.Show(errorMsg, "Result of inserting message");
}
...
            



DatabaseHandling.cs
...
public static void insertMessage(string receiver, string messageText,
            out string errorMsg)
{
    Connect(out errorMsg);
    if (errorMsg != "")
       return;

    try
    {
       SqlCommand sqlComm = sqlConn.CreateCommand();
       sqlComm.CommandText = "insert into ozekimessageout " +
                    "(msgtype,receiver,msg,status) " +
                    "values ('SMS:TEXT','" + receiver + "','" +
                    messageText + "','send');";
       if (sqlComm.ExecuteNonQuery() == 0)
       {
            errorMsg = "Insert was UNsuccessful!";
       }
       else
       {
            errorMsg = "Insert was successful!";
       }
    }
    catch (Exception e)
    {
        errorMsg = e.Message;
    }

    CloseConnection();
}
...
            


Step 3: Checking the incoming and the outgoing message table
By refreshing the table of incoming and outgoing messages, you can check the incoming messages and the status of outgoing messages. Look at the updateListViewOut() procedure. First, you clear its rows. Then, you create the connection. If it is successful, you will refill the outgoing messages table.

DatabaseHandling.cs
...
public static void updateListViewOut(ListView lvOut)
{
    string errorMsg = "";
    Connect(out errorMsg);
    if (errorMsg != "")
        return;

    lvOut.Items.Clear();

    try
    {
        SqlCommand sqlComm = sqlConn.CreateCommand();
        sqlComm.CommandText = "select id,sender,receiver,msg,senttime," +
            "receivedtime,operator,msgtype,status from ozekimessageout;";

        SqlDataReader dr = sqlComm.ExecuteReader();
        if (dr != null)
        {
            while (dr.Read())
            {
                string tmpStr = "";
                tmpStr = (dr.IsDBNull(0)) ? ("") : (dr.GetInt32(0).ToString()); //id
                ListViewItem lvi = new ListViewItem(tmpStr);
                tmpStr = (dr.IsDBNull(1)) ? ("") : (dr.GetString(1)); //sender
                lvi.SubItems.Add(tmpStr);
                tmpStr = (dr.IsDBNull(2)) ? ("") : (dr.GetString(2)); //receiver
                lvi.SubItems.Add(tmpStr);
                tmpStr = (dr.IsDBNull(4)) ? ("") : (dr.GetString(4)); //senttime
                lvi.SubItems.Add(tmpStr);
                tmpStr = (dr.IsDBNull(5)) ? ("") : (dr.GetString(5)); //received time
                lvi.SubItems.Add(tmpStr);
                tmpStr = (dr.IsDBNull(6)) ? ("") : (dr.GetString(6)); //operator
                lvi.SubItems.Add(tmpStr);
                tmpStr = (dr.IsDBNull(8)) ? ("") : (dr.GetString(8)); //status
                lvi.SubItems.Add(tmpStr);
                tmpStr = (dr.IsDBNull(7)) ? ("") : (dr.GetString(7)); //msgtype
                lvi.SubItems.Add(tmpStr);
                tmpStr = (dr.IsDBNull(3)) ? ("") : (dr.GetString(3)); //msg
                lvi.SubItems.Add(tmpStr);
                lvOut.Items.Add(lvi);
            }
            dr.Close();
        }

        sqlConn.Close(); //we close the SQL connection
    }
    catch (Exception)
    {

    }

    CloseConnection();
}
...
            

Summary

In the article it has been explained how you can add SMS functionality to your website by using the downloadable C#.NET example code. You can freely use and modify it. Using a C#.NET application (and the Ozeki NG - SMS Gateway Server) you can create a lot of useful services (e.g.: you can add SMS functionality to an application, create automated SMS notifications, increase application security by adding SMS login, etc.).

Frequently asked questions

Question: How can I send different message types?
Answer: For example, a Wap push message:
   
...
sqlComm.CommandText = "insert into ozekimessageout " +
                    "(msgtype,receiver,msg,status) " +
                    "values ('SMS:WAPPUSH','" + receiver + "','" +
                    messageText + "','send');";
...
messageText format will be the following:
<si>
<indication href="http://target_address" action="signal-high">
text of description
</indication>
</si>

The action can be one of the following: signal-high, signal-medium, signal-low, signal-none, signal-delete.

Question: Can I run the C# application on a computer different from the one running the Ozeki NG - SMS Gateway Server and the MS SQL Server?
Answer: Yes, you can. In the application code you have to set the Server in the "connectionString" of the computer running the MS SQL Server.

Question: Can I set the phone number so that the recipient can see where the sms comes from?
Answer: Yes, you can. In the code you have to set the sender field too. It works only if you have IP SMS connection.
   
...
string sender = "+449876543";
sqlComm.CommandText = "insert into ozekimessageout " +
                    "(msgtype,sender,receiver,msg,status) " +
                    "values ('SMS:TEXT','" + sender + "','" + receiver + "','" +
                    messageText + "','send');";
...
            

Dig deeper!
People who read this also read...


Data field change
Run the example
Ozeki NG Installation Guide
SMS Message Types
RS232 Serial Cable and Industrial Modems
IP SMS Gateway
Supported phones and GSM devices
Message statuses in database
Database table layout
FAQ



Next page: Data field change
Copyright © 2000 - 2017 Ozeki Informatics Ltd.
All rights reserved

Software PBX for Windows | VoIP SDK   |   Legal information   |   Privacy policy   |   Terms of use
Please, address your inquiries to info@ozekisms.com