Read from SQL and Send SMS

Post Reply
ammar
Posts: 6
Joined: Thu Jan 02, 2020 4:08 pm

Read from SQL and Send SMS

Post by ammar » Sat Oct 17, 2020 1:42 pm

Hello
When I read message from SQL and want to send them via SMPP, I got an error. But when replace parameter with static text, work normally.

Code: Select all

      public static ConcurrentQueue<string> iQ;  

      public static async Task<int> RunTest(SmppClient client)
        {
            int count = 0;
            DataTable dataTable = new DataTable();
            try
            {
                using (SqlConnection sqlConnection = new SqlConnection(@"connection string"))
                {
                    using (SqlCommand sqlCommand = new SqlCommand("sp_getData", sqlConnection))
                    {
                        sqlCommand.CommandType = CommandType.StoredProcedure;
                        sqlCommand.Parameters.AddWithValue("@params1", 1);
                        sqlCommand.Parameters.AddWithValue("@params2", 1000);
                        sqlConnection.Open();
                        dataTable.Load(sqlCommand.ExecuteReader());
                    }
                }
                count = dataTable.Rows.Count;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            Stopwatch watch = Stopwatch.StartNew();
            foreach (DataRow dtRow in dataTable.Rows)
            {
                 await SubmitSingleMessage(dtRow["From"].ToString(), dtRow["To"].ToString(), dtRow["[Text"].ToString(), client, int.Parse(dtRow["ID"].ToString()));
            }
            watch.Stop();
            return   mps = Convert.ToInt32(count / watch.Elapsed.TotalSeconds);
        }

        private static async Task SubmitSingleMessage(string from, string to, string text, SmppClient client, int messageID)
        {
            ISubmitSmBuilder builder = SMS.ForSubmit()
                .From(from)
                .To(to)
                .Coding((DataCodings)Enum.Parse(typeof(DataCodings), text))
                .Text(text);

            try
            {
                IList<SubmitSmResp> resp = await client.SubmitAsync(builder);

                if (resp.All(x => x.Header.Status == CommandStatus.ESME_ROK))
                {
                    var returns = string.Join(",", resp.Select(x => x.MessageId));
                    using (var output = new StringWriter())
                    {
                        JSON.Serialize(
                            new
                            {
                                messageID = messageID,
                                returnIds = returns.Split(',').Select(int.Parse).ToList(),
                                totalPart = returns.Split(',').Select(int.Parse).ToList().Count
                            },
                            output
                        );
                        iQ.Enqueue(output.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                //_log.Error("Submit failed. Error: {0}", ex.Message);
            }
       }
    }
    public class resultSMS
    {
        [JilDirective(Name = "messageID", IsUnion = true)]
        public int messageID { get; set; }

        [JilDirective(Name = "returnIds", IsUnion = true)]
        public List<int> returnIds { get; set; }

        [JilDirective(Name = "totalPart", IsUnion = true)]
        public int totalPart { get; set; }
    }
    
    
alt
Site Admin
Posts: 851
Joined: Tue Apr 25, 2006 9:45 am

Re: Read from SQL and Send SMS

Post by alt » Mon Oct 19, 2020 8:38 pm

Hello,
What error do you get? Which parameter do you mean?

I see one mistake in your code. Probably you should write
dtRow["Text"].ToString()
insead of
dtRow["[Text"].ToString()
ammar
Posts: 6
Joined: Thu Jan 02, 2020 4:08 pm

Re: Read from SQL and Send SMS

Post by ammar » Tue Oct 20, 2020 11:55 am

Hi,
I means when I want get Datacodding from Text by using this method

Code: Select all

.Coding((DataCodings)Enum.Parse(typeof(DataCodings), text))
I got error. But when I use (for example)

Code: Select all

DataCodings.UCS2
that work correctly
alt
Site Admin
Posts: 851
Joined: Tue Apr 25, 2006 9:45 am

Re: Read from SQL and Send SMS

Post by alt » Tue Oct 20, 2020 8:32 pm

what value contains the "text" variable?
ammar
Posts: 6
Joined: Thu Jan 02, 2020 4:08 pm

Re: Read from SQL and Send SMS

Post by ammar » Wed Oct 21, 2020 7:05 am

Text of message
Like "Hello" or can be anything
alt
Site Admin
Posts: 851
Joined: Tue Apr 25, 2006 9:45 am

Re: Read from SQL and Send SMS

Post by alt » Wed Oct 21, 2020 4:16 pm

you are using "text" parameter for message text and data coding, it is incorrect. You should introduce another variable/parameter for data coding.

if you want to convert any string variable to DataCodings enum, this variable should take one of the name listed on the page https://docs.inetlab.com/smpp/v2.8/api/ ... dings.html
ammar
Posts: 6
Joined: Thu Jan 02, 2020 4:08 pm

Re: Read from SQL and Send SMS

Post by ammar » Wed Oct 21, 2020 7:42 pm

How I can get DataCoding from text. For example:
My sms text is: "Hello" or "سلام"
I want know how I can get datacoding for sms text?
Post Reply