Sub filllist()


strsql = "select * from tabletest"

ListView1.Items.Clear()

sqlcmd.CommandText = strsql
sqlcmd.Connection = sqlconn
sqldr = sqlcmd.ExecuteReader()
sqlda.SelectCommand = sqlcmd -- the error is pointing here and it say NullReferenceException was handled then Object reference not set to an instance of an object. How do i solve this?

I'm trying to view in the listbox if the data i enter did store in my sql server. please help.


While (sqldr.Read())

With ListView1.Items.Add(sqldr("Id"))
.subitems.add(sqldr("Name"))
.subitems.add(sqldr("Adress"))
.subitems.add(sqldr("Age"))
End With
End While

sqldr.Close()

End Sub

sqlda.SelectCommand = sqlcmd -- the error is pointing here

How did you declared sqlda and salcmd?
there should be in your declaration Dim sqlda as New ...... and Dim sqlcmd as New ....

I put it in a module.

Imports System.Data.SqlClient
Module Module1

Public sqlconn As New SqlConnection


Public sqlcmd As New SqlCommand

Public sqldr As SqlDataReader
Public strsql As String


Public sqlda As SqlDataAdapter

Sub connect()

If sqlconn.State = ConnectionState.Open Then sqlconn.Close()

sqlconn.ConnectionString = "data source=.\sqlexpress; integrated security=true; attachdbfilename=|datadirectory|\TESTING.mdf; user instance=true;"
sqlconn.Open()

End Sub

End Module

I put it in a module.

Imports System.Data.SqlClient
Module Module1

Public sqlconn As New SqlConnection


Public sqlcmd As New SqlCommand

Public sqldr As SqlDataReader
Public strsql As String


Public sqlda As SqlDataAdapter

Sub connect()

If sqlconn.State = ConnectionState.Open Then sqlconn.Close()

sqlconn.ConnectionString = "data source=.\sqlexpress; integrated security=true; attachdbfilename=|datadirectory|\TESTING.mdf; user instance=true;"
sqlconn.Open()

End Sub

End Module

The text that is in bold characters is where the issue is, as samir said, declare them as New variables.

Public sqldr As New SqlDataReader
Public sqlda As New SqlDataAdapter

I did that and it now works :D BUT now I have another problem, now it's pointing to the .subitems.add(sqldr("Adress"))
It says that index out of range exception was unhandled.

i tried declaring sqldr as new sqldatareader but it won't let me, it says that sqldr

'System.Data.SqlClient.SqlDataReader.Friend Sub New(command As System.Data.SqlClient.SqlCommand, behavior As System.Data.CommandBehavior)' is not accessible in this context because it is 'Friend'.

Helppp ppleeeasssee T.T

Use 'New' Keyword

Private Class MyDAta
'----Constructor for class
Dim Myval1 as integer
Public sub New(Byval A as integer, Byval B as integer)
If A>B Then
Myval1 = A
Else
Myval = B
End if
End Sub

End Class

Dim A as MyDAta <User defined class>

Private sub Button1_click()handle Button1.click

A = New MyDAta(3,5)
<You cannot access A.Myval1 since it is valid inside the class only... use public property or declare it as public variables to make it visible throughout the functions>

Your Code _
___________
___________
___________
>
End sub

oh ok then, i'll try and do that. Thank You :D

Hello again! :d

does anyone know how to store option value in sqldatabase? Thank you.
Answers are very much appreciated! :d

If ComboBox1.Text = "" Then
        MsgBox("enter logintype")
    ElseIf txtname.Text = "" Then
        MsgBox("enter loginname")
    ElseIf txtpass.Text = "" Then
        MsgBox("enter loginpass")
    Else
        connstr = "Data Source=.;Initial Catalog=inventory;Integrated Security=True"
        query = "select * from regis where logintype='" & ComboBox1.Text & "' and loginname= '" & txtname.Text & "' "
        If rs.State = 1 Then rs.Close()
        conn.Open()
        cmd = New SqlCommand(query, conn)
        If rs.EOF = False Then
            MsgBox("Record exist")
        Else
            query = "insert into regis (logintype,loginname,loginpass)"
            query = query & "values('" & ComboBox1.Text & "','" & txtname.Text & " ','" & txtpass.Text & "')"
            count = cmd.ExecuteNonQuery
            MsgBox("registered successfully")
            clear()
            loadGrid()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.