Problem to show the message

Hi,
I don't know why the prompt message is sometimes shown but sometimes not. For instance, 1st message below can be shown but 3rd one cannot be shown.

                        Page.ClientScript.RegisterStartupScript(this.GetType(), "Information", "javascript:alert('Invalid number');", true);
                        ...
                        catch (Exception ex2)
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "Information", "javascript:alert('exception got');", true);
                            return;
                        }
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "Information", "javascript:alert('Have applied the change fine');", true);
                        ...

Open in new window

if I put something like

lb_msg.Text=...;

Open in new window

instead of 3rd message line above, then that line can be executed. why?
LVL 11
HuaMin ChenProblem resolverAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
The keys for the script have to be unique (i.e. you can't register two different scripts with "Information" as the key). Try changing "Information" in the 3rd one to something else and seeing if that makes a difference.
0
David Johnson, CD, MVPOwnerCommented:
You hit the nail on the head, Carl
A startup script is uniquely identified by its key and its type. Scripts with the same key and type are considered duplicates. Only one script with a given type and key pair can be registered with the page. Attempting to register a script that is already registered does not create a duplicate of the script.
http://msdn.microsoft.com/en-us/library/z9h4dk8y%28v=vs.110%29.aspx
0
HuaMin ChenProblem resolverAuthor Commented:
Thanks all.
I've tried to instead put these
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "InfoA", "javascript:alert('Invalid number');", true);
                        ...
                        catch (Exception ex2)
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "InfoB", "javascript:alert('exception got');", true);
                            return;
                        }
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "InfoC", "javascript:alert('Have applied the change fine');", true);
                        ...

Open in new window

but the 3rd message still cannot be displayed. But if I put something like

lb_msg.Text=...;

Open in new window


instead of 3rd message line above, then that line can be executed. why?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Carl TawnSystems and Integration DeveloperCommented:
What do you mean by "can't be executed"? Do you mean it just doesn't do anything, or does it raise an exception?
0
HuaMin ChenProblem resolverAuthor Commented:
I mean 3rd message line above does not show one message as expected. But if I put something like

lb_msg.Text=...;

instead of 3rd message line above, then that line can be executed.
0
Carl TawnSystems and Integration DeveloperCommented:
When you "view source" on your outputted HTML do you see the line of javascript?
0
HuaMin ChenProblem resolverAuthor Commented:
I choose "View source" on the web page, and then try to find the message

Have applied the change fine

Open in new window


but I cannot find out such message within that.
0
Carl TawnSystems and Integration DeveloperCommented:
Well, what you have should work fine; even having two startup scripts should work ok.
Is there something else in your code that could be causing it not to reach that line?
0
HuaMin ChenProblem resolverAuthor Commented:
That line can be reached. If not, why this line below

lb_msg.Text=...;

can be executed, if I put it instead of the above 3rd message line.
0
Carl TawnSystems and Integration DeveloperCommented:
Can you post your code as you currently have it? That's everything from the first "RegisterStartupScript" line, up to and including the third one.
0
HuaMin ChenProblem resolverAuthor Commented:
To provide both code-behind and markup page to you here?
0
HuaMin ChenProblem resolverAuthor Commented:
I just wonder why one message can be shown there but one other message cannot.
0
Carl TawnSystems and Integration DeveloperCommented:
Just the code-behind will do. There is not reason why what you are trying to do shouldn't be working - that's why I wanted to see the whole of your code to see if there is something odd that is preventing the line from executing.

Maybe that line is throwing and exception and you are suppressing it somewhere with and empty catch block.

If I run this:
        Try
            ClientScript.RegisterStartupScript(Me.GetType(), "Information", "alert('Hello world');", True)
        Catch ex As Exception
            ClientScript.RegisterStartupScript(Me.GetType(), "Information", "alert('Something went wrong');", True)
        End Try

        ClientScript.RegisterStartupScript(Me.GetType(), "Information", "alert('Hello another world');", True)

Open in new window

I only get one message, as expected, because of the duplicate key name.
However, this:
        Try
            ClientScript.RegisterStartupScript(Me.GetType(), "Information", "alert('Hello world');", True)
        Catch ex As Exception
            ClientScript.RegisterStartupScript(Me.GetType(), "Information", "alert('Something went wrong');", True)
        End Try

        ClientScript.RegisterStartupScript(Me.GetType(), "Information2", "alert('Hello another world');", True)

Open in new window

Works as expected.

So I can only think that you have something somewhere in your code that is preventing it from behaving as it should.

By the way, what version of the .Net framework are you using?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HuaMin ChenProblem resolverAuthor Commented:
Many thanks Carl. Here is code-behind.

using System;
using System.IO;
using System.IO.Compression;
using System.Text; // This was needed to make ASCIIEncoding function
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.SqlTypes;
using System.Data;
using System.Data.Common;
using System.Xml;
using System.Windows;
using System.Data.OleDb;
using System.Security.Cryptography; // This was needed to make everything related to MD5 function

namespace Verify_ite
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void lb_reg_Click(object sender, EventArgs e)
        {
            Response.Redirect("http://192.168.168.1/Add_ite2_dev/Default.aspx");
        }
        protected void ValButton_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Mssqlconn2"].ConnectionString);
            //OleDbConnection conn = null;
            //conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Jet OLEDB:Database Password=?????;Data Source=C:\inetpub\VS2012\DB1.accdb");
            int ret_val = 0;
            if (tb_ver_code.Text == "")
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "InfoA", "javascript:alert('testing1');", true);
                return;
            }
            else
            {
                conn.Open();
                try
                {
                    SqlCommand cmd = new SqlCommand("select COUNT(*) from ite_master where verify_code=@ver_cd and isnull(verified,0)=1", conn);
                    cmd.Parameters.Add("ver_cd", SqlDbType.Decimal).Value = Convert.ToDecimal(tb_ver_code.Text);
                    ret_val = Convert.ToInt32(cmd.ExecuteScalar());
                    if (ret_val > 0)
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "InfoB", "javascript:alert('testing2');", true);
                        //lb_msg.Text = "testing2";
                        return;
                    }
                }
                catch (Exception ex)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "InfoC", "javascript:alert('testing3');", true);
                    return;
                }
                finally
                {
                    conn.Close();
                }
                conn.Open();
                try
                {
                    SqlCommand cmd = new SqlCommand("select COUNT(*) from ite_master where verify_code=@ver_cd and isnull(verified,0)=0", conn);

                    cmd.Parameters.Add("ver_cd", SqlDbType.Int).Value = Convert.ToInt32(tb_ver_code.Text);
                    //cmd.Parameters.Add("ver_cd",OleDbType.Decimal).Value = Convert.ToDecimal(tb_ver_code.Text);
                    ret_val = Convert.ToInt32(cmd.ExecuteScalar());
                    if (ret_val == 0)
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "InfoD", "javascript:alert('Invalid number');", true);
                        //lb_msg.Text = "Invalid number";
                        return;
                    }
                    else
                    {
                        try
                        {
                            SqlCommand cmd2 = new SqlCommand("update ite_master set verified=1 where verify_code=@ver_cd", conn);
                            cmd2.Parameters.Add("ver_cd", SqlDbType.Int).Value = Convert.ToInt32(tb_ver_code.Text);
                            cmd2.ExecuteNonQuery();
                        }
                        catch (Exception ex2)
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "InfoE", "javascript:alert('exception got');", true);
                            return;
                        }
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "InfoF", "javascript:alert('Have applied the change fine');", true);
                        //lb_msg.Text = "Have applied the change fine";
                    }
                }
                catch (Exception ex)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "InfoG", "javascript:alert('testing4');", true);
                    return;
                }
                finally
                {
                    conn.Close();
                }
            }
        }
    }
}

Open in new window


In above, I don't know why the message with key InfoF, is not shown as expected. But if I instead put "lb_msg.Text" line that is right after it, then such line can be called, as I can see the value of such label.

I am using .Net framework v4.0.
0
Carl TawnSystems and Integration DeveloperCommented:
I've tried your code in a test environment and I can hit all of the javascript messages fine. The only route through your code I can see that wouldn't hit a message is if one of the conn.Open() lines threw an exception - seeing as they sit outside of your try...catch blocks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.