0

I had a problem on displaying count sql in vb. Anyone would help me? I had google about this, and I had to use executereader but I don't know how to use it

Dim dso1 As Label = CType(e.Item.FindControl("dso1"), Label)
        If Not IsNothing(dso1) Then
            Dim name As String

            name = dr.Item("alert_id")

            If (name = "DSO") Then

                strSQL = "SELECT COUNT(alert_value) AS alert1 "
                strSQL = strSQL & "FROM company_alert a "
                strSQL = strSQL & "LEFT JOIN ref_alert_parameter b ON a.alert_id = b.alert_id "
                strSQL = strSQL & "WHERE b.alert_id = 'DSO' And alert_value >= para1 "


                'dso1.Text = dr.Item("alert1")
                dso1.Text = e.Item.DataItem("alert1").ToString
                dso1.BackColor = Drawing.Color.Red
3
Contributors
11
Replies
14
Views
6 Years
Discussion Span
Last Post by Pgmer
0

What count you want to show? Where is the execute reader in your code here?
This half code does not explain anything...

0

I want to show count for the alert value. I don't know how to use the execute reader, that's why I'm asking it here

0
Try
            Dim cn As SqlConnection
            Dim strCnn As String = "Your data base connection string"
            cn = New SqlConnection(strCnn)
            cn.Open()
            Dim comm As New SqlCommand("Your sp or Command text to get the data from DB", cn)
            '' If ur using Sp then Commandtype= CommandType.StoredProcedure if it is Text then comm.CommandType=CommandType.Text
            comm.CommandType = CommandType.StoredProcedure
            Dim ds As SqlDataReader
            ds = comm.ExecuteReader
            If ds.HasRows Then
                While ds.Read
                    ''
                    ' do what u want here using the data
                End While
            End If

            ''Close your connections and commands.
            cn.Close()
        Catch ex As Exception
            ''Handle error if any
        End Try
0

thank you pgmer, i'm using that in my code, my probs is just to display the count sql. i had try using

If (name = "DSO") Then
 
                strSQL = "SELECT COUNT(alert_value) AS alert1 "
                strSQL = strSQL & "FROM company_alert a "
                strSQL = strSQL & "LEFT JOIN ref_alert_parameter b ON a.alert_id = b.alert_id "
                strSQL = strSQL & "WHERE b.alert_id = 'DSO' And alert_value >= para1 "
 
 
                dso1.Text = e.Item.DataItem("alert1").ToString

alert1 to display the count, but it seems nothing come out..

0

What is e?
In your code you are not showing a connection or a datareader.
Pgmer's code will work just fine if you replace connection string and the command.

In order to get the count into dso1 replace

ds = comm.ExecuteReader

with

dso1.text = comm.ExecuteScalar

(You will also have to remove the IF and the While)

0

I have been adding that, but it seems nothing was coming out..

Dim dsola As String

                dsola = "SELECT COUNT(alert_value) AS alert1 FROM company_alert a LEFT JOIN ref_alert_parameter b ON a.alert_id = b.alert_id WHERE b.alert_id = 'DSO' And alert_value >= para1 "
                cmd = New MySqlCommand(dsola, conn)
                dr = cmd.ExecuteReader()

                If dr.Read() Then
                    dso1.Text = dr.Item("alert1")
                End If

                dr.Close()
0

If you run ur Query before cmd.ExecuteReader() on SQL server are u getting any data?

0

replace this:

dr = cmd.ExecuteReader()

                If dr.Read() Then
                    dso1.Text = dr.Item("alert1")
                End If

with this:

dso1.text = cmd.ExecuteScalar

Also make sure that your connection string is correct.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.