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);
            }



Thursday, August 6, 2015

Populate Checkboxes of gridview checked or unchecked based on Database value



         Suppose you have a column in in your table as "SelectedFlag" as char datatype. And you are storing 'Y' or 'N' as per your requirement.  Now you have to modify your select statement like



"select  col1, col2, cast( (case when SelectedFlag='Y' then

                                 when SelectedFlag = 'N' then 0 end) as bit) as SelectedFlag from Table1"


 in GridView


  <asp:TemplateField HeaderText="Select">
     <ItemTemplate>
         <asp:CheckBox ID="ChkSelect" runat="server" Checked='<%# Eval("SelectedFlag") %>' />
     </ItemTemplate>
  </asp:TemplateField>

It will automatically check/uncheck your checkbox inside girdview based on your database.



Thursday, July 9, 2015

How to hide Grid View column in ASP.net




protected void gvAppList_DataBound (object sender, EventArgs e)
        {
             GvDetails.Columns[1].Visible = false;

        }

Thursday, May 15, 2014

Javascript Confirmation message on DropDownList Selected Index Changed Event


 Suppose a  user entering records in fields and before save, changed the selected DropDownList suddenly there should be a message . Otherwise the values will be lose those are loaded under   DropDownList selected index change event.I took a another textbox to hold the previous selected index value of  DropDownList .(Also we can take HiddenField)


     protected void DDlPo_SelectedIndexChanged(object sender, EventArgs e)
        {
            DDlPo.Attributes.Add("onchange", "return OnSelectedIndexChange();");
            LoadMaterialBatchDetails();//this is load method

         }





   <script language="javascript" type="text/javascript">
            function OnSelectedIndexChange() {
                  var IndexValue = document.getElementById('DDlPo');               
                  var PreviousIndexValue = document.getElementById('Txt PreviousIndexValue ').value;
                  if (IndexValue.selectedIndex != PreviousIndexValue) {
                      if (confirm("Are you sure to select another Order Number, Data will be loss for this Order Number?") == true) {
                          document.getElementById('BtnClose').click();
                          IndexValue.selectedIndex = IndexValue.selectedIndex;
                      }
                      else {
                          IndexValue.selectedIndex = PreviousIndexValue;
                      }
                }
               
            }

        </script>