2015-05-08 128 views
0

在這裏,當我選擇相同的日期和同一批次時,它應該彈出警報消息。我嘗試trycatch方法,但它不工作。告訴我我錯了什麼地方。如何通過彈出窗口獲取警報消息?

到目前爲止我的代碼

using (SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) 
     { 
      var fromdate = DateTime.Parse(txtfromdate.Text); 
      var todate = DateTime.Parse(txttodate.Text); 
      var datedif1 = (todate - fromdate).Days; 

      var sqlInsert = new SqlCommand("INSERT INTO datelist ([datedif],[batch],[daywk],[semester],[weekbatch],[subject],[facultyname],[facultyid],[WeekMonth]) SELECT * from (SELECT @datedif as datedif, @batch as batch,@daywk as daywk,@semester as semster,@weekbatch as weekbatch,@subject as subject,@facultyname as facultyname,@facultyid as facultyid,@weekMonth as weekMonth) AS temp WHERE NOT EXISTS (SELECT datedif,batch from datelist where [email protected] and [email protected])", con2); 

      var sqlParamater = sqlInsert.Parameters.Add("@datedif", SqlDbType.Date); 
      var sqlParameter1 = sqlInsert.Parameters.Add("@batch", SqlDbType.NVarChar); 
      var sqlParameter2 = sqlInsert.Parameters.Add("@daywk", SqlDbType.NVarChar); 
      var sqlParameter3 = sqlInsert.Parameters.Add("@semester", SqlDbType.NVarChar); 
      var sqlParameter4 = sqlInsert.Parameters.Add("@weekbatch", SqlDbType.NVarChar); 
      var sqlParameter5 = sqlInsert.Parameters.Add("@subject", SqlDbType.NVarChar); 
      var sqlParameter6 = sqlInsert.Parameters.Add("@facultyname", SqlDbType.NVarChar); 
      var sqlParameter7 = sqlInsert.Parameters.Add("@facultyid", SqlDbType.NVarChar); 
      var sqlParameter8 = sqlInsert.Parameters.Add("@WeekMonth", SqlDbType.NVarChar); 

      con2.Open(); 
      try 
      { 
       for (var i = 0; i <= datedif1; i++) 
       { 
        var consecutiveDate = fromdate.AddDays(i); 

        sqlParamater.Value = consecutiveDate; 
        sqlParameter1.Value = batch1; 
        sqlParameter2.Value = dayweek; 
        sqlParameter3.Value = semester; 
        sqlParameter4.Value = weekbatch; 
        sqlParameter5.Value = subject; 
        sqlParameter6.Value = faculty; 
        sqlParameter7.Value = facultyid; 
        sqlParameter8.Value = weekmonth; 

        int s = sqlInsert.ExecuteNonQuery(); 
       } 

      } 

      catch 
      { 
       Response.Write("<script>alert('Already date is selected.Select another date')</script>"); 
       con2.Close(); 
      } 
     } 
    } 
+0

你是什麼意思_not working_ exact?你有任何異常或錯誤信息?你可以請更具體嗎? –

+0

沒有錯誤,但彈出不起作用。 –

+0

試試這個http://stackoverflow.com/a/29619834/4513879 –

回答

1

1 - 請首先檢查你要去裏面catch塊放在一個調試器那裏。
2 - 將一個調試器也放在您的頁面加載上,以檢查它何時發生。

可以使用的RegisterStartupScript()方法
在服務器端

catch 
     { 
string msg="Already date is selected.Select another date"; 
ScriptManager.RegisterStartupScript(this, GetType(), "myFunction",  "myFunction('"+msg+"');", true); 
} 

在客戶端

<script> 
     function myFunction(a) { 

      alert(a);   
     } 
    </script> 

而且最好是做一些存儲過程。

相關問題