Hi everyone, I'm new here and is in need of some help.
I'm trying to complete my project, however I'm stuck at writing the data from the sql into the vb.net/asp.net gridview.
Was never good at database to start with and I had never learnt how to query from VB.NET.
Answers are truly appreciated :)

Btw, I got this code to connect to the database from online thread and add on my own code into it.
msgbox : Object reference not set to an instance of an object

    Dim con As SqlConnection = New SqlConnection("Data Source=BHI88QP0M1\SERVER2012;Integrated Security=True")
    Dim query As String = "SELECT Job_Status FROM rep WHERE Job_Start_Date = '2013-12-02' AND Job_Start_Time = '07:00:02'"
    Dim cmd As SqlCommand
    Dim reader As SqlDataReader


    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        Try
            With cmd
                .Connection = con
                .CommandText = query
            End With

            reader = cmd.ExecuteReader()

            Do While reader.Read()



                Dim a As Integer

                For a = 1 To 31

                    If query = "Finished" Then  'query -> don't know how to call back 
                        serverStat.Rows(0).Cells(a).BackColor = Drawing.Color.DarkBlue
                    Else

                        serverStat.Rows(0).Cells(a).BackColor = Drawing.Color.Red
                    End If

                Next

            Loop

            reader.Close()
            con.Dispose()
            con.Close()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

//if you want to show the data to gridview means?

Try
With cmd
.Connection = con
.CommandText = query
End With
Dim da As New SqlDataAdapter(query,con)
Dim dt as DataTable
da.Fill(dt)
GridView1.DataSource=dt
GridView1.DataBind()

'You may try this..

1.         Dim con As SqlConnection = New SqlConnection("Data Source=BHI88QP0M1\SERVER2012;Integrated Security=True")
2.         Dim query As String = "SELECT Job_Status FROM rep WHERE Job_Start_Date = '2013-12-02' AND Job_Start_Time = '07:00:02'"
3.         Dim cmd As SqlCommand
4.         Dim reader As SqlDataReader
5.         Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
6.             Try
7.              With cmd
8.                  .Connection = con
9.                  .CommandText = query
10.                 End With
11.         Dim da As New SqlDataAdapter(query,con)
12.         Dim dt as DataTable
13.         da.Fill(dt)
14.         GridView1.DataSource=dt
15.         reader.Close()
16.         con.Dispose()
17.         con.Close()
18.             Catch ex As Exception
19.                 MsgBox(ex.Message)
20.             End Try

Edited 2 Years Ago by Danuja

This article has been dead for over six months. Start a new discussion instead.