Novice developer here, seeking advice.
I am writing a simple bulletin board and this is my first ASP project. When my main page loads, it displays the latest thirty threads inside of a repeater control. Above the repeater control there is a search-box where users can search all threads (not just the thirty shown) for a specific term. Unfortunately when they click the search button however, the repeater control just disappears. The desired behavior is obviously for the table to refresh itself and bring back results that are relevant to the search terms given.
C# code behind on button:
protected void customSearchButton_Click(object sender, EventArgs e)
{
string titleSearch = customSearchTextBox.Text;
SqlConnection conn;
SqlCommand comm;
SqlDataAdapter myCommand;
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand("SELECT * FROM Threads WHERE [AdvertTitle] LIKE '%" + titleSearch + "%' and [ThreadDeleted] = 'No'", conn);
myCommand = new SqlDataAdapter("SELECT * FROM Threads WHERE [AdvertTitle] LIKE '%" + titleSearch + "%' and [ThreadDeleted] = 'No'", conn);
DataSet ds = new DataSet();
myCommand.Fill(ds);
try
{
//if (IsPostBack)
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
Repeater4.DataSource = ds;
Repeater4.DataBind();
}
catch (Exception ex)
{
Response.Write(@"<SCRIPT LANGUAGE=""JavaScript"">alert('" + "Message:" + ex.Message + "')</SCRIPT>");
}
finally
{
conn.Close();
}
}
And the corresponding code on the *.*aspx page itself:
<asp:TextBox ID="customSearchTextBox" runat="server"></asp:TextBox><p></p>
<asp:Button ID="customSearchButton" runat="server" Text="Search Adverts!" OnClick="customSearchButton_Click" /><p></p>
<asp:UpdatePanel runat="server" ID="UpdatePanel" UpdateMode="Conditional">
<ContentTemplate>
<asp:Repeater ID="Repeater4" runat="server" DataSourceID="SqlDataSource4">
<HeaderTemplate>
<table id="w3TableSearch" class="w3TableSearch"><tr class="w3TableSearch">
<th style="width:140px;" class="w3TableSearch">Advert Type</th><th style="width:auto;" class="w3TableSearch">Advert Title</th><th style="width:auto;" class="w3TableSearch">Posted By</th><th style="width:auto;" class="w3TableSearch">Post Created</th></tr>
</HeaderTemplate>
<ItemTemplate>
<tr class="w3TableSearch"><td class="w3TableSearch"><%# Eval("AdvertType") %></td><td class="w3TableSearch"><b><a href="viewThread.aspx?id=<%# Eval("Ad_ID") %>"><%# Eval("AdvertTitle") %></b></a></td><td class="w3TableSearch"><%# Eval("AdvertiserName") %></td><td class="w3TableSearch"><%# Eval("PostCreationDateTime", "{0:d/M/yyyy <i> hh:mm:ss tt}") %></td></tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater></ContentTemplate>
</asp:UpdatePanel>
The issue is solved!
I posted this same question on another forum and I received a very helpful response, which presented me with a solution. The problem with my code was that I had set both the DataSource and DataSourceID to Repeater4. This was causing an exception as the data was not bound to the repeater when the button was pressed.
The following example code was provided to me for my C# code behind. I used this example to solve my issue and hopefully it'll assist someone else in the future. Don't forget to remove the data source from the repeater tag on the *.*aspx page too!
protected void Page_Load(object sender, EventArgs e)
{
string titleSearch = customSearchTextBox.Text;
SqlConnection conn;
SqlDataAdapter myCommand;
string connectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString2"].ConnectionString;
conn = new SqlConnection(connectionString);
conn.Open();
myCommand = new SqlDataAdapter("SELECT top 30 * FROM Bulletin ", conn);
DataSet ds = new DataSet();
myCommand.Fill(ds);
Repeater4.DataSource = ds;
Repeater4.DataBind();
conn.Close();
}
protected void customSearchButton_Click(object sender, EventArgs e)
{
string titleSearch = customSearchTextBox.Text;
SqlConnection conn;
SqlDataAdapter myCommand;
string connectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString2"].ConnectionString;
conn = new SqlConnection(connectionString);
try
{
conn.Open();
myCommand = new SqlDataAdapter("SELECT * FROM Bulletin WHERE [AdvertTitle] LIKE '%" + titleSearch + "%' ", conn);
DataSet ds = new DataSet();
myCommand.Fill(ds);
Repeater4.DataSource = ds;
Repeater4.DataBind();
}
catch (Exception ex)
{
Response.Write(@"<SCRIPT LANGUAGE=""JavaScript"">alert('" + "Message:" + ex.Message + "')</SCRIPT>");
}
finally
{
conn.Close();
}
}