I am trying to save items from listview to database,but whenever I try to run my code, the message "Object reference not set to an instant of an object".Here is my code..............

 Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        strsql = "Update tbl_tec_temp set tem_we_position = '" & TextBox8.Text & "', tem_we_company = '" & TextBox10.Text & "', tem_we_salary = '" & TextBox21.Text & "', tem_we_date = '" & DateTimePicker1.Text & "', tem_we_rl = '" & TextBox22.Text & "', tem_we_attach = '" & TextBox36.Text & "' where temp_no = '" & TextBox39.Text & "'"
        Dim da As New MySqlDataAdapter(strsql, CONNECTION)
        da.Fill(ds)

        MessageBox.Show("Proceed to Work Experience", "Message", MessageBoxButtons.OK, MessageBoxIcon.None)

        Try
            Dim i, c As String
            Dim a, b As String
            i = ListView1.Items.Count - 1
            While i >= 0
                a = ListView1.Items.Item(i).Text
                b = ListView1.Items.Item(i).SubItems.Item(1).Text
                c = ListView1.Items.Item(i).SubItems.Item(2).Text

                Dim QUERY As String
                QUERY = "INSERT INTO tbl_tec_temp(tem_we_position,tem_we_company,tem_we_salary)VALUES ('" + a + "','" + b + "','" + c + "')"




            End While
        Catch eEndEdit As System.Exception
            System.Windows.Forms.MessageBox.Show(eEndEdit.Message)
        End Try

Recommended Answers

All 3 Replies

Try initializing all your strings to "" in the declaration(i.e Dim i as String = ""). Without knowing what line is throwing the exception, it's hard to be more specific.

how about
Dim i as integer = ListView1.Items.Count - 1 ? i think it's more appropriate to do that

you might also want to read this

As tinstaafl says, you didn't say what line was throwing the error. Also, you should never (never) build queries by concatenation. That leads you open to SQL injection attacks. Build your queries using parameters. In your case that looks like

strsql = "UPDATE tbl_tec_temp                 " _
       & "   SET tem_we_position = @position, " _
       & "       tem_we_company  = @company,  " _
       & "       tem_we_salary   = @salary,   " _
       & "       tem_we_date     = @date,     " _
       & "       tem_we_rl       = @rl,       " _
       & "       tem_we_attach   = @attach    " _
       & " WHERE temp_no = @no"

Dim cmd As New SqlCommand(strsql)
cmd.Parameters.AddWithValue("@position", TextBox8.Text)     
cmd.Parameters.AddWithValue("@company ", TextBox10.Text)
cmd.Parameters.AddWithValue("@salary  ", TextBox21.Text)
cmd.Parameters.AddWithValue("@date    ", DateTimePicker1.Text)
cmd.Parameters.AddWithValue("@rl      ", TextBox22.Text)
cmd.Parameters.AddWithValue("@attach  ", TextBox36.Text)
cmd.Parameters.AddWithValue("@no      ", TextBox39.Text)

Please note the formatting of the statement that builds the query. Spreading the query across multiple lines makes it easier to read and adding a few blanks as padding to line fields up makes it easier to spot typos.

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.