3

I have database code like this

try
{
    string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString;

    SqlConnection myConnection = new SqlConnection(strConnectionString);
    myConnection.Open();

    string hesap = Label1.Text;
    string musteriadi = DropDownList1.SelectedItem.Value;
    string avukat = DropDownList2.SelectedItem.Value;

    SqlCommand cmd = new SqlCommand("INSERT INTO AVUKAT VALUES (@MUSTERI, @AVUKAT, @HESAP)", myConnection);

    cmd.Parameters.AddWithValue("@HESAP", hesap);
    cmd.Parameters.AddWithValue("@MUSTERI", musteriadi);
    cmd.Parameters.AddWithValue("@AVUKAT", avukat);
    cmd.Connection = myConnection;

    SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
    Response.Redirect(Request.Url.ToString());
    myConnection.Close();
}
catch (Exception)
{
    Response.Write("<h2>ERROR</h2>");
}

It works fine but what I want, in the catch function, is to call the javascript alert function.

I've tried this

Response.Write("<script language=javascript>alert('ERROR');</script>);

But there is an error enter image description here

How can I show error message in javascript alert function?

Rob
  • 45,296
  • 24
  • 122
  • 150
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364

7 Answers7

12

Replace:

Response.Write("<script language=javascript>alert('ERROR');</script>);

With

Response.Write("<script language=javascript>alert('ERROR');</script>");

In other words, you're missing a closing " at the end of the Response.Write statement.

It's worth mentioning that the code shown in the screenshot appears to correctly contain a closing double quote, however your best bet overall would be to use the ClientScriptManager.RegisterScriptBlock method:

var clientScript = Page.ClientScript;
clientScript.RegisterClientScriptBlock(this.GetType(), "AlertScript", "alert('ERROR')'", true);

This will take care of wrapping the script with <script> tags and writing the script into the page for you.

Rob
  • 45,296
  • 24
  • 122
  • 150
  • @Soner, I suspect you're not showing us enough / the right code =) I've just copied & pasted the replacement I suggested (with the closing double quote) into a page and it ran without compilation error and showed a javascript popup of 'ERROR' for me. – Rob Feb 23 '11 at 09:21
6

Try using RegisterScriptBlock. Example from the link:

public void Page_Load(Object sender, EventArgs e)
{
    // Define the name and type of the client scripts on the page.
    String csname1 = "PopupScript";
    String csname2 = "ButtonClickScript";
    Type cstype = this.GetType();

    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Check to see if the startup script is already registered.
    if (!cs.IsStartupScriptRegistered(cstype, csname1))
    {
      String cstext1 = "alert('Hello World');";
      cs.RegisterStartupScript(cstype, csname1, cstext1, true);
    }

    // Check to see if the client script is already registered.
    if (!cs.IsClientScriptBlockRegistered(cstype, csname2))
    {
      StringBuilder cstext2 = new StringBuilder();
      cstext2.Append("<script type=\"text/javascript\"> function DoClick() {");
      cstext2.Append("Form1.Message.value='Text from client script.'} </");
      cstext2.Append("script>");
      cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false);
    }
}
JW Lim
  • 1,794
  • 3
  • 21
  • 41
Neil Knight
  • 47,437
  • 25
  • 129
  • 188
3
string str = "Error mEssage";
Response.Write("<script language=javascript>alert('"+str+"');</script>");
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0

You ca also use Response.Write("alert('Error')");

Nadeem Shaikh
  • 117
  • 14
0

Concatenate the string separating the slash and the word script in this way.

Response.Write("<script language='javascript'>alert('Especifique Usuario y Contraseña');</" + "script>");
0

Use this....

string popupScript = "<script language=JavaScript>";
popupScript += "alert('Please enter valid Email Id');";
popupScript += "</";
popupScript += "script>";
Page.RegisterStartupScript("PopupScript", popupScript);
-1

You can simply write

try
 {
    //Your Logic and code
 }
 catch (Exception ex)
 {
    //Error message in  alert box
    Response.Write("<script>alert('Error :" +ex.Message+"');</script>");
 }

it will work fine

Ritz
  • 394
  • 1
  • 3
  • 12
  • 2
    The question was five years old, answered, and your answer is the same as other answers (essentially stolen). – devRicher Jan 24 '17 at 13:15