使用Visual Basic输入框的Asp.net错误

时间:2018-12-26 18:30:59

标签: c# asp.net visual-studio

我在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();
        }

3 个答案:

答案 0 :(得分:0)

为什么您将VB msgbox替换为MessageBox.Show的原因。您将在下面获得更多帮助-

Issue with Microsoft.VisualBasic.Interaction.MsgBox method

答案 1 :(得分:0)

由于背后的代码在服务器端运行,因此不能同时使用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

答案 2 :(得分:0)

使用以下脚本解决:

<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();
        }
     }