when i click the btn assign the error come out where go worng?

      Private Sub btnAssign_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAssign.Click
        ListBoxall.Items.Add(ListBoxassign.SelectedItem)
    End Sub  

(ArgumentNullExceotion was unhandld Value cannot be null.
Parameter name: item)

the admin the listbox done not show up which part go wong is it the code? i wan it like when come out this form it will auto show out the admin

    Private Sub ListBoxassign_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBoxassign.SelectedIndexChanged

        Dim conn As New System.Data.OleDb.OleDbConnection()
        conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\temp\Database1.accdb"

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

        Dim sql As String = "Select * From tbl_info where AdminNo"

        Dim sqlCom As New System.Data.OleDb.OleDbCommand(sql, conn)
        sqlCom.Connection = conn

        Dim sqlReader As System.Data.OleDb.OleDbDataReader = sqlCom.ExecuteReader

        While sqlReader.Read = True

            ListBoxassign.Items.Add(sqlReader.Item("AdminNo").ToString)



        End While
        sqlReader.Close()
        sqlCom.Dispose()
        conn.Close()

        Student_Form.Show()


    End Sub

Recommended Answers

All 6 Replies

Hi

If no item is selected in the ListBoxassign then you will receive a NullReferenceException error. You need to check that an item is selected before trying to use it:

If ListBoxassign.SelectedItem IsNot Nothing Then

HTH

can i know in the listbox how to show out the adminNo

i have think about puting like this so that the adminNO will show out in listbox but have have error

   Private Sub cbbproject_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbproject.SelectedIndexChanged

        Dim conn As New System.Data.OleDb.OleDbConnection()
        conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\temp\Database1.accdb"
        If conn.State = ConnectionState.Open Then conn.Close()
        conn.Open()
        Dim sql As String = "Select [Project Name] As ProNm From tbl_assignment Where ProjectNo ='" & cbbproject.Items(cbbproject.SelectedIndex) & "' AND ProjectNo ='" & cbbproject.Items(cbbproject.SelectedIndex) & "'"
        Dim sqlCom As New System.Data.OleDb.OleDbCommand(sql, conn)
        sqlCom.Connection = conn
        Dim sqlReader As System.Data.OleDb.OleDbDataReader = sqlCom.ExecuteReader()
        If sqlReader.HasRows Then
            sqlReader.Read()
            txtpn.Text = (sqlReader.Item("ProNm").ToString)

        **ListBoxassign.Items.Add(sqlReader.Item("AdminNo").ToString)**    ***[IndexOutOfrangeException was unhandled][AdminNo]]*** **[what that mea]**


       End If
        sqlReader.Close()
        sqlCom.Dispose()
        conn.Close()

    End Sub

Hi

I don't see in your code where you get AdminNo from. The error is basically stating that you are trying to access a field from the DataReader called AdminNo but the DataReader does not have this in it's collection, i.e. it is not in your SELECT statement.

why textbox and listbox dose not show out

    Private Sub cbbproject_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbproject.SelectedIndexChanged

            Dim conn As New System.Data.OleDb.OleDbConnection()
            conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\temp\Database1.accdb"
            If conn.State = ConnectionState.Open Then conn.Close()
            conn.Open()
            Dim sql As String = "Select [Project Name] As ProNm From tbl_assignment Where ProjectNo ='" & cbbproject.Items(cbbproject.SelectedIndex) & "' AND AdminNo ='" & cbbproject.Items(cbbproject.SelectedIndex) & "'"
            Dim sqlCom As New System.Data.OleDb.OleDbCommand(sql, conn)
            sqlCom.Connection = conn
            Dim sqlReader As System.Data.OleDb.OleDbDataReader = sqlCom.ExecuteReader()
            If sqlReader.HasRows Then
                sqlReader.Read()
                txtpn.Text = (sqlReader.Item("ProNm").ToString)
                ListBoxassign.Items.Add(sqlReader.Item("AdminNo").ToString)
            End If
            sqlReader.Close()
            sqlCom.Dispose()

Codes are quite right.
But, you didn't mention the AdminNo in select Statement to get as output result, so how could you get it from sqlReader.

Already you have the AdminNo incbbproject combobox. Take it from there.

If sqlReader.HasRows Then
    sqlReader.Read()
    txtpn.Text = (sqlReader.Item("ProNm").ToString)
    ListBoxassign.Items.Add(cbbproject.Items(cbbproject.SelectedIndex))
End If

Hope it can help you.

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.