Insert SQL with variables and variable is not changing in loop (vb.net, postgresql)? -
i'm working on app insert data csv postgresql database. populate datagrid, loop through datagrid , insert record table (yes know code pretty verbose, want way right testing purposes). seems work perfectly; when running code , debugging, variables change during loop, but, on insert database, inserts data first row each new insert , not new variable value.
thoughts? suggestions?
here's full code:`me.btnpoupulatedata.enabled = true dim objconn new system.data.odbc.odbcconnection dim objcmd new system.data.odbc.odbccommand dim dtadapter new system.data.odbc.odbcdataadapter dim ds new dataset dim strconnstring string dim strsql string
'these required fields table product_template 'catc null vals exceptions dim str_mes_type string = "fixed" dim i_uom_id integer = 1 dim i_uom_po_id integer = 1 dim strtype string = "product" dim str_procure_method string = "make_to_stock" dim str_cost_method string = "standard" dim i_categ_id integer = 1 dim str_supply_method string = "buy" dim str_sale_ok string = true dim str_import_date string = me.txtimportid.text dim dgv datagridview = datagridview1 dim iimportcounter integer = 0 system.windows.forms.cursor.current = cursors.waitcursor strconnstring = "dsn=postgresql35w;database=openerp;server=localhost;port=5432;uid=openpg;pwd=openpgpwd" objconn.connectionstring = strconnstring objconn.open() strsql = "insert product_template (name,description,standard_price,list_price,mes_type,uom_id,uom_po_id,type,procure_method,cost_method,categ_id,supply_method,sale_ok,import_date) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" dtadapter.selectcommand = objcmd objcmd.connection = objconn try integer = 0 dgv.rowcount - 1 'workaround problem of not exiting loop when @ end of rows if dgv.rowcount = 1 exit try end if iimportcounter = iimportcounter + 1 me.lblrecordsimported.text = "records imported: " & iimportcounter dim r datagridviewrow = dgv.rows(i) '*************these changeable variables****** objcmd .parameters.add("name", odbc.odbctype.nvarchar) .parameters.add("description", odbc.odbctype.nvarchar) .parameters.add("standard_price", odbc.odbctype.nvarchar) .parameters.add("list_price", odbc.odbctype.nvarchar) end 'name goes default code internal reference number dim strname string strname = dgv.rows(i).cells(0).value dim str_description string str_description = dgv.rows(i).cells(1).value dim i_standard_price string i_standard_price = dgv.rows(i).cells(2).value dim i_list_price string i_list_price = dgv.rows(i).cells(3).value objcmd 'number of parameters must equal number of ? marks in sql statement '14 params '.parameters.addwithvalue used data that's constant .parameters("name").value = strname .parameters("description").value = str_description .parameters("standard_price").value = i_standard_price .parameters("list_price").value = i_list_price .parameters.addwithvalue("mes_type", str_mes_type) .parameters.addwithvalue("uom_id", i_uom_id) .parameters.addwithvalue("uom_po_id", i_uom_po_id) .parameters.addwithvalue("type", strtype) .parameters.addwithvalue("procure_method", str_procure_method) .parameters.addwithvalue("cost_method", str_cost_method) .parameters.addwithvalue("categ_id", i_categ_id) .parameters.addwithvalue("supply_method", str_supply_method) .parameters.addwithvalue("sale_ok", str_sale_ok) '*******created new column in product_template called import_date******* 'type set char verying, used later searching .parameters.addwithvalue("import_date", str_import_date) .commandtext = strsql .executenonquery() 'delete gridview row after import dgv.rows.removeat(i) application.doevents() end next catch ex exception ' 'this way resume next since there errors on specific data rows 'will ignored importcsv() end try objconn.close() system.windows.forms.cursor.current = cursors.default
`
you should adding parameters once outside loop set parameter values inside loop using:
with objcmd .parameters.append objcmd.createparameter("name", advarchar, adparaminput, 20) ... end integer = 0 dgv.rowcount - 1 dim r datagridviewrow = dgv.rows(i) dim strname string = dgv.rows(i).cells(0).value dim i_standard_price string = dgv.rows(i).cells(1).value dim i_list_price string = dgv.rows(i).cells(2).value objcmd 'number of parameters must equal number of ? marks in sql statement .parameters("name").value = strname ...
Comments
Post a Comment