-1

我有这个 C# 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;

public partial class UpdateArticle : System.Web.UI.Page
{
    public string ArticleTitle, ArticleBody, PostDate;

    protected void Page_Load(object sender, EventArgs e)
    {
        string ArticleId;
        try
        { ArticleId = Request.QueryString["ArticleId"].ToString();
            string dpath = Server.MapPath(@"App_Data") + "/MySite.mdb";
            string connectionstring = @"Data source='" + dpath + "';Provider='Microsoft.Jet.OLEDB.4.0';";
            string q = string.Format("select *  from tblArticles where ArticleId={0}", ArticleId);
            OleDbConnection con = new OleDbConnection(connectionstring);
            OleDbCommand cmd = new OleDbCommand(q, con);
            con.Open();
            cmd.ExecuteNonQuery();
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "tbl");
            con.Close();
            ArticleTitle = ds.Tables[0].Rows[0]["ArticleTitle"].ToString();
            ArticleBody = ds.Tables[0].Rows[0]["ArticleBody"].ToString();
            PostDate = ds.Tables[0].Rows[0]["PostDate"].ToString();


        }
        catch
        { }
        string NewArticleTitle, NewArticleDate, NewArticleBody, ArticleId1;  
        try
        {

                NewArticleTitle = Request.Form["ArticleTitle"].ToString();
                NewArticleDate = Request.Form["ArticleDate"].ToString();
                NewArticleBody = Request.Form["ArticleBody"].ToString();
                ArticleId1 = Request.QueryString["ArticleId"].ToString();
                string dpath = Server.MapPath(@"App_Data") + "/MySite.mdb";
                string connectionstring = @"Data source='" + dpath + "';Provider='Microsoft.Jet.OLEDB.4.0';";
                OleDbConnection con = new OleDbConnection(connectionstring);
                string QuaryString = string.Format("insert into tblArticles(ArticleTitle, ArticleBody, PostDate) values ('{0}','{1}','{2}') where ArticleId='{3}'", NewArticleTitle, NewArticleBody, NewArticleDate, ArticleId1);
                OleDbCommand cmd = new OleDbCommand(QuaryString, con);
                OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds, "tbl");
                con.Close();
            }
            catch { }
       // Response.Redirect("ArticlesTable.aspx");

}}

我的代码中的问题是,当涉及到这一行时:

ArticleId1 = Request.QueryString["ArticleId"].ToString();

它从 try 块中退出。上面几行我写了这段代码:

ArticleId = Request.QueryString["ArticleId"].ToString();

它工作得很好。那么为什么下线不起作用呢?当我第一次调试它时:1:在ASPX页面中提交表单后:http: //i.stack.imgur.com/CfKXL.png :这是aspx代码:

<b>
כותרת המאמר :<br /></b><input id="Text1" type="text" name="ArticleTitle" value="<%=ArticleTitle %>"/><p></p>
<b>

תאריך פרסום: <br /></b><input id="Text2" type="text" name="ArticleDate" value="<%=PostDate %>"/><p></p>

<b>

תוכן המאמר: <br /></b> <textarea rows="10" cols="60"  name="ArticleBody"><%=ArticleBody %></textarea>
<br />
<input id="Reset1" type="reset" value="נקה" />
<input id="Submit1" type="submit" value="שלח כתבה!" />
</form>
<p></p><p></p>

<a href="Default.aspx">חזור לדף הבית</a>

并且在提交之后(var 没有得到 Id 值!)在此处输入图像描述

4

2 回答 2

0

如果您的代码突然意外地从try块中退出,则它正在捕获异常。

如果您的 catch 语句像以前那样抛出该异常 catch { },那么您将永远看不到它。

您应该try/catch完全删除并让错误自然冒泡,或者您应该抓住它并对其进行处理:

catch (Exception ex)
{
   // However you want to handle things going wrong.
}
于 2013-01-10T21:27:43.460 回答
0

您的代码抛出异常,很可能是因为Request.QueryString["ArticleId"]超出了范围。我不确定为什么会发生这种情况,但这是最有可能的解释。更改catch {}catch (Exception e) {}在那里设置一个断点,以便您可以检查异常。假设这是正在发生的事情,ArticleId1请在第一次尝试之外声明 a ,然后在设置后将其设置为行ArticleId(在第一次尝试中)。

于 2013-01-10T21:31:23.777 回答