vb.net - SQL Server database not getting updated -
i using following code, database not getting updated:
dim update new sqlcommand("update fagr set fagrn=@fagrn,fagru=@fagru fagrc=@fagrc", connection) update.parameters.add(new sqlparameter("@fagrc", sqldbtype.nchar, 3)) update.parameters.add(new sqlparameter("@fagrn", sqldbtype.nvarchar, 50)) update.parameters.add(new sqlparameter("@fagru", sqldbtype.nchar, 3)) dim integer = 0 while < datagridview1.rows.count try update.parameters(0).value = datagridview1.rows(i).cells(0).tostring update.parameters(1).value = datagridview1.rows(i).cells(1).tostring update.parameters(2).value = datagridview1.rows(i).cells(2).tostring vartemp1 = update.executenonquery() i=i+1 catch ex exception msgbox("exception:" & vbcrlf & ex.message) me.close() end try loop
'in similar insert query first row of grid inserted in database. other records not inserted no error shown.
first, make sure you're referring right columns:
dim fagrc datagridviewcolumn = datagridview1.columns("fagrc") dim fagrn datagridviewcolumn = datagridview1.columns("fagrn") dim fagru datagridviewcolumn = datagridview1.columns("fagru")
datagridviewcell.tostring
not return cell value. reflector:
public mustinherit class datagridviewcell public overrides function tostring() string return string.concat(new string() {"datagridviewcell { columnindex=", me.columnindex.tostring(cultureinfo.currentculture), ", rowindex=", me.rowindex.tostring(cultureinfo.currentculture), " }"}) end function end class
do this:
dim id string = nothing each row datagridviewrow in datagridview1.rows if (not row.isnewrow) id = row.cells(fagrc.index).value.tostring() if (string.isnullorempty(id)) throw new applicationexception("fagrc missing.") else update.parameters(0).value = id update.parameters(1).value = row.cells(fagrn.index).value.tostring() update.parameters(2).value = row.cells(fagru.index).value.tostring() if (update.executenonquery() <= 0) throw new applicationexception("could not update fagrc value '" & id & "'.") end if end if end if next
Comments
Post a Comment