VB.NET divide number from SQL

I'm not a programmer, I have very little experience doing such

I have a program I'm developing to aid me at work.  I want to use it to display information gathered on users so when they call me I can quickly pull up useful information.  My current dilemma is taking a number (users memory in bytes) and dividing it by 1024 twice or even three times.  I can't seem to figure out how to do this.  Attached is my code and the "i" and "math" are the variables I'm working with.
Private Sub moreBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles moreBtn.Click
        ' create a new SqlConnection object with the appropriate connection string
        Dim sqlConn As New SqlConnection("server=blah;uid=blah;pwd=blah;database=logoninfo;") 'OleDbConnection i
        ' open the connection
        sqlConn.Open()
        'do some operations here...
        'query sql
        Dim sqlCommData As New SqlCommand("SELECT ontime,inorout,compname,ip,data01,data02 FROM loginout WHERE username='" & usernameLabel.Text & "' ORDER BY ontime DESC", sqlConn)
        'create a binding source for the data
        Dim dataBind As New BindingSource
        'run the sql command from above
        Dim dataRead As SqlDataReader = sqlCommData.ExecuteReader()
        'add the data to the binding source
        'convert Byte memory to MB
        Dim i As Integer
        i = dataRead("data02")
        Dim math
        math = (i / 1024)
        i = (math / 1024)
        'set mem label equal to amount of memory
        memLabel.text = i
        dataBind.DataSource = dataRead
        'attach the binding source to the datagrid to display the data
        DataGrid01.DataSource = dataBind
        'close the reader
        dataRead.Close()
        'close the connection
        sqlConn.Close()
    End Sub

Open in new window

Snyper82Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DhaestCommented:
Convert Bytes to Megabytes
http://www.freevbcode.com/ShowCode.Asp?ID=48
Public Function BytesToMegabytes(Bytes As Double) As Double
   'This function gives an estimate to two decimal
   'places.  For a more precise answer, format to
   'more decimal places or just return dblAns
 
  Dim dblAns As Double
  dblAns = (Bytes / 1024) / 1024
  BytesToMegabytes = Format(dblAns, "###,###,##0.00")
  
End Function

Open in new window

0
Snyper82Author Commented:
ok, how to I do that with a field from SQL?
If I wanted to take data02 (sql data) and divide it how would I?
0
DhaestCommented:
How is the data stored in your database ? What type is the field ?


Dim input As double
dim output as double
input = dataRead("data02")
output = BytesToMegabytes(input)
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Snyper82Author Commented:
so now that I've done the attached, I get a pop-up on my program the has the words "data02" in it.
it must not be seeing the data as a number.

the field is a varchar 100



'Query sql for Memory size
                Dim sqlCommMemRead As New SqlCommand("SELECT TOP 1 data02 FROM loginout WHERE username='" & usernameLabel.Text & "' and inorout='IN' ORDER BY ontime DESC", sqlConn1)
                'run the query
                Dim memRead As SqlDataReader = sqlCommTimeRead.ExecuteReader()
                'convert Byte memory to MB
                Dim input As Double
                Dim output As Double
                'populate the input
                input = memRead("data02")
                'run the "Bytes to Megabytes" function (below)
                output = BytesToMegabytes(input)
                'set mem label equal to amount of memory
                memLabel.Text = output & "MB"
                'close the sql connection
                sqlConn1.Close()

Open in new window

0
DhaestCommented:
sqlConn1

I see, you are using a sqldatareader, try using a executeScalar-function ,like this:

'Query sql for Memory size
                Dim sqlCommMemRead As New SqlCommand("SELECT TOP 1 data02 FROM loginout WHERE username='" & usernameLabel.Text & "' and inorout='IN' ORDER BY ontime DESC", sqlConn1)
                'run the query

                'convert Byte memory to MB
                Dim input As Double
                Dim output As Double

                'populate the input
                input = Convert.ToInt32(cmd.ExecuteScalar())

                'run the "Bytes to Megabytes" function (below)

                output = BytesToMegabytes(input)

                'set mem label equal to amount of memory
                memLabel.Text = output & "MB"

                'close the sql connection
                sqlConn1.Close()

Using sqlConn1 As New SqlConnection(connString)
        Dim sqlCommMemRead As New SqlCommand("SELECT TOP 1 data02 FROM loginout WHERE username='" & usernameLabel.Text & "' and inorout='IN' ORDER BY ontime DESC", sqlConn1)

        Try
            conn.Open()
            memLabel.Text = BytesToMegabytes(Convert.ToInt32(cmd.ExecuteScalar())) & "MB"
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Using

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Snyper82Author Commented:
input = Convert.ToInt32(sqlCommMemRead.ExecuteScalar())
did the trick
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.