Results 1 to 2 of 2

Thread: [2005] How to get a one cell value without datagridview?

  1. #1

    Thread Starter
    Addicted Member Genom's Avatar
    Join Date
    May 2006
    Posts
    186

    [2005] How to get a one cell value without datagridview?

    Hi,

    I want to make a query with SUM function in SQL. Ill get a value not a table. So I dont want to use a datagridview for it. Is it possible to get this value?

    I make the query with this code normally:
    Code:
            Dim DS As New DataSet
            Dim OL As New SqlClient.SqlDataAdapter("", CN)
    
    
            DGW.DataSource = Nothing
            DS.Tables.Clear()
            DS.DataSetName = "NewDataSet"
    
    
            CN.Close()
    
            Connect(UserName, Password, DBPlace)
            OL.SelectCommand.CommandText = Command 'This String parameter is being taken in the sub
    
            DS.Tables.Add(Table) 'Table string parameter is being taken too
            OL.Fill(DS.Tables(Table))
            DS.DataSetName = CN.ConnectionString
            DGW.DataSource = DS.Tables(Table)

    Thanks for any help...
    Dim Me As Coder

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] How to get a one cell value without datagridview?

    To get the sum of some field in a database table, all you need is a command object and call the ExecuteScalar function of the command object. This will return an object and you will cast it to the proper type (i.e Decimal, Double, Integer... depending on what data type the field is). Something like this (coding from memory... Not tested)
    Code:
    'You'll need to change the sql string have the correct field name, table name and predicates (if needed)
    Dim sql As String = "Select Sum(field_Name_Here) As TotalValue From table_Name_Here Where predicates_here"
    Dim cmd As New SqlClient.SqlCommand()
    cmd.Connection = CN
    cmd.CommantText = sql
    CN.Open()
    Dim total As Decimal = Convert.ToDecimal(cmd.ExecuteScalar)
    CN.Close()
    Upon executing this code, the variable total will have the sum that you after.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width