none
I don't know the code! :( RRS feed

  • Question

  • Greetings everyone,

    I'm completely new to VB.NET, and I need to make a project: I need to find a way that when I press a specific button, it goes and fetches the content of a database cell (I have it on SQL Server 17 and connected on VS) and pastes it unto a button text(serving as textbox), so then I can use it to calculate.

    (It's a store's calculator: I program button X to a value, when a costumer asks for that product and/or more, we just press that key: e.g milk 3€ and 3,00 appears on textbox, then I press button Y for cereals and it gives 1,00€ on screen, press equal button shows 4,00.) 

    I've google alot and I can find barely anything similiar, just query and query and query. Can I get a startup on what tags catch content? And how to make those catch content from a DB.

    Tried stuff like 

      Private Sub Cinz11_Click(sender As Object, e As EventArgs) Handles Cinz11.Click
    
            Dim SQL As String = "SELECT 1 FROM bcinz1 WHERE 1 IS NOT NULL;"
            While Dim reader As SqlDataReader = String
            RCT.Text = 0
                RCT.Text = String
    
            End While
    
        End Sub
    From scraps online, but that's not legit the thing, am I neaR?

    Thanks in advance.




    Friday, March 16, 2018 12:37 PM

All replies

  • Hi Fmdsma,

    Thank you for posting here.

    According to your question is more relayed to VB, I will move it to Visual Basic forum for suitable support.

    The CLR Forum discuss and ask questions about .NET Framework Base Classes (BCL) such as Collections, I/O, Regigistry, Globalization, Reflection. Also discuss all the other Microsoft libraries that are built on or extend the .NET Framework, including Managed Extensibility Framework (MEF), Charting Controls, CardSpace, Windows Identity Foundation (WIF), Point of Sale (POS), Transactions.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 20, 2018 1:24 AM
  • The basics are to search by a id or name etc.

    If GetSingleProduct returns Nothing the product was not located, otherwise the data is in the returned Product.

    Public Function GetSingleProduct(ByVal pIdentifier  As Integer) As Product
        Dim prod as Product = Nothing
    
        Using cn As New SqlConnection With 
            {
                .ConnectionString = "Your connection string goes here"
            }
    
            Using cmd As New SqlCommand With 
                {
                    .Connection = cn, 
                    .CommandText = "SELECT Name, Price FROM Products WHERE Id = @Id"
                }
    
                cmd.Parameters.AddWithValue("@Id", pIdentifier)
                cn.Open()
                Dim reader As SqlDataReader = cmd.ExecuteReader()
                If reader.HasRows
                    prod = new Product()
                    reader.Read()
                    prod.Id = pIdentifier
                    prod.Name = reader.GetString(0)
                    prod.Price = reader.GetDecimal(1)
                        
                End If
            End Using
        End Using
    
        return prod
    
    End Function

    Concrete class to return data

    Public Class Product
        Public Property Id() As Integer
        Public Property Name() As String
        Public Property Price() As Decimal
    End Class

    The above can be called in a button click event.

    To configure your connection

    https://www.connectionstrings.com/sql-server/


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Tuesday, March 20, 2018 1:57 AM
    Moderator
  • Karen, minor typo... your parameter names don't match between the query string and the SqlParameter instance.

    It's also worth noting that the OP probably does not want to create and open/close a connection in every button click event.  If they want to generate a button for each item in the table they should do all of that when the form loads and associate the price and any other data with the button instance.  Reading from the database should likely be a one-time operation for all buttons.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, March 20, 2018 11:50 AM
    Moderator
  • Homework or production code?

    Tuesday, March 20, 2018 12:03 PM
  • Karen, minor typo... your parameter names don't match between the query string and the SqlParameter instance.

    It's also worth noting that the OP probably does not want to create and open/close a connection in every button click event.  If they want to generate a button for each item in the table they should do all of that when the form loads and associate the price and any other data with the button instance.  Reading from the database should likely be a one-time operation for all buttons.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Fixed typo.

    In regards to the connection open every button click, I see zero issues with doing this, it takes a split second to perform this operation. This is common place for most data centric applications but thanks for bring it up. 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, March 20, 2018 12:10 PM
    Moderator
  • In regards to the connection open every button click, I see zero issues with doing this, it takes a split second to perform this operation. This is common place for most data centric applications but thanks for bring it up. 

    Ideally it is quick, but that is completely dependent on network conditions and server load.

    The actual application usage should be the deciding factor.  In this case there doesn't appear to be any quantity information in the program.  If the user wants 3 cartons of milk they may press the button three times.  That's three round trips to the db server to retrieve the exact same data each time.  Or if the user wants one of everything, they click all the buttons and now you have multiple consecutive db connections, all of which could have been served in a single connection.

    There are certainly many instances where it is fine to do it this way.  But it is worth noting that there are also times when this is not necessarily the most efficient method and it can create unnecessary load on the server.

    The point here was that the developer must asses the common usage scenario(s) for the software and plan the data access accordingly - there is no one-size-fits-all solution.  They should also confer with the DBA to determine the server's average load and how this software will impact server performance.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, March 20, 2018 12:52 PM
    Moderator