I have a asp:GridView
set up on the client side like so:
<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>
Then in the code behind I set the DataKeyNames
and the asp:SQLDataSource
arguments inside of try/catch statements. Even when I comment out the first try catch I cannot get the second one to fire.
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>";
}
}
}
here is the click event
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>";
}
}
it should work but it doesnt
Edited here is the first try catch that is working that was originally left out
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>";
}
You need to add the percent sign in the SelectCommand
of the Department area.
Follow the correct form : SqlDataSource1.SelectCommand = "SELECT * FROM [table2] " + "WHERE Department_Name LIKE '%" + id + "%' ORDER BY [Department_Name] DESC";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.