Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27311

Problems with datagridview

$
0
0
I have a datagrid view in which I populate manually. I search a database then use datagrid.rows.add to populate the grid. This works wonderful.

I use this loop to insert into the database from the datagrid:

Code:

For Each row As DataGridViewRow In DataGrid.Rows

            If Not row.IsNewRow Then
               
                sqlstatement = "INSERT INTO Catagories(ID, Catagories, Product, [Key], [Desc], link, visible, jump, prev, userlevel) VALUES('" & row.Cells(0).Value.ToString & "', '" & row.Cells(1).Value.ToString & "', '" & row.Cells(2).Value.ToString & "', '" & row.Cells(3).Value.ToString & "', '" & row.Cells(4).Value.ToString & "', '" & row.Cells(5).Value.ToString & "', '" & row.Cells(6).Value.ToString & "', '" & row.Cells(7).Value.ToString & "', '" & row.Cells(8).Value.ToString & "', '" & row.Cells(9).Value.ToString & "')"
               
                cmd.CommandText = sqlstatement

                cmd.ExecuteNonQuery()

                sqlstatement = ""
            End If

        Next


This also works great.

Now my problem is I want the user to be able to edit everything. It will insert everything into the database correctly if it has been pulled from the data base and no rows have been added.
In other words, If I query the database and show the results in the datagrid. I can delete everything from the database and reinsert from the datagrid and it works great. It will also work if you edit a cell of an existing row.

It will not work anytime you add a new row. If you add one at the bottom, or insert one it the middle. It will give the error: Object reference not set to an instance of an object.

If you pull something out of the database and add a row to it, then save it back to the database, it will save everything except the new row before giving the error.

Can someone help me out? Thanks, Victor.

Viewing all articles
Browse latest Browse all 27311

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>