2017-08-07 55 views
-2

首先另一個對象的字符串值露面過,這裏是我的代碼:如何獲得一個非彈出警報基於在C#/ asp.net

private void CheckPolicy(string PolicyNumber, string PolicyMod) 
    { //Check to see if the policy mod combo is a valid policy mod 
     SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Conn_DataDelivery"].ConnectionString); 

     string strPolicyStatus = ""; 

     try 
     { 
      conn.Open(); 
      SqlCommand cmd = conn.CreateCommand(); 

      cmd.CommandType = CommandType.StoredProcedure; 
      cmd.CommandText = "Advantage.GetAdvantageEligibilityPolicyCheck"; 
      SqlParameter inparm1 = cmd.Parameters.Add("@PolicyNumber", SqlDbType.VarChar); 
      inparm1.Direction = ParameterDirection.Input; 
      inparm1.Value = "%" + PolicyNumber;//Pass a wildcard with the policy Number 

      SqlParameter inparm2 = cmd.Parameters.Add("@PolicyMod", SqlDbType.NVarChar); 
      inparm2.Direction = ParameterDirection.Input; 
      inparm2.Value = PolicyMod; 

      SqlDataReader rdr = cmd.ExecuteReader(); 
      while (rdr.Read()) 
      { 

       if (rdr["PolicyStatus"] != DBNull.Value) 
       { 
        strPolicyStatus = rdr["PolicyStatus"].ToString();       

        switch (strPolicyStatus) 
        { 
         case "Policy Does Not Exist": 
          lblresult.Text = strPolicyStatus.ToString(); 
          break; 
         case "Policy Exist": 
          SaveExceptionData(PolicyNumber, PolicyMod, 1, 0, Environment.UserName); //Need to add the user Information 
          break; 
         case "Duplicate Policy": 
          lblresult.Text = strPolicyStatus.ToString(); 
          break; 
         default: 
          lblresult.Text = "An error occurred, please try again."; 
          break; 
        } 
       } 
       else 
       { } 
      }     
      rdr.Close(); 
     } 
     catch (SqlException ex) 
     { 
      lblresult.Text = ex.ToString(); 
     } 
     finally 
     { 
      conn.Close(); 
     } 
    } 

    private void SaveExceptionData(string PolicyNumber, string PolicyMod, int AdvantagePolicyIndicator, int isDeleted, string UserName) 
    { 

     //string Name = Environment.UserName; 

    SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Conn_DataDelivery"].ConnectionString); 
     void GetPolicyCheck(ref string strPolicyStatus) 
     { 
      strPolicyStatus = ""; 
     } 

     bool valb = vala; 
     if (valb) 
     { 
      Console.WriteLine("Save Was successful"); 
     } 
     valb = !valb; 
     if (!valb) 
     { 
      Console.WriteLine("Policy Exist"); 
      Console.WriteLine("An error occurred, please try again."); 
     } 
     try 
     { 
      conn.Open(); 
      SqlCommand cmd = conn.CreateCommand(); 

      cmd.CommandType = CommandType.StoredProcedure; 
      cmd.CommandText = "advantage.InsertIntoAdvantageEligibilityOverride"; 
      SqlParameter spPolicyNumber = cmd.Parameters.Add("@PolicyNumber", SqlDbType.VarChar); 
      spPolicyNumber.Direction = ParameterDirection.Input; 
      spPolicyNumber.Value = PolicyNumber; 


      SqlParameter spPolicyMod = cmd.Parameters.Add("@PolicyMod", SqlDbType.VarChar); 
      spPolicyMod.Direction = ParameterDirection.Input; 
      spPolicyMod.Value = PolicyMod; 


      SqlParameter spAdvantagePolicyIndicator = cmd.Parameters.Add("@AdvantagePolicyIndicator", SqlDbType.Int); 
      spAdvantagePolicyIndicator.Direction = ParameterDirection.Input; 
      spAdvantagePolicyIndicator.Value = AdvantagePolicyIndicator; 


      SqlParameter spisDeleted = cmd.Parameters.Add("@isDeleted", SqlDbType.Int); 
      spisDeleted.Direction = ParameterDirection.Input; 
      spisDeleted.Value = isDeleted; 


      SqlParameter spUserName = cmd.Parameters.Add("@UserName", SqlDbType.VarChar); 
      spUserName.Direction = ParameterDirection.Input; 
      spUserName.Value = UserName; 

      cmd.CommandType = CommandType.StoredProcedure; 
      cmd.ExecuteNonQuery(); //this was missing. 

     } 
     catch (SqlException ex) 
     { 
      Console.WriteLine(ex.ToString()); 
     } 
     finally 
     { 
      ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true); 
      conn.Close(); 
     } 

    } 

我想要得到的應用程序中顯示基於CheckPolicy的if-case語句中的字符串值是什麼,屏幕上的文本(不是在彈出窗口中是整個問題的原因)。當策略爲檢查在CheckPolicy當結果是「政策存在」我希望SaveExceptionData返回「保存成功」,如果返回除了什麼「政策存在」,然後返回「保存失敗」。我相信要正確地做到這一點,我需要獲取CheckPolicy中的字符串結果,以便通過SaveExceptionData讀取並使用連接到將執行我想要的結果的if語句的bool?任何方向將不勝感激。

另外請注意,我試圖創建一個引用的CheckPolicy結果的布爾但我不能讓它在SaveExceptionData部分引用。 SaveExceptionData標記爲GetPolicyCheck和Bool的混亂是什麼。

+0

如果你想要回發沒有 - 這是MVC或Web窗體? MVC非常簡單,只需創建一個鉤子,以便您可以使用JavaScript進行抓取。 Web Forms也不難,你會依賴[WebMethod]。基本上,我會讓CheckPolicy返回一個複合對象(一個「代碼」和一個可以在你的標籤中出現的字符串)。然後在你的頁面中,使用$ .ajax(jQuery)或其他一些方法調用CheckPolicy,獲取代碼和/或字符串,並根據需要採取行動。 如果你罰款回傳,很多這是非常實際意義。只要可訪問,您可以設置lblresult.Text。 – Robert

+0

我很抱歉這個遲到的迴應,我忙於另一個項目。你可能會告訴我如何調用回傳?我研究了它並理解它的作用,但我不確定在我的情況下它將如何實施。感謝您的時間到目前爲止 –

回答

0

這裏是我如何做到這一點,但你的代碼在適用的拼接。

有你的返回對象的類。可以作爲顆粒或者你想它(如果你想只返回一個消息,然後在客戶端集反應基於字符串,這樣做,如果你喜歡一個代碼,做到這一點。如果你只想回報模糊一個字符串,你可以做到這一點,旁路創建一個特殊類):在這一點上

public class CheckPolicyMessage 
{ 
    public string Code { get; set; } // could be whatever type you want it to be. 
    public string Message { get; set; } 
} 

現在,我會修改CheckPolicy返回此消息。

[WebMethod] 
public CheckPolicyMessage CheckPolicy(string PolicyNumber, string PolicyMod) 
{ 
    CheckPolicyMessage msg = new CheckPolicyMessage(); 
    ... 
    // set the msg properties in all of your case statements, note, you have 
    // one treated as an exception case (Policy Exist) but doesn't appear that you 
    // communicate it back to the user. 
    msg.Code = "???"; 
    msg.Message = "Some message you want to display here"; 
    ... 
    return msg; 
} 

然後在JavaScript中使用jQuery(儘管這是可選的,很明顯),我會做:

function CheckPolicy() { 
    var policyCheck = new Object(); 
    policyCheck["PolicyNumber"] = // grab Policy Number from page. 
    policyCheck["PolicyMod"] = // grab Policy Mod from page. 

    $.ajax({ 
    url: 'NameOfAspxPageWithCheckPolicyMethod.aspx/CheckPolicy', 
    type: 'POST', 
    contentType: 'application/json; charset=utf-8', 
    data: JSON.stringify(policyCheck), 
    success: function (data) { 
     // I *think* it'd be data.d.Message. When I've done it, I'm returning 
     // lists so it is data.d[i].Whatever and iterating it in a for loop. 
     // can use Chrome's Dev Tools (or others) to inspect the data element 
     // at the next line and see what it contains (which tells you how to grab). 
     // May be data.Message, not sure, but again, inspect data at this point. 
     $('#IdOfSpanForlblresult').text(data.d.Message); 
    }, 
    error: function (response) { 
     // probably a similar thing, but you will want to communicate 
     // how that it failed on the server end somehow. Possibly: 
     // $('#IdOfSpanForlblresult').text(response.responseJSON.Message); 
    } 
    }); 
} 
+0

感謝您的回覆!我最終找到了一個在朋友的幫助下更簡單的方法。我簡單地添加了lblresult.Text =「保存成功」;在SaveExceptionData底部的conn.Close()之前。 –