vb.net - How to insert data into SQL Server database and retrieve it and put into datagridview? -
what want is, after insert data sql server database, datagridview retrieve added data database , add in row.
i'm new vb.net
code in dialog add form. name dlgadd
imports system.windows.forms imports system.data.sqlclient public class dlgadd dim constr string = "data source=server008;initial catalog=collectiondb;persist security info=true;user id=sa;password=p@ssw0rd1234" dim sql string dim sqlcon new sqlconnection dim sqlcmd new sqlcommand private sub ok_button_click(byval sender system.object, byval e system.eventargs) handles ok_button.click sql = "insert datacollection(customername,or_no,sor_no,balance,cash_amount,check_amount) values('" & txtname.text & "','" & txtor.text & "','" & txtsor.text & "','" & txtbalance.text & "','" & txtcash.text & "','" & txtcheck.text & "')" sqlcmd = new sqlcommand(sql, sqlcon) sqlcmd.executenonquery() me.dialogresult = system.windows.forms.dialogresult.ok end sub private sub cancel_button_click(byval sender system.object, byval e system.eventargs) handles cancel_button.click me.dialogresult = system.windows.forms.dialogresult.cancel me.close() end sub private sub dlgadd_load(byval sender system.object, byval e system.eventargs) handles mybase.load sqlcon = new sqlconnection(constr) sqlcon.open() end sub end class ' here code in maintrans form dgmaindata datagridview. imports system.data imports system.data.sqlclient public class frmmaintrans dim constr string = "data source=server008;initial catalog=collectiondb;persist security info=true;user id=sa;password=p@ssw0rd1234" dim sql string = "select * datacollection" dim sqlcon new sqlconnection dim sqlcmd new sqlcommand dim sqladapter sqldataadapter dim sqlbuilder sqlcommandbuilder dim sqldataset dataset dim sqltable datatable private sub getdata() sqlcon = new sqlconnection(constr) sqlcon.open() sqlcmd = new sqlcommand(sql, sqlcon) sqladapter = new sqldataadapter(sqlcmd) sqlbuilder = new sqlcommandbuilder(sqladapter) sqldataset = new dataset sqladapter.fill(sqldataset, "datacollection") sqltable = sqldataset.tables("datacollection") sqlcon.close() dgmaindata.datasource = sqldataset.tables("datacollection") dgmaindata.readonly = true dgmaindata.selectionmode = datagridviewselectionmode.fullrowselect end sub private sub btnadd_click(byval sender system.object, byval e system.eventargs) handles btnadd.click dlgadd.showdialog() end sub private sub frmmaintrans_load(byval sender system.object, byval e system.eventargs) handles mybase.load call getdata() end sub end class
Comments
Post a Comment