Something's wrong with this code...
...but I don't know what it is. It's syntactically correct, passes all compiler tests and all that, but returns no results. It's supposed display all jobs in the jobs table mathcing the user's search. it just displays the header od the datagrid.
<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">
void Page_Load()
{
if (Page.IsPostBack)
{
lblMessage.Text="Your search results...";
}
else
{
lblMessage.Text="";
}
}
void Search_Click(object sender, EventArgs e)
{
string Title = txtTitle.Text;
string Location = txtLocation.Text;
string Type="Permanent";
DataGrid2.DataSource=ListJobs(Title,Location,Type) ;
DataGrid2.DataBind();
}
System.Data.DataSet ListJobs(string Title, string Location, string Type)
{
string connectionString = "server=\'(local)\'; trusted_connection=true; database=\'nkonye_test\'";
//string connectionString = ConfigurationSettings.AppSettings("ConnectionStrin g");
System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(connectionStri ng);
string queryString = "SELECT [Jobs].* FROM [Jobs]" +
" WHERE [Jobs].[job_title] LIKE '%@Title%' " +
" OR [Jobs].[location] LIKE '%@Location%' " +
" OR [Jobs].[job_type] LIKE '%@Type%'" ;
System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
dbCommand.CommandText = queryString;
dbCommand.Connection = dbConnection;
//Title
SqlParameter Jtitle = new SqlParameter("@Title",SqlDbType.VarChar,250);
Jtitle.Value=Title;
dbCommand.Parameters.Add(Jtitle);
//Location
SqlParameter JLoc = new SqlParameter("@Location",SqlDbType.VarChar,50);
JLoc.Value=Location;
dbCommand.Parameters.Add(JLoc);
//job type
SqlParameter JType = new SqlParameter("@Type",SqlDbType.VarChar, 50);
JType.Value = Type;
dbCommand.Parameters.Add(JType);
// Response.Write(Title);
// Response.Write("<br />");
// Response.Write(Location);
// Response.Write("<br />");
// Response.Write(Type);
// Response.End();
System.Data.IDbDataAdapter dataAdapter = new System.Data.SqlClient.SqlDataAdapter();
dataAdapter.SelectCommand = dbCommand;
System.Data.DataSet dataSet = new System.Data.DataSet();
dataAdapter.Fill(dataSet);
return dataSet;
}
</script>
<html>
<head>
</head>
<body>
<form action="search.aspx" method="post" runat="server">
<table cellpadding="0" width="750" align="center" cellpsacing="0">
<tbody>
<tr>
<td align="middle" colspan="2">
<a href="list.aspx">List</a> | Search | <a href="index.aspx">Log out</a>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td colspan="2">
<h2>Search jobs
</h2>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
Job title:</td>
<td>
<asp:textbox id="txtTitle" runat="server" size="45px"></asp:textbox>
</td>
</tr>
<tr>
<td>
Location:</td>
<td>
<asp:textbox id="txtLocation" runat="server" size="20px"></asp:textbox>
</td>
</tr>
<tr>
<td>
Type:</td>
<td>
<asp:dropdownlist id="Location" runat="server">
<asp:listitem id="Permanent">Permanent</asp:listitem>
<asp:listitem id="Contract">Contract</asp:listitem>
</asp:dropdownlist>
</td>
</tr>
<tr>
<td colspan="2">
<asp:button id="Search" onclick="Search_Click" runat="server" Text="Search"></asp:button>
<asp:button id="clear" runat="server" Text="Clear"></asp:button>
</td>
</tr>
</tbody>
</table>
<br />
<br />
<h3><asp:Label runat="server" id="lblMessage"></asp:Label>
</h3>
<br />
<asp:DataGrid id="DataGrid2" runat="server" Autogeneratecolumns="False" align="center" width="650px" ForeColor="Black" CellPadding="4" BackColor="White" BorderColor="#DEDFDE" BorderWidth="1px" GridLines="Vertical" BorderStyle="None">
<FooterStyle backcolor="#CCCC99"></FooterStyle>
<HeaderStyle font-bold="True" forecolor="White" backcolor="#6B696B"></HeaderStyle>
<PagerStyle horizontalalign="Right" forecolor="Black" backcolor="#F7F7DE" mode="NumericPages"></PagerStyle>
<SelectedItemStyle font-bold="True" forecolor="White" backcolor="#CE5D5A"></SelectedItemStyle>
<AlternatingItemStyle backcolor="White"></AlternatingItemStyle>
<ItemStyle backcolor="#F7F7DE"></ItemStyle>
<Columns>
<asp:BoundColumn DataField="job_id" HeaderText="ID" ReadOnly="True" />
<asp:BoundColumn DataField="job_title" HeaderText="Title"></asp:BoundColumn>
<asp:BoundColumn DataField="location" HeaderText="Location"></asp:BoundColumn>
<asp:BoundColumn DataField="job_type" HeaderText="Job Type"></asp:BoundColumn>
<asp:BoundColumn DataField="salary" HeaderText="Salary" DataFormatString="{0:c}"></asp:BoundColumn>
<asp:BoundColumn DataField="job_type" HeaderText="Type"></asp:BoundColumn>
<asp:templatecolumn>
<itemtemplate>
<a href='edit_job.aspx?job_id=<%# DataBinder.Eval ( Container.DataItem, "job_id") %>' > Edit</a>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn>
<itemtemplate>
<a href='delete_job.aspx?job_id=<%# DataBinder.Eval ( Container.DataItem, "job_id") %>' > Delete</a>
</itemtemplate>
</asp:templatecolumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
|