vb.net - error deleting from database -
i have sql server compact database created through vs 2010 (local database file option). on form load (categoryform
) load values database datagridview1
. add buttoncolumn
programmatically use delete
part. problem this:
- on initial form load, first time press delete on row, works. if press again not work.
- the second time click button, printed text on
msgbox
, text of button! (delete) (screenshot included) p.s mentionted below, when comment-out stuff, correct values returned.
what tried:
- commented out sql-related, left part
rowindex
, value @ specific cell @ index. print them both onmsgbox
. values correct. example,0
index , valuetest
first row texttest
.
below progress screenshots:
cellcontentclick
method:
private sub datagridview1_cellcontectclick(byval sender object, byval e system.windows.forms.datagridviewcelleventargs) handles datagridview1.cellcontentclick dim string 'if e.columnindex = 1 'if e.rowindex >= 0 , e.rowindex <= datagridview1.rows.count - 1 = datagridview1.rows(e.rowindex).cells(0).value.tostring msgbox(e.rowindex) msgbox(i) sqlstringdelete = "delete category categoryname = '" & & "'" sqlconn.connectionstring = connstring 'set connection string sqlconn.open() 'open connection sqlcmd.connection = sqlconn 'sets connection use sql command sqlcmd.commandtext = sqlstringdelete 'sets sql string sqlcmd.executenonquery() 'executes sql command 'create adapter dim dataadapter sqlcedataadapter = new sqlcedataadapter("select categoryname category", sqlconn) 'create dataset dim dataset new dataset 'fill datset dataadapter.fill(dataset) 'attach dataset datagrid datagridview1 .datasource = dataset.tables(0) .columns(0).headertext = "" .columns(0).autosizemode = datagridviewautosizecolumnmode.fill end dataadapter = nothing dataset = nothing sqlconn.close() 'close connection end sub
form_load
method:
private sub categoryform_load(byval sender system.object, byval e system.eventargs) handles mybase.load sqlconn.connectionstring = connstring 'set connection string sqlconn.open() 'open connection 'create adapter dim dataadapter sqlcedataadapter = new sqlcedataadapter("select categoryname category", sqlconn) 'create dataset dim dataset new dataset 'fill datset dataadapter.fill(dataset) 'attach dataset datagrid datagridview1 .datasource = dataset.tables(0) .columns(0).headertext = "" .columns(0).autosizemode = datagridviewautosizecolumnmode.fill end dataadapter = nothing dataset = nothing sqlconn.close() buttoncolumn .name = "deletebuttoncolumn" .headertext = "" .text = "delete" .usecolumntextforbuttonvalue = true .autosizemode = datagridviewautosizecolumnmode.fill .width = 50 end if datagridview1.columns.count = 1 datagridview1.columns.add(buttoncolumn) end if end sub
screenshots:
turns out columns autoregenerating causing column re-arrangement on each second click. set datagridview1.autogeneratecolumns = false
in both form load , event , works.
Comments
Post a Comment