Hi,

I've problem here. I create 3 check boxlist using vb.net 2.0. One of my check boxlist is uncheck when i try to save in database. when i want to save this using Microsoft access database the error come System.NullReferenceException – “Object reference not set to an instance of an object.

Please help me to encounter this problem.

Dani AI

Generated

and — quick, practical follow-ups based on the code shown.

The crash comes from dereferencing Nothing (for example using SelectedItem.Text when no item is selected) and from incorrect checks and command setup. IsDBNull tests for a database DBNull.Value, not for VB Nothing — use If control.SelectedItem IsNot Nothing Then ... or iterate the Items collection to find checked entries. Also make sure the OleDbCommand.CommandText and parameters are set before calling ExecuteNonQuery. See the docs for SelectedItem and IsDBNull for details: ListControl.SelectedItem and Information.IsDBNull.

Example patterns to apply (VB.NET):

' build a comma-separated value from checked items
Dim sb As New System.Text.StringBuilder()
For Each li As ListItem In chkluaran.Items
  If li.Selected Then sb.Append(li.Text).Append(","c)
Next
Dim luaranValue As String = If(sb.Length > 0, sb.ToString().TrimEnd(","c), String.Empty)
' parameterized insert using Using blocks (replace MyTable and columns)
Using conn As New OleDbConnection(connString)
  Using cmd As New OleDbCommand("INSERT INTO MyTable (result_luaran, date_created) VALUES (?, ?)", conn)
    cmd.Parameters.AddWithValue("?", luaranValue)
    cmd.Parameters.AddWithValue("?", DateTime.Now)
    conn.Open()
    cmd.ExecuteNonQuery()
  End Using
End Using

Checklist: guard against Nothing before accessing properties; iterate CheckBoxList.Items for multiple selections; always set CommandText and use parameters (OleDb uses ? placeholders); use Using to close connections; log exceptions server-side instead of dumping SQL errors to the page. For command details see the OleDbCommand docs: OleDbCommand.

Recommended Answers

All 2 Replies

Member Avatar for Member #46692

Please help me to encounter this problem.

Try running your program again :) I'm sure you'll get the same error.

Try running your program again :) I'm sure you'll get the same error.

Here i 'give my sample coding to help me.

Protected Sub btnaccept_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnaccept.Click
Dim strSQl As String
Dim strluaran, strPersekitaran, strperibadi As String
Dim mycommand As OleDbCommand = New OleDbCommand


conn = New OleDbConnection(connString)
mycommand.Connection = conn

lbldate.Text = Format(Date.Now, "dd-MMM-yyyy")

If IsDBNull(chkluaran.SelectedItem.Text) Then
chkluaran.SelectedItem.Text = ""
End If
If IsDBNull(chkPersekitaran.SelectedItem.Text) Then
chkPersekitaran.SelectedItem.Text = ""
End If
If IsDBNull(chkperibadi.SelectedItem.Text) Then
chkperibadi.SelectedItem.Text = ""
End If
strSQl = " INSERT INTO scan_Cust_result(cust_name,cust_mothername, cust_result_email, cust_result, result_luaran, result_persekitaran, " _
& " result_peribadi, cust_desc, date_created) VALUES " _
& " ('" & lblname.Text & "', '" & lblmothername.Text & "','" & lblemail.Text & "', '" & chkmasalah.SelectedItem.Text & "', " _
& " '" & chkluaran.SelectedItem.Text & "', '" & chkPersekitaran.SelectedItem.Text & "', '" & chkperibadi.SelectedItem.Text & "', " _
& " '" & txtdesc.Text & "', '" & lbldate.Text & "')"
Try
conn.Open()
mycommand.ExecuteNonQuery()
conn.Close()
Response.Write("Your Results are Successfully Saved.")
Catch SQLexc As OleDbException
Response.Write("Insert Failed. Error Details are: " & SQLexc.ToString())
End Try

End Sub

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.