尝试捕获时遇到问题

本文关键字:遇到 问题 | 更新日期: 2023-09-27 18:34:30

我在客户端设置了一个asp:GridView,如下所示:

<asp:GridView ID="GridView1" DataSourceID="SqlDataSource1" AutoGenerateColumns="true"
            AutoGenerateDeleteButton="true" AutoGenerateEditButton="true" CssClass="GV" PagerStyle-CssClass="pgr" 
            AlternatingRowStyle-CssClass="alt" AllowPaging="true"
            runat="server">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:MyConnection%>" runat="server">
</asp:SqlDataSource>

然后在后面的代码中,我在 try/catch 语句中设置了DataKeyNamesasp:SQLDataSource参数。即使我注释掉了第一次尝试捕获,我也无法让第二次尝试射击。

protected void Page_Load(object sender, EventArgs e)
{
    string qs = Request.QueryString["param"];
    string id = Request.QueryString["id"];
    if (qs != null)
    {
        try
        {
            if (qs == "Department")
            {
                GridView1.DataKeyNames = new string[] {"id"};
                SqlDataSource1.SelectCommand = "SELECT * FROM [table2] "
                    + "WHERE Department_Name LIKE'" + id + "' ORDER BY [Department_Name] DESC";
                SqlDataSource1.UpdateCommand = "UPDATE table2 SET Department_Name=@Department_Name, Phone=@Phone, "
                    + "Fax=@Fax, Contact=@Contact, Address=@Address, City=@City, State=@State "
                    + "WHERE (id = @id)";
                SqlDataSource1.DeleteCommand = "DELETE FROM table2 WHERE id = @id";
            }
        }
        catch (Exception ex)
        {
            SqlDataSource1.SelectCommand = "SELECT * FROM [table1]";
            //ApplicantsSqlDataSource.UpdateCommand = "";
            //ApplicantsSqlDataSource.DeleteCommand = "";
            GridView1.Visible = false;
            NoResults.Text = "<p>Sorry, there are no results that match your search query.<br />" + ex + "</p>";
        }
    }                   
}

这是点击事件

protected void SearchDept_Click(object sender, EventArgs e)
{
    TextBox txtSearchDept = (TextBox)Page.FindControl("txtSearchDept");
    if (txtSearchDept.Text.Length > 0)
    {
        Response.Redirect("Default.aspx?param=Department&id=" + txtSearchDept.Text.ToString());
    }
    else
    {
        NoResults.Text = "<p>Please enter a search parameter.</p>";
    }
}

它应该有效,但它没有

编辑这是最初被遗漏的第一个尝试捕获

try
            {
                if (qs == "LastName")
                {
                    GridView1.DataKeyNames = new string[] {"EMPLOYEE"};
                    SqlDataSource1.SelectCommand = "SELECT * FROM [table1] "
                    + "WHERE Last_Name='" + id + "' ORDER BY [EMPLOYEE] DESC";
                    SqlDataSource1.UpdateCommand = "UPDATE table1 SET FIRST_NAME=@FIRST_NAME, LAST_NAME=@LAST_NAME, "
                    + "TITLE=@TITLE, DATE_HIRED=@DATE_HIRED, WK_PHONE_NBR=@WK_PHONE_NBR, WK_PHONE_EXT=@WK_PHONE_EXT, "
                    + "EMAIL_ADDRESS=@EMAIL_ADDRESS, DEPARTMENT=@DEPARTMENT, PROCESS_LEVEL=@PROCESS_LEVEL, CELL_PHONES=@CELL_PHONES, FAX_NUM=@FAX_NUM "
                    + "WHERE (EMPLOYEE = @EMPLOYEE)";
                    SqlDataSource1.DeleteCommand = "DELETE FROM table1 WHERE EMPLOYEE = @EMPLOYEE";
                }

            }
            catch (Exception ex)
            {
                SqlDataSource1.SelectCommand = "SELECT * FROM [table1]";
                //ApplicantsSqlDataSource.UpdateCommand = "";
                //ApplicantsSqlDataSource.DeleteCommand = "";
                GridView1.Visible = false;
                NoResults.Text = "<p>Sorry, there are no results that match your search query.<br />" + ex + "</p>";
            }

尝试捕获时遇到问题

您需要在部门区域的SelectCommand中添加百分号。

遵循正确的形式:SqlDataSource1.SelectCommand = "SELECT * FROM [table2] " + "WHERE Department_Name LIKE '%" + id + "%' ORDER BY [Department_Name] DESC";