sql server - How to update a datagridview using vb.net -
i using below mention code update , delete data datagridview. unable solve issue. delete working not update.
public sub customerupdatebatch(byval dt datatable)
dim connection sqlconnection = new sqlconnection(invoice.getconnect()) connection.open() try dim command sqlcommand = new sqlcommand("update_customer", connection) command.commandtype = commandtype.storedprocedure command.parameters.add("@c_id", sqldbtype.int, 16, "c_id") command.parameters.add("@c_name", sqldbtype.varchar, 50, "c_name") command.parameters.add("@c_address", sqldbtype.varchar, 50, "c_address") command.parameters.add("@c_tel", sqldbtype.varchar, 50, "c_tel") command.parameters.add("@c_fax", sqldbtype.varchar, 50, "c_fax") command.parameters.add("@c_mobile", sqldbtype.varchar, 50, "c_mobile") command.parameters.add("@c_email", sqldbtype.varchar, 50, "c_email") command.parameters.add("@c_tin", sqldbtype.varchar, 50, "c_tin") command.parameters.add("@c_remarks", sqldbtype.varchar, 50, "c_remarks") command.commandtype = commandtype.storedprocedure dim delcommand sqlcommand = new sqlcommand("delete_customer", connection) delcommand.commandtype = commandtype.storedprocedure command.parameters.add("@c_id", sqldbtype.int, 16, "c_id") dim adapter sqldataadapter = new sqldataadapter() adapter.updatecommand = command adapter.deletecommand = delcommand adapter.deletecommand.updatedrowsource = updaterowsource.none adapter.update(dt) catch ex exception console.writeline(ex.message) throw connection.close() end try end sub
one possible issue has using auto-number indexes. when add new data table index not generated or picked db autmatically. don't database have, i'll show example mysql , ms sql.
first, need capture events data adapter. "connection" , "dataadapter" definitions need outside function:
dim withevents adapter sqldataadapter = new sqldataadapter() then you'll need have function capture events:
private sub myadapter_rowupdated(byval sender object, byval e system.data.oledb.oledbrowupdatedeventargs) handles adapter.rowupdated if (e.statementtype = statementtype.insert , e.status = updatestatus.continue) dim id ulong = getidentity() if (id > 0) e.row(0) = id end if end if end sub the getidentiy() function need specific sql server. 1 mysql:
public function getidentity() ulong dim newcommand new mysqlcommand("select last_insert_id();", connection) return newcommand.executescalar() end function here 1 ms sql:
public function getidentity() ulong dim newcommand new oledbcommand("select @@identity", connection) return newcommand.executescalar() end function
Comments
Post a Comment