【问题标题】:Asp.net error using Visual Basic Input Box使用 Visual Basic 输入框的 Asp.net 错误
【发布时间】:2018-12-26 18:30:58
【问题描述】:

我在 C# 中使用 Visual Basic 参考向表中添加位置。 这在本地有效,但在将其发布到托管站点后会产生此错误: 当应用程序未在 UserInteractive 模式下运行时显示模式对话框或表单不是有效操作。指定 ServiceNotification 或 DefaultDesktopOnly 样式以显示来自服务应用程序的通知。

我的代码如下:

        string addlocation = Microsoft.VisualBasic.Interaction.InputBox("Enter New Location", "Location", "", 600, 400);
        if (addlocation == "" || addlocation == null)
        {
            Microsoft.VisualBasic.Interaction.MsgBox("Enter a Valid Name!", 0);
            return;
        }
        using (var connection3 = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString))
        {
            connection.Open();
            SqlCommand comm = new SqlCommand("SELECT COUNT(*) FROM Locations WHERE Locations = '" + addlocation + "'", connection);
            Int32 count = Convert.ToInt32(comm.ExecuteScalar());

            if (count == 0)
            using (var cmd1 = new SqlCommand("INSERT INTO Locations(Locations) VALUES('" + addlocation + "');", connection3))
            {
                connection3.Open();
                cmd1.ExecuteNonQuery();

                connection3.Close();
                Page.Response.Redirect(Page.Request.Url.ToString(), true);
                connection3.Close();
            }
            else
            {
                Microsoft.VisualBasic.Interaction.MsgBox("Location Already Exists!", 0);
            }
            connection.Close();
        }

【问题讨论】:

  • 这是一个网站?你不能显示那些类型的对话框。即使没有导致错误,也没有人会看到它们。您可以在本地电脑上看到它,因为您以交互方式登录到服务器。
  • ... 并使用参数来避免查询中的 sql 注入和格式错误。

标签: c# asp.net visual-studio


【解决方案1】:

为什么您使用 VB msgbox 而它已被 MessageBox.Show 取代。您可以在下面获得更多帮助 -

Issue with Microsoft.VisualBasic.Interaction.MsgBox method

【讨论】:

  • MessageBox.Show 不太可能在 ASP.NET 环境中工作得更好
  • 感谢您的及时回复。对不起,我是新手(相对于 Asp.net,当然也部署到主机站点)。我从 1980 年就开始从事计算机行业,对于大多数年轻一代来说,我可能看起来是个白痴。但如果他们需要 Fortran、Cobol、DEC DCL (VMS) 甚至旧的 HP Basic 编程方面的帮助,我将能够提供帮助。关键是概念很难同化,例如当第一个浏览器在 1990 年代发布时,我花了很长时间才掌握它的全部内容。本质上,我需要被引导走向启蒙。问候吉姆
  • PS:回复都是关于消息框而不是请求添加到 SQL Server 表的输入。
【解决方案2】:

由于后面的代码在服务器端运行,您不能同时使用InputBoxMsgBox 方法在客户端浏览器中显示输入或警告框。我建议使用 JS prompt 作为输入框替换,或者使用您自己的包含文本输入的模态对话框(例如 Bootstrap 模态),然后使用 alert 函数来显示消息。

这是一个显示提示并重定向到代码隐藏方法的示例:

var location = prompt("Enter New Location", "");

// check against null or empty value
if (location == null || location == "")
{
    alert("Enter a Valid Name!");
}
else
{
    $.ajax({
        type: "POST",
        url: "pagename.aspx/CheckLocation",
        data: "{'location':'" + location + "'}",
        dataType: "json",
        success: function (response) {
            if (response.d == true) {
                // show alert box
                alert("Location Already Exists!");
            }
            else {
                // redirect to another page
                window.location.href = '<%= Request.Url.ToString() %>';
            }
        },
        error: function (xhr, status, err) {
            // error handling
        }
    });
}

代码隐藏

[WebMethod]
public bool CheckLocation(string location)
{
    bool LocationExists = false;
    using (var connection3 = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString))
    {
        connection.Open();
        SqlCommand comm = new SqlCommand("SELECT COUNT(*) FROM Locations WHERE Locations = @location", connection);
        comm.Parameters.AddWithValue("@location", location);
        int count = Convert.ToInt32(comm.ExecuteScalar());

        if (count == 0)
        {
            using (var cmd1 = new SqlCommand("INSERT INTO Locations(Locations) VALUES(@addlocation)", connection3))
            {
                cmd1.Parameters.AddWithValue("@addlocation", location);
                connection3.Open();
                cmd1.ExecuteNonQuery();
            }
        }
        else
        {
            LocationExists = true;
        }
    }

    return LocationExists;
}

相关问题:

Error while showing a modal dialog box or form

【讨论】:

  • 感谢 Tetsuya,但我无法让您的解决方案正常工作,但我确实使用 Javascript 找到了答案!
【解决方案3】:

使用脚本解决如下:

<title></title>
<script>
 function GetUserValue() {
   var newlength = prompt("Please Enter New Length", "");
   if (newlength != null && newlength != "") {
     document.getElementById("<%=hdnLengthInput.ClientID%>").value = newlength;
     return true;
  }
  else
  return false;
 }
</script>

<div>
 <asp:HiddenField runat="server" ID="hdnLengthInput" />
</div>

And Then in the .CS file:

protected void lnkButton_Click(object sender, EventArgs e)
    {
        Response.Write(hdnLengthInput.Value);
        using (var connection3 = new 
SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString))
        {
            connection.Open();
            SqlCommand comm = new SqlCommand("SELECT COUNT(*) FROM Categories WHERE 
Length = '" + hdnLengthInput.Value + "'", connection);
            Int32 count = Convert.ToInt32(comm.ExecuteScalar());

            if (count == 0)
                using (var cmd1 = new SqlCommand("INSERT INTO Categories(Length) 
VALUES('" + hdnLengthInput.Value + "');", connection3))
                {
                    connection3.Open();
                    cmd1.ExecuteNonQuery();

                    connection3.Close();
                    Page.Response.Redirect(Page.Request.Url.ToString(), true);
                }

            connection.Close();
        }
     }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多