1

I am testing my knowledge of ADO.NET and SQL and am currently just trying to do the basics. I want to read from a table and when the user clicks a button, a message box pops up with the value in the ApplicationName column.

It currently doesn't do anything when I click the button... any ideas?

protected void TestSubmit_ServerClick(object sender, EventArgs e)
 {
 // Initialize the database connector.
 SqlConnection connectSQL = new SqlConnection();
 // Send the connection string.
 connectSQL.ConnectionString = @"Data Source = localhost\SQLEXPRESS;" + 
 "Initial Catalog = Inputs; Integrated Security = SSPI";
 try
 {
 // Setup our command.
 SqlCommand theCommand = new SqlCommand("SELECT * FROM Inputs", connectSQL);
 // Write the stored value in the text boxes.
 connectSQL.Open();
 SqlDataReader theReader;
 theReader = theCommand.ExecuteReader();
 theReader.Read();
 MessageBox(theReader["ApplicationName"].ToString());
 theReader.Close();
 connectSQL.Close();
 }
 catch (Exception ee)
 {
 MessageBox("Oopsie: " + ee);
 } 
 private void MessageBox(string msg)
 {
 Label lbl = new Label();
 lbl.Text = "" + Environment.NewLine + "window.alert('" + msg + "')";
 Page.Controls.Add(lbl);
 }
asked Aug 13, 2009 at 18:28

3 Answers 3

2

You are basically just sending "window.alert('your message');" as HTML to the browser, this wont execute as JavaScript. Do you really want it to be a "popup"? If so consider using RegisterStartupScript() instead of outputting the JS via a label (http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx). If not then why not just set the contents of the label to your return message?

answered Aug 13, 2009 at 18:36
2
  • right - the problem with your implementation is that you're transmitting a fragment of Javascript without a <script></script> tag around it. Clearly that's not going to execute the script. The proper way to do this server-side on a postback in WinForms is to register the script using RegisterStartupScript or RegisterClientScript Commented Aug 13, 2009 at 18:45
  • DOH!!! It didn't copy over the <script>.... it's there, but StackOverflow won't let it copy over... Commented Aug 13, 2009 at 18:51
0

Sample taken from MSDN & modified for your example

private void MessageBox(string msg)
{
 StringBuilder cstext2 = new StringBuilder();
 cstext2.Append("<script type=\"text/javascript\">");
 cstext2.Append("window.alert('" + msg + "')} </");
 cstext2.Append("script>");
 ClientScript.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false);
}

You could also use RegisterStartupScript instead of RegisterClientScriptBlock.

EDIT: OR the classic ASP way should also work.
I am writing this without any editor.

Response.Write(@"<script language='javascript'>window.alert('" + msg + "');</script>");
answered Aug 13, 2009 at 18:39
0

It does execute actually. I use this in other code and that messagebox function works fine on other projects.

Here's what I really want to do:

try
 {
 // Setup our command.
 SqlCommand theCommand = new SqlCommand("SELECT * FROM Inputs", connectSQL);
 // Write the stored value in the text boxes.
 connectSQL.Open();
 SqlDataReader theReader;
 theReader = theCommand.ExecuteReader();
 theReader.Read();
 TextBox6.Text = (theReader["ApplicationName"].ToString());
 theReader.Close();
 connectSQL.Close();
 }
 catch (Exception ee)
 {
 MessageBox("Oopsie: " + ee);
 } 

Note that TextBox6 is an ASP text box on the website. Clicking TestSubmit does not show the text.

answered Aug 13, 2009 at 18:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.