Click here to Skip to main content
15,923,197 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi
I have som products in a datalist with a textbox. When you enter a number in the textbox and you click on the button it could make a sum on the products price with how many you orderd.

How does I get the value from my database and + it with the value in the textbox??

I have try this:

Frontend

C#
 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [delikatesse]"></asp:SqlDataSource>
            
            
           <div id="ekstra_vare">
                <asp:DataList ID="VisDeli" runat="server" RepeatLayout="Table"
RepeatColumns="4" DataSourceID="SqlDataSource1" OnItemDataBound="VisDeli_ItemDataBound" CellSpacing="5" CssClass="tabel1" >
                    
                    <HeaderTemplate>
                        <p class="overskrift_bestil">Vælg tilbehør</p>
                    </HeaderTemplate>
                    
                    <ItemTemplate>
                 
                          <td><asp:TextBox ID="TextBox_deli" runat="server" Width="15" Height="15"></asp:TextBox></td>
                          <td><p><%#Eval("deli_navn") %></p>
                              <td><p><a class="email" href="#emailpopup?deli_id=">info</a></p></td>

                
                             
            </div>
                               
             

                          </td>
                    
                       
                    </ItemTemplate>
                </asp:DataList>

</div>
            <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [kurve] WHERE kurve_id = 1"></asp:SqlDataSource>
       
            <div id="ekstra_pris">
                <asp:Repeater ID="Repeater2" runat="server" DataSourceID="SqlDataSource3">
                    <ItemTemplate>
                        <%#Eval("pris") %> kr.
                    </ItemTemplate>
                </asp:Repeater>
                <asp:Label ID="Label_Vispris" runat="server" Text=""></asp:Label>
                <asp:Button ID="Button1" runat="server" Text="Opdater" OnClick="Button1_Click" />
                 </div>      


Backend:

C#
protected void Button1_Click(object sender, EventArgs e)
   {
       int totalvaerdi = 0;

       List<ValgtDeli> valgteDeli = new List<ValgtDeli>(); //Her laver jeg en liste over valgte frugter. Det er et nyt objekt, som kan indeholde påde ID, Navn, Antal og Værdi



       foreach (DataListItem item in VisDeli.Items)
       {
           if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
           {
               TextBox Textbox_antal = item.FindControl("TextBox_deli") as TextBox;


               int pris = 149;

               int prisVare = ;

               int enkeltFrugtAntal = int.Parse(Textbox_antal.Text);


               totalvaerdi = pris + enkeltFrugtAntal;


           }

       }
       if (totalvaerdi > 0)
       {

           Label_Vispris.Text = Convert.ToString(totalvaerdi);
       }



   }
Posted
Comments
Sergey Alexandrovich Kryukov 27-Oct-13 15:06pm    
And what's the problem?
—SA
tina_overgaard 27-Oct-13 15:40pm    
I need to get the price from the database
Sergey Alexandrovich Kryukov 27-Oct-13 15:50pm    
And what's the problem of doing so, technically speaking?
—SA
tina_overgaard 27-Oct-13 15:53pm    
int prisVare = How do I get the price from the database here?

So if I choose 2pcs of Nuts with ID 1 and presse the button it will 2 * price of nuts with ID 1

tina_overgaard 27-Oct-13 17:11pm    
I got it to work

1 solution

may be you get some help from this...

please change in the Select Command of the asp:SqlDataSource ..from this query you get the three column
First is 'CountOfProduct',second is 'PriceOfProduct',third is 'TotalPricce'...

now which data is display in this column
In the CountOfProduct : display count of row which is related to kurve_id in short suppose your kurve_id is 3, then in the database there are five product is available for this kurve_id, then CountOfProduct = 5..

in the >PriceOfProduct : display the Price of Product which is related to kurve_id which you entered and this price is same for the all product and it's kurve_id you select, Suppose for the Product Price =10 of the kurve_id =3 then PriceOfProduct = 10

In the TotalPricce : you get the total price of the Product related to kurve_id which you select.

SelectCommand="SELECT COUNT(*)AS CountOfProduct,PriceOfProduct,((COUNT(*))*(PriceOfProduct))AS TotalPricce FROM [kurve] WHERE kurve_id = 1 GROUP BY PriceOfProduct"...


And Now You Can Use "TotalPricce" Where You Want ....

Hope Fully You got the Solution From this ...
Best Of Luck....
 
Share this answer
 

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