using System; using System.Configuration; using System.Net; using System.Text; using System.Web.Script.Serialization; public partial class WorkshopSchedule : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnSubmit_Click(object sender, EventArgs e) { string username = txtUsername.Text.Trim(); if (string.IsNullOrEmpty(username)) { // Simple alert for empty input ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Please enter a valid Reg No or Student ID');", true); return; } // 1. Prepare UI state InputPanel.Visible = false; LoadingPanel.Visible = true; ResultPanel.Visible = false; // Hide previous results try { // 2. Prepare API Data string clientId = ConfigurationManager.AppSettings["ClientId"]; string clientKey = ConfigurationManager.AppSettings["ClientKey"]; var requestData = new { username = username, client_id = clientId, client_key = clientKey }; JavaScriptSerializer js = new JavaScriptSerializer(); string jsonRequest = js.Serialize(requestData); // 3. Call API using (WebClient client = new WebClient()) { client.Headers[HttpRequestHeader.ContentType] = "application/json"; string responseString = client.UploadString("https://amcr.aiou.edu.pk/api/get_schedule3", jsonRequest); var data = js.Deserialize(responseString); // 4. Check for No Record Found if (data == null || data.user_info == null || data.workshop_info == null || data.workshop_info.Length == 0) { ShowError("No workshop schedule found for the entered ID/Reg No. Please verify your input."); return; } // 5. Display Results lblStudentName.Text = data.user_info.ST_NAME; lblStudentID.Text = data.user_info.ROLLNO; gvWorkshop.DataSource = data.workshop_info; gvWorkshop.DataBind(); ResultPanel.Visible = true; } } catch (WebException webEx) { // Handle specific web request errors (e.g., API server down) ShowError("Connection Error: Could not retrieve data from the server. Please try again later."); } catch (Exception ex) { // Handle other general errors ShowError("An unexpected error occurred: " + ex.Message); } finally { // 6. Final UI State LoadingPanel.Visible = false; } } private void ShowError(string message) { // Replace single quotes to prevent JavaScript injection/syntax errors string safeMessage = message.Replace("'", "\\'"); // Use $"" for cleaner string formatting string script = $@" document.getElementById('errorText').innerText='{safeMessage}'; var myModal = new bootstrap.Modal(document.getElementById('errorModal')); myModal.show();"; // Use a unique key for the script ClientScript.RegisterStartupScript(this.GetType(), "errorModalScript", script, true); // Reset display to the input panel InputPanel.Visible = true; LoadingPanel.Visible = false; ResultPanel.Visible = false; } } // Classes for API response public class ApiResponse { public UserInfo user_info { get; set; } public WorkshopInfo[] workshop_info { get; set; } } public class UserInfo { public string ST_NAME { get; set; } public string ROLLNO { get; set; } } public class WorkshopInfo { public string Course_code { get; set; } public string Group { get; set; } public string Start_Date { get; set; } public string End_Date { get; set; } }