Try / Catch Block - 退出试用代码

时间:2013-01-10 21:12:34

标签: c# try-catch request.querystring

我有这个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();

它运作得很好。那么为什么下线不起作用呢? 当我第一次调试时:1http://i.stack.imgur.com/CfKXL.png 在ASPX页面中提交表单后: 这是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值!)enter image description here

2 个答案:

答案 0 :(得分:0)

如果您的代码突然意外退出try块,则会出现异常。

如果你的catch语句以catch { }的形式抛弃该异常,那么你将永远不会看到它。

你应该完全删除try/catch并让错误自然地冒出来,或者你应该抓住它并用它做点什么:

catch (Exception ex)
{
   // However you want to handle things going wrong.
}

答案 1 :(得分:0)

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