Hi all,
Check this code and please tell me a solution.I have 2 buttons (Pizza & pepsi),the price should display in textbox8 when i click on button, here the price had shown in Textbox8,but it is not getting added with next price when i click second button.

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        con.ConnectionString = str
        TextBox8.Text = 0.0
        adp = New SqlDataAdapter("select Item,Price from Bill where Id = '1'", con)
        adp.Fill(ds)
        DataGridView1.DataSource = ds.Tables(0)
        TextBox8.Text = ds.Tables(0).Rows(0)("Price") + TextBox8.Text -->
    End Sub

Recommended Answers

All 4 Replies

If each time you call this button, you set the text value to 0, the old value is lost.

I would suggest to remove the line

TextBox8.Text = 0.0

from there and move it in an outer initializer sub.

Hope this helps

Thanks lolafuertes,
the value is not getting added with last one,instead it is showing like
(in textbox8) 75.00 75.00 75.00

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        con.ConnectionString = str
        adp = New SqlDataAdapter("select Count,Item,Price from Bill where Id = '1'", con)
        adp.Fill(ds)
        DataGridView1.DataSource = ds.Tables(0)
        TextBox8.Text = Convert.ToString(ds.Tables(0).Rows(0)("Price")) + TextBox8.Text
    End Sub
TextBox8.Text += Convert.ToDouble(ds.Tables(0).Rows(0)("Price"))

will do the trick

Because TextBox8.Text is just a string you should convert to some kind of numeric (like double) to add it to another value like:

TextBox8.Text = String.Format("{0:C2}",CCur(ds.Tables(0).Rows(0)("Price")) + CCur(TextBox8.Text))

This will format the result as a currency, with 2 decimals. Each operand will be converted to currency before being added together.

Hope this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.