0

So i have a stored procedure that i would like to add to a label.. How do i do this please and what even do i need to strike ... i.e

 protected void label_DataBinding(object sender, EventArgs e)
        {

        }

I have my data being pulled from a class ..

Data class

 public DataSet TotalPacked(int itemSeriesMasterId, DateTime shiftstart, DateTime shiftend)
        {
            object[] args = new object[3] {itemSeriesMasterId, shiftstart, shiftend};
            return CallSp(MethodBase.GetCurrentMethod(), args) as DataSet;
        }
    }
}

Biz Class

 public DataTable GetTotalPacked(DateTime shiftStart, DateTime shiftEnd, int seriesMasterId)
        {
            using (DataManager dmgr = new DataManager())
            {
                dmgr.Connect(ConfigurationManager.AppSettings["ProductionKey"]);
                DataSet dset = dmgr.TotalPacked(seriesMasterId,shiftStart,shiftEnd);
                return CreatePackingStats(dset);

So how do i call the stored proc from the label? any help would be much appreciated.

8
  • Why would you need to call a Stored procedure from the label? Commented May 30, 2018 at 9:25
  • because i need to show updated figures every 30 seconds called from the database... Commented May 30, 2018 at 9:28
  • if you want to display the data only to the label then it does not need to be called from the label. An ajax request with setInterval() is a better approach for that. Commented May 30, 2018 at 9:33
  • I was told to do it this way by my manager, i am a trainee you see lol. I dont know what i am doing lol Commented May 30, 2018 at 9:39
  • you can take a look at this Link it might help in what you are trying to do. Commented May 30, 2018 at 9:52

1 Answer 1

0

So, in order for a label to have data refreshed in every couple of seconds you can try that by creating WebMethod to get the data and then call that particular WebMethod via a setInterval() from javascript.

For More info about WebMethods WebMethods via Web Service & Paramterized WebMethods

Code sample:

So, for example, I have a method called GetTime() from which I have to fetch DateTime and display that on to label in my .aspx page with a span of every 5 seconds. In order to do that, I have to call the WebMethods from JavaScript(client side). So first, we add a ScriptManager(this you can either drag from the toolbar or can simply write) on your either calling page(.aspx) or on your master page(if you need it in other pages) with an option of EnablePageMethods="true".

ScriptManager

<asp:ScriptManager ID="scriptManager1" EnablePageMethods="true" runat="server"></asp:ScriptManager>

Server Side:

    [System.Web.Services.WebMethod]
    public static string GetTime()
    {
        return System.DateTime.Now.ToString();
    }

And to bind it on the client side i have the label and have set the property of the lable ClientIdMode="Static" so, we can easily get the control in javascript:

<asp:Label runat="server" ID="lbltime" ClientIDMode="Static">time will be displayed here</asp:Label>

and here is the javascript function to call the Webmethod:

function getTime()
{
  PageMethods.GetTime(ongetTimePass, ongetTimefail);            
}

The PageMethods takes the methodname along with the parameters and the onSuccess() and onFailure function with an syntax as: PageMethods.<WebMethodName>(Paramter1,Paramter2....,OnSucess(),OnFailure()).

If you happen to have JQuery library attached to your project you can call the function via ajax as:

function getTime()
    {
        $.ajax({
            type: "POST",
            url: "default.aspx/GetTime",
            data: '',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: ongetTimePass,
            failure: ongetTimefail
        });

    }

And here is the ongetTimePass() and the ongetTimefail() function:

 function ongetTimePass(result)
 {
     //here we pass the result if the method has a success response.            
     document.getElementById('lbltime').innerHTML = result;
 }

 function ongetTimefail(error)
 {
     alert(error);
 }

And now to call it, we add it to the load() event of the page with an [setInterval()] function(https://www.w3schools.com/jsref/met_win_setinterval.asp):

window.onload = function () {
        var st = setInterval(getTime, 1000);
    };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.