vb.net Response.Redirect 使用查询字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15348469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:45:13  来源:igfitidea点击:

Response.Redirect using query string

asp.netvb.netms-accessquery-stringresponse.redirect

提问by adaam

Okay so I've just written my own "add a book to the database" ASP.net page that looks like so:

好的,所以我刚刚编写了自己的“向数据库添加一本书”的 ASP.net 页面,如下所示:

<div class="addbook"><br />If you feel that we&#39;ve forgotten a book
that you love, please fill out this form 
below to help us keep the site fresh and dynamic!
<h3>Title:</h3><asp:TextBox ID="tb_booktitle" runat="server"></asp:TextBox>
<h3>Author:</h3><asp:TextBox ID="tb_bookauthor" runat="server"></asp:TextBox>
<h3>Publication Date:</h3><asp:TextBox ID="tb_bookpubyear" runat="server" ></asp:TextBox>    <asp:CompareValidator
ID="CompareValidatorTextBox1" runat="server"
ControlToValidate="tb_bookpubyear"
Type="Date"
Operator="DataTypeCheck" 
ErrorMessage="Date must be in the DD/MM/YYYY format)" ForeColor="Red"
/>
<h3>Number of Pages:</h3><asp:TextBox ID="tb_bookpages" runat="server"></asp:TextBox><asp:RangeValidator ID="RangeValidator1" runat="server" 
    ControlToValidate="tb_bookpages" 
    ErrorMessage="Please enter a number." ForeColor="Red" 
    MaximumValue="9999999" MinimumValue="1" SetFocusOnError="True"></asp:RangeValidator>


<h3>Publisher:</h3><asp:TextBox ID="tb_publisher" runat="server"></asp:TextBox>
<h3>Book Cover:</h3>
<asp:FileUpload ID="fu_picture" runat="server" />
<h3>Your Rating:</h3>
<p>    
    <asp:RadioButtonList ID="rbl_Stars" runat="server" 
    RepeatDirection="Horizontal" Width="142px">
    <asp:ListItem>1</asp:ListItem>
    <asp:ListItem>2</asp:ListItem>
    <asp:ListItem>3</asp:ListItem>
    <asp:ListItem>4</asp:ListItem>
    <asp:ListItem>5</asp:ListItem>
</asp:RadioButtonList>
</p>
<p>
    <asp:Button ID="btn_submission" runat="server" Text="Upload Book!" />
</p>
<div class="addbook"><br />If you feel that we&#39;ve forgotten a book
that you love, please fill out this form 
below to help us keep the site fresh and dynamic!
<h3>Title:</h3><asp:TextBox ID="tb_booktitle" runat="server"></asp:TextBox>
<h3>Author:</h3><asp:TextBox ID="tb_bookauthor" runat="server"></asp:TextBox>
<h3>Publication Date:</h3><asp:TextBox ID="tb_bookpubyear" runat="server" ></asp:TextBox>    <asp:CompareValidator
ID="CompareValidatorTextBox1" runat="server"
ControlToValidate="tb_bookpubyear"
Type="Date"
Operator="DataTypeCheck" 
ErrorMessage="Date must be in the DD/MM/YYYY format)" ForeColor="Red"
/>
<h3>Number of Pages:</h3><asp:TextBox ID="tb_bookpages" runat="server"></asp:TextBox><asp:RangeValidator ID="RangeValidator1" runat="server" 
    ControlToValidate="tb_bookpages" 
    ErrorMessage="Please enter a number." ForeColor="Red" 
    MaximumValue="9999999" MinimumValue="1" SetFocusOnError="True"></asp:RangeValidator>


<h3>Publisher:</h3><asp:TextBox ID="tb_publisher" runat="server"></asp:TextBox>
<h3>Book Cover:</h3>
<asp:FileUpload ID="fu_picture" runat="server" />
<h3>Your Rating:</h3>
<p>    
    <asp:RadioButtonList ID="rbl_Stars" runat="server" 
    RepeatDirection="Horizontal" Width="142px">
    <asp:ListItem>1</asp:ListItem>
    <asp:ListItem>2</asp:ListItem>
    <asp:ListItem>3</asp:ListItem>
    <asp:ListItem>4</asp:ListItem>
    <asp:ListItem>5</asp:ListItem>
</asp:RadioButtonList>
</p>
<p>
    <asp:Button ID="btn_submission" runat="server" Text="Upload Book!" />
</p>

And the code behind:

以及背后的代码:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Protected Sub btn_submission_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_submission.Click
    Dim myGUID = Guid.NewGuid()

    Dim newFileName As String = myGUID.ToString() & ".jpg"
    Dim fileLocationOnServerHardDisk = Request.MapPath("img/thumb") & "/" & newFileName
    fu_picture.SaveAs(fileLocationOnServerHardDisk)
    Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
    Dim SqlString As String = "Insert into booklist(Title,Author,PublicationDate,Pages,Publisher,imgurl,AverageRating)
Values (@f1,@f2,@f3,@f4,@f5,@f6,@f7)"
    Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
    cmd.CommandType = CommandType.Text
    cmd.Parameters.AddWithValue("@f1", tb_booktitle.Text)
    cmd.Parameters.AddWithValue("@f2", tb_bookauthor.Text)
    cmd.Parameters.AddWithValue("@f3", tb_bookpubyear.Text)
    cmd.Parameters.AddWithValue("@f4", tb_bookpages.Text)
    cmd.Parameters.AddWithValue("@f5", tb_publisher.Text)
    cmd.Parameters.AddWithValue("@f6", "img/thumb/" & newFileName)
    cmd.Parameters.AddWithValue("@f7", rbl_Stars.SelectedValue)
    oleDbConn.Open()
    cmd.ExecuteNonQuery()
    Response.Redirect("detail.aspx?ID={0}")

End Sub

Protected Sub rbl_Stars_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles rbl_Stars.SelectedIndexChanged

End Sub 
End Class
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Protected Sub btn_submission_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_submission.Click
    Dim myGUID = Guid.NewGuid()

    Dim newFileName As String = myGUID.ToString() & ".jpg"
    Dim fileLocationOnServerHardDisk = Request.MapPath("img/thumb") & "/" & newFileName
    fu_picture.SaveAs(fileLocationOnServerHardDisk)
    Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
    Dim SqlString As String = "Insert into booklist(Title,Author,PublicationDate,Pages,Publisher,imgurl,AverageRating)
Values (@f1,@f2,@f3,@f4,@f5,@f6,@f7)"
    Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
    cmd.CommandType = CommandType.Text
    cmd.Parameters.AddWithValue("@f1", tb_booktitle.Text)
    cmd.Parameters.AddWithValue("@f2", tb_bookauthor.Text)
    cmd.Parameters.AddWithValue("@f3", tb_bookpubyear.Text)
    cmd.Parameters.AddWithValue("@f4", tb_bookpages.Text)
    cmd.Parameters.AddWithValue("@f5", tb_publisher.Text)
    cmd.Parameters.AddWithValue("@f6", "img/thumb/" & newFileName)
    cmd.Parameters.AddWithValue("@f7", rbl_Stars.SelectedValue)
    oleDbConn.Open()
    cmd.ExecuteNonQuery()
    Response.Redirect("detail.aspx?ID={0}")

End Sub

Protected Sub rbl_Stars_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles rbl_Stars.SelectedIndexChanged

End Sub 
End Class

As you can see in the code behind above, once the connection to the database has been made, i want to redirect to the detail.aspx (which is a page showing a single book record from the database and associated content) using a query string to show the record that was just added . But I receive an error like so:

正如您在上面的代码中看到的那样,一旦建立了与数据库的连接,我想使用查询字符串重定向到 detail.aspx(这是一个页面,显示数据库中的单个书籍记录和相关内容)显示刚刚添加的记录。但我收到这样的错误:

Input string was not in a correct format. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[FormatException: Input string was not in a correct format.]
System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +10722195 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +145
System.String.System.IConvertible.ToInt32(IFormatProvider provider) +46 System.Convert.ChangeType(Object value, TypeCode typeCode, IFormatProvider provider) +297
System.Web.UI.WebControls.Parameter.GetValue(Object value, String defaultValue, TypeCode type, Boolean convertEmptyStringToNull, Boolean ignoreNullableTypeChanges) +126
System.Web.UI.WebControls.Parameter.GetValue(Object value, Boolean ignoreNullableTypeChanges) +63
System.Web.UI.WebControls.Parameter.get_ParameterValue() +40
System.Web.UI.WebControls.ParameterCollection.GetValues(HttpContext context, Control control) +247
System.Web.UI.WebControls.SqlDataSourceView.InitializeParameters(DbCommand command, ParameterCollection parameters, IDictionary exclusionList) +257 System.Web.UI.WebControls.SqlDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +589
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +21
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +138
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +30
System.Web.UI.WebControls.GridView.DataBind() +4
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +105 System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +75 System.Web.UI.Control.EnsureChildControls() +83 System.Web.UI.Control.PreRenderRecursiveInternal() +42
System.Web.UI.Control.PreRenderRecursiveInternal() +168
System.Web.UI.Control.PreRenderRecursiveInternal() +168
System.Web.UI.Control.PreRenderRecursiveInternal() +168
System.Web.UI.Control.PreRenderRecursiveInternal() +168
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +974

输入字符串的格式不正确。说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

异常详细信息:System.FormatException:输入字符串的格式不正确。

源错误:

执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常来源和位置的信息。

堆栈跟踪:

[FormatException: 输入字符串的格式不正确。]
System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +10722195 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info ) +145
System.String.System.IConvertible.ToInt32(IFormatProvider provider) +46 System.Convert.ChangeType(Object value, TypeCode typeCode, IFormatProvider provider) +297
System.Web.UI.WebControls.Parameter.GetValue(Object value, String defaultValue, TypeCode 类型, Boolean convertEmptyStringToNull, Boolean ignoreNullableTypeChanges) +126
System.Web.UI.WebControls.Parameter.GetValue(Object value, Boolean ignoreNullableTypeChanges) +63
System.Web.UI.WebControls.Parameter.get_ParameterValue() +40
System.Web.UI.WebControls.ParameterCollection.GetValues(HttpContext context, Control control) +247
System.Web.UI.WebControls.SqlDataSourceView.InitializeParameters(DbCommand 命令, ParameterCollection 参数,IDictionary excludeList) +257 System.Web.UI.WebControls.SqlDataSourceView.ExecuteSelect(DataSourceSelectArguments 参数) +589
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments 参数,DataSourceViewSelectCallback 回调) +21
System.Web.UI.WebControls .DataBoundControl.PerformSelect() +138
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +30
System.Web.UI.WebControls.GridView.DataBind() +4
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +105 System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +75 System.Web.UI.Control.EnsureChildControls() +83 System.Web.UI.Control .PreRenderRecursiveInternal() +42
System.Web.UI.Control.PreRenderRecursiveInternal() +168
System.Web.UI.Control.PreRenderRecursiveInternal() +168
System.Web.UI.Control.PreRenderRecursiveInternal() +168
System.Web.UI .Control.PreRenderRecursiveInternal() +168
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +974

How would I go about achieving what I am trying to do?

我将如何实现我想要做的事情?

采纳答案by Ryan Gunn

Why not create a stored procedure that has an ouput parameter of the id of your newly inserted book and then do the following

为什么不创建一个存储过程,它的输出参数是新插入的书的 id 参数,然后执行以下操作

If outputparam.value isnot nothing then
    Response.Redirect(String.Format("detail.aspx?ID={0}",outputparam.value)
end if

Please look at this url for more info.

请查看此网址以获取更多信息。

Input and Output Parameters, and Return Values

输入和输出参数以及返回值