这里当我选择相同的日期和同一批次时,它应该给我带弹出的警告信息。我尝试了try
catch
方法,但它无法正常工作。请告诉我出错的地方。
到目前为止我的代码
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 batch=@batch and datedif=@datedif)", 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 :(得分: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>
最好做一些商店程序。