vb.net - How to insert data to DB using datagrid -


i want insert data sql server database using datagrid view.i added data grid , need commit them using loop.

dim i, rowcount integer rowcount = dgorder.rows.count  = 0 (rowcount - 1)     objcon.doexecute("insert ordermf (itemtype,itemnm,unitprice,quantity,discount,totalvalue,freeitem)values('" _                         & dgorder.item(i, 1).value & "','" _                         & dgorder.item(i, 0).value & "','" _                         & txtordno.text & "','" _                         & dgorder.item(i, 3).value & "','" _                         & dgorder.item(i, 2).value & "','" _                         & dgorder.item(i, 5).value & "','" _                         & dgorder.item(i, 4).value & "','" _                         & dgorder.item(i, 6).value & "')")     if objcon.m_success = "0"         msgbox("record added successfully", msgboxstyle.information, "robbialak")         call clearfields1()         cmdorder.enabled = false     end if next 

this code button click.but dosen't work me.it getting exception saying "index out of range.must non negative , less size of collection,parameter name:index" .in code dgorder datagridview name , used method call doexecute execute sql string.please me through problem.

against intuition, .item(columnindex, rowindex) not .item(rowindex, columnindex). luckily there .item(columnname, rowindex) , advise use that, because increase readability lot. think should change

dgorder.item(i, 1).value 

to

dgorder.item("itemtype", i).value 

(if column name "itemtype") , on.

also, seems trying insert more values there columns specified

  1. itemtype,
  2. itemnm,
  3. unitprice,
  4. quantity,
  5. discount,
  6. totalvalue,
  7. freeitem - 7 columns:

    1. dgorder.item(i, 1).value & "','" _

    2. & dgorder.item(i, 0).value & "','" _

    3. & txtordno.text & "','" _

    4. & dgorder.item(i, 3).value & "','" _

    5. & dgorder.item(i, 2).value & "','" _

    6. & dgorder.item(i, 5).value & "','" _

    7. & dgorder.item(i, 4).value & "','" _

    8. & dgorder.item(i, 6).value & "' - 8 values


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -