Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to change the Label text in my DataList. I want to check whether the product stock is 0 or not, if the stock is large from 0 to notify: Available in stock, if the stock is 0 to notify: Not available stock.

My code is:
C#
int stock = 0;
      OleDbConnection con1 = new OleDbConnection();
      con1.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("") + "\\Database1.accdb";
      con1.Open();
      string sqlstring2Stock = "select * from ProductsTable where ProductModel like '%" + search.Text + "%' or ProductManufacturer like '%" + search.Text + "%' or ProductManufacturer + ' ' + ProductModel  like '%" +         search.Text + "%' order by CatalogNumber";
      OleDbCommand Cmd22 = new OleDbCommand(sqlstring2Stock, con1);
      OleDbDataReader d1r = Cmd22.ExecuteReader();
      d1r.Read();
      stock = int.Parse(d1r["ProductStock"].ToString());
      con1.Close();

      if (stock <= 0)
      {
          LabelAvailable.Text = "Not available";
      }
      else
      {
          LabelAvailable.Text = "Available";
      }



My DataList is:
ASP.NET
<asp:DataList ID="DataList1" runat="server"  DataKeyField="ProductModel" RepeatDirection="Horizontal" 
        RepeatColumns="5" Width="100%">
        <ItemTemplate>
         <td align="center" >
                          <img src="<%# DataBinder.Eval(Container.DataItem, "ProductImage")%>" height='120' width='150' /><br />
                          <%# DataBinder.Eval(Container.DataItem, "ProductManufacturer")%> 
                          <%# DataBinder.Eval(Container.DataItem, "ProductModel")%><br />                    
                         <%# DataBinder.Eval(Container.DataItem, "Price")%> $<%--<br />--%>
                         <asp:Label ID="LabelAvailable" runat="server" Text="Label"></asp:Label><br />
                          <a href="ShowProductSearch.aspx?Model=<%# DataBinder.Eval(Container.DataItem, "ProductModel")%>">For details
</a><br />
                          <asp:Button ID="AddToCart" runat="server" Text="Add To Cart" />
                          </td>
        </ItemTemplate>
        </asp:DataList>




Thanks for your help!!!
Posted
Comments
ZurdoDev 18-Jan-16 8:08am    
Use a CASE statement.
Amit Ben Zur 19-Jan-16 8:53am    
Can you explain to me how I use it?
Where am I writing this?
ZurdoDev 19-Jan-16 9:01am    
In your SQL.
Amit Ben Zur 19-Jan-16 10:21am    
I am using access, I read that on access the usage is different
Maciej Los 19-Jan-16 16:40pm    
Don't you see errors in code? For example:
stock = int.Parse(d1r["ProductStock"].ToString());
//later
if (stock <= 0)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900