好了,所以我只是寫我自己的「圖書添加到數據庫」 ASP.net頁,看起來像這樣:Response.Redirect的使用查詢字符串
<div class="addbook"><br />If you feel that we'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>
而後面的代碼:
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
正如您在上面的代碼中看到的那樣,一旦與數據庫建立連接,我想重定向到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
我該如何去實現我想要做的事情?
一件事,你的Response.Redirect並不表示什麼{0}指。您需要提供該信息的第二個參數。身份證是什麼以及它會從哪裏來? – Melanie 2013-03-11 21:02:35
該ID是我的數據庫中'booklist'表中的一個字段(連接字符串被稱爲BookMeetConnString) – adaam 2013-03-11 21:03:52
我選擇了類似這樣的內容:Response.Redirect(「detail.aspx?ID =」&Request.QueryString(「ID 「))。重定向工作,但查詢字符串值在URL中爲空(即「http:// localhost:65458/detail.aspx?ID =」) – adaam 2013-03-11 21:38:54