简体   繁体   中英

Throw Alert Message on Asp.net Application not working

I am trying to query against a database and retrieve one row of data then if data exist program continue if not I would like for an alert message to pop up the below code should work, I am not sure what happened after the rebuild the application runs the alert message code just as it were a normal program and does not display the message and no error occurs, I want the alert to popup and when the user clicks ok on the alert focus on the item textbox. please help below is the code I am using:

 try
 {

 var connectionstring = ConfigurationManager.ConnectionStrings["TestDataTable"].ConnectionString;
 OdbcConnection con = new OdbcConnection(connectionstring);
 //con.ConnectionString = connectionstring;
 if (TxtItem.Text != hold_item)
 {
 con.Open();
 OdbcCommand cmd = new OdbcCommand("Select t_item,t_idsc,t_upct,t_item_upc,t_ctyp,t_citg,t_best,t_disp,t_mold,t_csel " + "from informix.tsckcm907 " + "where t_item = " + stitem, con);

 OdbcDataReader myReader = cmd.ExecuteReader();
 DataTable testdt = new DataTable();

 testdt.Load(myReader);
 if (testdt.Rows.Count > 0)
 {
 {
 foreach (DataRow row in testdt.Rows)
 {
 lbldesc.Text = row["t_idsc"].ToString();
 lbldesc.Visible = true;


 Spanish_Item();
 if (hold_lang_value == 0)
 {
 TxtBestBeforeMonths.Text = row["t_best"].ToString();
 holdbest = Convert.ToInt16(TxtBestBeforeMonths.Text);
 }
 else
 {
 DropDownList2.SelectedIndex = 1;
 object stlanguage = 1;
 hold_language = Convert.ToString(stlanguage);
 TxtBestBeforeMonths.Text = row["t_best"].ToString();
 holdbest = Convert.ToInt16(TxtBestBeforeMonths.Text);
 }
 }
 }




 myReader.Close();
 myReader.Dispose();
 cmd.Dispose();
 con.Close();
 con.Dispose();
 }
 else
 {
 string message = "Item Not Found";
 System.Text.StringBuilder sb = new System.Text.StringBuilder();
 sb.Append("<script type = 'text/javascript'>");
 sb.Append("window.onload=function(){");
 sb.Append("alert('");
 sb.Append(message);
 sb.Append("')};");
 sb.Append("</script>");
 ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString()); ;
 TxtItem.Focus();

 }
 }  

so this is what fixed my issue above,

I had to use a bool to stop the program from continuing on and throw the error

 else
    {
     string strErrorDesc = "Item Not Found";
     Response.Write(@"<script language='javascript'>
alert('The following errors have occurred: \n" + strErrorDesc + " .');</script>");
    value = false;
    }
    if (value == false)
    {

    }
    else
    {
    Calc_Rotation();
    Calc_Best_Before();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM