Friday, December 30, 2022

Bootstrap Modal popup for Error Show or hide - OnAsyncPostBackError ASP.NET C# - Show and hide modal popup javascript

   <!-- Modal -->

          <div class="modal fade" id="myModal" role="dialog">

            <div class="modal-dialog modal-sm">

              <div class="modal-content">

                <div class="modal-header">

                  <button type="button" class="close" data-dismiss="modal">&times;</button>

                  <h4 class="modal-title">Error</h4>

                </div>

                <div class="modal-body">

                  <p>This is a small modal.</p>

                </div>

                <div class="modal-footer">

                  <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>

                </div>

              </div>

            </div>

          </div>


javascript


$('.modal-body').html('Enter body');

               $('#myModal').modal('show');

//$('#myModal').modal({ show: false});


reference:- https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_modal_lg&stacked=h

Thursday, December 29, 2022

How to send null integer or string values to database SQL ASP.NET C#

 There are two ways

either you can send null value from C#


 public static bool UpdateOpenVacancy(int id, string cadreName, int? cadreID, string instituteName, int? instituteID, string disciplineName, int? disciplineID, int? sanctionedStrength, int? inPosition, int? vacancy, int openVacancy)

        {

            SqlParameter[] param = {

                new SqlParameter("@id", id),

                new SqlParameter("@cadreName", cadreName),

                new SqlParameter("@cadreId",cadreID),

                new SqlParameter("@instituteName", instituteName),

                new SqlParameter("@instituteId", instituteID==null?Convert.DBNull:instituteID),

                new SqlParameter("@disciplineName", disciplineName),

            };

using Convert.DBNull


or in stored procedure pass null value to parameters

ALTER PROCEDURE [dbo].[UpdateOpenVacancy]

@id int,

@cadreName VARCHAR(100),

@cadreID INT = null,

@instituteName VARCHAR(100) = null,


END

or 

int? counts1 = string.IsNullOrEmpty(yarncounts.Text) ? (int?)null : int.Parse(yarncounts.Text);


Wednesday, December 28, 2022

How to use scriptmanager master page to content page(other page)- ASPX C#

 System.Web.UI.ScriptManager.GetCurrent(this.Page);


Exception handling update panel script manager - ASP.NET C# - onAsyncPostBackError

 catch (Exception ex)
    {
        string message = string.Format("Message: {0}\\n\\n", ex.Message);
        message += string.Format("StackTrace: {0}\\n\\n", ex.StackTrace.Replace(Environment.NewLine, string.Empty));
        message += string.Format("Source: {0}\\n\\n", ex.Source.Replace(Environment.NewLine, string.Empty));
        message += string.Format("TargetSite: {0}", ex.TargetSite.ToString().Replace(Environment.NewLine, string.Empty));
        ClientScript.RegisterStartupScript(this.GetType(), "alert""alert(\"" + message + "\");"true);
    }


 reference:- https://www.aspsnippets.com/Articles/Display-Exception-Error-Message-details-using-JavaScript-Alert-MessageBox-in-ASPNet.aspx


or


<script type="text/javascript">

              //function pageLoad() {

       //    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

       //}

OrderBy LINQ ASP.NET C#



 dtOpenVacancy.AsEnumerable().OrderByDescending(a => a.Field<int>("id"));


IF WANT TO USE WITH GRID DATABASE:-

dtOpenVacancy.AsEnumerable().OrderByDescending(a => a.Field<int>("id")).CopyToDataTable()


or


gridview

private void BindOpenVacancy()

        {

            DataTable dtOpenVacancy = Repository.GetAllOpenVacancy();


            if (dtOpenVacancy.Rows.Count > 0)

            {

                ViewState["vwOpenVacancy"] = dtOpenVacancy.AsEnumerable().OrderByDescending(a => a.Field<int>("id")).CopyToDataTable();


                grdOpenVacancy.DataSource = dtOpenVacancy.AsEnumerable().OrderByDescending(a => a.Field<int>("id")).CopyToDataTable(); 

            }

            grdOpenVacancy.DataBind();

        }


reference:- https://www.tutorialsteacher.com/linq/linq-sorting-operators-orderby-orderbydescending

Script manager OnAsyncPostBackError ASP.NET C# - Error handling in Scriptmanager

 If you are not using scriptmanager in master page, and try and catch is not used in the page,

than you can use OnAsyncPostBackError event in Webpage.

if you are using scriptmanger in webpage without master pager, than you can easily use OnAsyncPostBackError , but you don't have to use try and catch


<asp:ScriptManager runat="server" ID="ScriptManager" 

EnablePartialRendering="true" OnAsyncPostBackError="OnAsyncPostBackError">

  </asp:ScriptManager>


references:- http://etutorials.org/Programming/aspnet+ajax/Chapter+5+Using+the+ScriptManager/Error+Handling/

https://stackoverflow.com/questions/5944481/error-handling-in-updatepanel-with-customerrors-mode-on

https://www.oreilly.com/library/view/aspnet-ajax-updatepanel/0596527470/ar01s06.html

To prevent After saving on button click on refresh button saving data - ASP.NET C#

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
     <ContentTemplate>            
           <asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />
           <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />      
     </ContentTemplate>
</asp:UpdatePanel>


reference:-  https://www.aspsnippets.com/questions/184293/Prevent-Page-refresh-on-Button-Click-in-ASPNet/