Tuesday, April 16, 2019

Track error in sql server procedures


1. Create a ErrorLog Table

  CREATE TABLE [dbo].[ErrorLog](
[ErrorLogId] [int] IDENTITY(1,1) NOT NULL,
[CreatedDate] [datetime] NULL,
[ProcedureName] [varchar](100) NULL,
[ErrorLine] [int] NULL,
[ErrorNumber] [int] NULL,
[ErrorMessage] [varchar](max) NULL,
[ErrorSeverity] [varchar](max) NULL,
[ErrorState] [int] NULL,
[ErrorProcedure] [varchar](100) NULL


2. Implement  Try and Catch

   BEGIN TRY 

    --  Sql Statements

 END TRY
 BEGIN CATCH
  Insert into ErrorLog(CreatedDate,ProcedureName,ErrorLine,ErrorNumber,ErrorMessage,ErrorState,ErrorProcedure)
  Values(GetDate(),'ProcedureName',ERROR_LINE(),ERROR_NUMBER(),ERROR_MESSAGE(),ERROR_STATE(),ERROR_PROCEDURE())
 END CATCH  

Friday, January 11, 2019

Send FCM Notification to Multiple Users based on Database Object in C#

 Process -1

         public void SendRequestNotification()
        {
            try
            {
                 DataBase obj = new DataBase();
                 var objData = obj.SelectUsersToSendNotification();
            if (objData.Count() > 0)
            {
                for (int i = 0; i < objData.Count(); i++)
                {
                var result = "-1";
                var webAddr = "https://fcm.googleapis.com/fcm/send";

                var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
                httpWebRequest.ContentType = "application/json";
                string serverKey = "XXXXXXXXXXXXXXXXXX";
                httpWebRequest.Headers.Add("Authorization:key=" + serverKey);
                httpWebRequest.Method = "POST";

                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                   string json =
 "{\"to\":\"" + objData.ToList()[i].DeviceId + "\"  ," + "\n" + //( DeviceId : Mobile/Equipment ID )

 "\"data\": {\"message\": \"Request Notification!\"," + "\n" +
              "\"Info1\":\"" + objData.ToList()[i].Info1 + "\" ," + "\n" +
              "\"Info2\": \"" + objData.ToList()[i].Info2 + "\" ," + "\n" +
              "\"Info3\": \"" + objData.ToList()[i].Info3 + "\" , }," +

"\"notification\": { \"body\" :\"" +objData.ToList()[i].Info3 + "\"," + "\n" +
                      "\"title\" : \"Request Notification\"," + "\n" +
                      "\"content_available\" : \"true\",}}"
                      
//   "content_available" : true for IOS
                    streamWriter.Write(json );
                    streamWriter.Flush();
                }

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    result = streamReader.ReadToEnd();
                }
            }
        }

                // return result;
            }
            catch (Exception ex)
            {
                //  Response.Write(ex.Message);
            }
        }

 Process -2

   
            try
            {


                 DataBase obj = new DataBase();
                 var objData = obj.SelectUsersToSendNotification(Id);
            if (objData.Count() > 0)
            {
                for (int i = 0; i < objData.Count(); i++)
                {

                    WebRequest tRequest =                 WebRequest.Create("https://fcm.googleapis.com/fcm/send");
                    tRequest.Method = "post";                 
                    tRequest.Headers.Add(string.Format("Authorization: key={0}", "XXXXXXXXXXXXXXXXXXXXXXX"));
                    tRequest.ContentType = "application/json";

                    var payload = new
                    {
                        to = objData.ToList()[i].DeviceID,
                        priority = "high",
                        content_available = true,

                        notification = new
                        {
                            sound = "default",
                            body = objData.ToList()[i].DeviceID,
                            title = "Request Notification",
                            content_available = true  // this is for IOS
                        },
                        data = new
                        {
                            message = "Request",
                            Info1 = objData.ToList()[i].Info1,
                            Info2 = objData.ToList()[i].Info2,
                        }
                    };

   

                    string postbody = JsonConvert.SerializeObject(payload).ToString();
                    Byte[] byteArray = Encoding.UTF8.GetBytes(postbody);
                    tRequest.ContentLength = byteArray.Length;
                    using (Stream dataStream = tRequest.GetRequestStream())
                    {
                        dataStream.Write(byteArray, 0, byteArray.Length);
                        using (WebResponse tResponse = tRequest.GetResponse())
                        {
                            using (Stream dataStreamResponse = tResponse.GetResponseStream())
                            {
                                if (dataStreamResponse != null) using (StreamReader tReader = new StreamReader(dataStreamResponse))
                                    {
                                        String sResponseFromServer = tReader.ReadToEnd();
                                        //result.Response = sResponseFromServer;
                                    }
                            }
                        }
                    }
                }
            }
            }
            catch (Exception ex)
            {
                //  Response.Write(ex.Message);
            }