i am inserting data using datagrid but my problem is that it is not inserting the last row item itemqty value only,in one row i am inserting groupcode,itemcode,itemqty,entrydate,locationcode,addt only .the itemqty of last row is not inserted if i give itemqty=1 by default it takes 0 and all(groupcode,itemcode,entrydate,locationcode,addt) remaining item is inserted correctely.like that if i insert 2 row row 1 is correctely inserted but row 2 itemqty is 0,but when i insert 2 row with clicking outside the itemqty cell after putting the value in itemqty cell then it insert the correct value in database.there is some focus or click event problem plz help me ........ code is given below

Sub modify_inventory(ByVal frm As frminventory)
        Dim writesqltime As String
        Dim writesqldate As String
        writesqldate = Format(CDate(sqldate), "MM/dd/yyyy")
        writesqltime = sqltime
        Dim cmd = New SqlClient.SqlCommand
        If active_frm.Name <> "frminventory" Then
            Exit Sub
        Else
            writesqldate = Format(CDate(sqldate), "MM/dd/yyyy")
            Dim j As Integer
            con = connect()
            con.Open()
            For j = 0 To 5
                For i = 0 To frm.dgvitem(j).RowCount - 1
                    If frm.dgvitem(j).Item(4, i).Value <> 0 Then
                        cmd = New SqlClient.SqlCommand("INSERT INTO minventory(groupcode,itemcode,itemqty,entrydate,locationcode,addt) VALUES('" & frm.dgvitem(j).Item(0, i).Value & "','" & frm.dgvitem(j).Item(1, i).Value & "'," & frm.dgvitem(j).Item(4, i).Value & ",'" & writesqldate & "'," & location_code & ",'" & writesqltime & "'  )", con)
                        cmd.ExecuteNonQuery()

                    End If
                Next i
            Next j
            con.Close()

            If active_frm.Name <> "MDIPOS" Then
                active_frm.Close()
            End If
            MDIpos.Panel1.Visible = False
            MDIpos.mnumaster.Enabled = True
            MDIpos.MenuItem1.Enabled = True
            MDIpos.mnureport.Enabled = True
            MDIpos.mnuexit.Enabled = True
            MDIpos.mnumodify.Visible = False
            MDIpos.mnumodify.Enabled = False
            MDIpos.mnuadd.Visible = False
            MDIpos.mnuadd.Enabled = False
            MDIpos.Mnuclear.Visible = False
            MDIpos.Mnuclear.Enabled = False
            MDIpos.Mnudelete.Visible = False
            MDIpos.Mnudelete.Enabled = False
        End If
    End Sub

Edited 5 Years Ago by __avd: Added [code] tags.

>i am inserting data using datagrid but my problem is that it is not inserting the last row item itemqty value only

Maybe ItemQty is empty. I think more explanation is needed.

thanks for your response adatapost
I am running the code from module , my datagrid is in farminventory page , modify button is in mdipos page. i insert value in farminventory's(datagrid) page ,then click modify button which calls the function modify_inventoryin which is in module where the code is written.you are right that itemqty field is empty. suppose there are 2 rows in my datagrid having 2 itemqty column i put value 1 in( itemqty in row 1) and value 2 in ( itemqty in row 2)then click modify button which is in mdipos the row 1 itemqty is 1 but the itemqty of row2 is vanishes just when i click modify button .but the same code is running swiftly when assign to a button event in farminventory page . it loses focus when i click modify button

Actually last row in dataGridView is considered as "New" or "Empty" so I think you need to put - frm.dgvitem(j).RowCount - 2

For i = 0 To frm.dgvitem(j).RowCount - 2
 ....

adatapost
when i use
For i = 0 To frm.dgvitem(j).RowCount - 2
the result is same

no loop related problem there is focus problem when i click modify button it exits from farminventory page and the item where the cursor was positioned before click event is not entered . but when i click outside the itemqty column after putting value then the value is inserted correct.

Please answer these questions.

For j = 0 To 5
                For i = 0 To frm.dgvitem(j).RowCount - 1
                    If frm.dgvitem(j).Item(4, i).Value <> 0 Then
                        cmd = New SqlClient.SqlCommand("INSERT INTO minventory(groupcode,itemcode,itemqty,entrydate,locationcode,addt) VALUES('" & frm.dgvitem(j).Item(0, i).Value & "','" & frm.dgvitem(j).Item(1, i).Value & "'," & frm.dgvitem(j).Item(4, i).Value & ",'" & writesqldate & "'," & location_code & ",'" & writesqltime & "'  )", con)
                        cmd.ExecuteNonQuery()

                    End If
                Next i
            Next j

1. How many datardiview are there? I think they are six.

2. How many columns a table has?

yes u are right there are 6 datagrid and

6 column in my database as below; (minventory my db name)

groupcode       int             Checked
itemcode        int             Checked
entrydate       datetime        Checked
locationcode    int             Checked
ADDT            varchar(50)     Checked
itemqty         int             Checked

5 columns in my datagrid

GROUPCODE
ITEMCODE
ITEMNAME
TOTALQTY
ADDQUANTITY

iam populating datagrid in frm inventory page load event

[code begin of load event of frminventory]

Private Sub frminventory_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        active_frm.MdiParent = MDIpos
        MDIpos.Panel1.Visible = False
        MDIpos.mnumaster.Enabled = True
        MDIpos.MenuItem1.Enabled = False
        MDIpos.mnureport.Enabled = True
        MDIpos.mnuexit.Enabled = True
        MDIpos.mnumodify.Visible = True
        MDIpos.mnumodify.Enabled = True
        MDIpos.mnuadd.Visible = True
        MDIpos.mnuadd.Enabled = True
        MDIpos.Mnuclear.Visible = True
        MDIpos.Mnuclear.Enabled = True
        MDIpos.Mnudelete.Visible = True
        MDIpos.Mnudelete.Enabled = False
        MDIpos.mnuexport.Enabled = True
        MDIpos.mnuimport.Enabled = True
        MDIpos.mnuuser.Enabled = True
        MDIpos.mnupos.Enabled = True
        tab_page(0) = Me.TabPage1
        tab_page(1) = Me.TabPage2
        tab_page(2) = Me.TabPage3
        tab_page(3) = Me.TabPage4
        tab_page(4) = Me.TabPage5
        tab_page(5) = Me.TabPage6
        dgvitem(0) = Me.DataGridView1
        dgvitem(1) = Me.DataGridView2
        dgvitem(2) = Me.DataGridView3
        dgvitem(3) = Me.DataGridView4
        dgvitem(4) = Me.DataGridView5
        dgvitem(5) = Me.DataGridView6
        taballgroup.DrawMode = TabDrawMode.OwnerDrawFixed
        taballgroup.SizeMode = TabSizeMode.Fixed
        Dim tab_size As Size = taballgroup.ItemSize
        tab_size.Width = 35
        tab_size.Height = 80
        taballgroup.ItemSize = tab_size
        Dim writesqldate As String
        Dim writesqldate1 As String
        writesqldate = Format(CDate(sqldate), "MM/dd/yyyy")
        con = connectlogin()
        con.Open()
        cmd = New SqlClient.SqlCommand("Select GetDate()", con)
        dtServerDateTime1 = cmd.ExecuteScalar
        writesqltime = Format(dtServerDateTime1, " hh:mm:ss tt")
        writesqldate1 = Format(dtServerDateTime1, "dd/MM/yyyy ")
        Me.lbldate.Text = writesqldate1
        Me.Label1.Text = writesqltime
        con.Close()
        clk = writesqltime
        con = connect()
        con.Open()
        i = 0
        cmd = New SqlClient.SqlCommand("Select groupname,groupcode from mgroup where locationcode=" & location_code & " order by groupcode", con)
        dr1 = cmd.ExecuteReader
        While dr1.Read
            Me.taballgroup.Controls(i).Text = Trim(dr1("groupname"))
            a(i) = dr1("groupcode")
            b(i) = Trim(dr1("groupname"))
            If i < 5 Then
                i = i + 1
            Else
                Exit While
            End If
        End While
        dr1.Close()
        con.Close()
        con = connect()
        con.Open()
        SQLda.SelectCommand = New SqlClient.SqlCommand("SELECT count(itemqty) FROM minventory where entrydate='" & writesqldate & "'", con)
        qty = SQLda.SelectCommand.ExecuteScalar
        con.Close()
        If qty = 0 Then
            MDIpos.mnuadd.Enabled = True
            MDIpos.mnumodify.Enabled = False
            For i = 0 To 5
                populate_datagrid("select groupcode,itemcode,itemname ,0 as Quantity from mitem where groupcode='" & a(i) & "'ORDER BY ITEMCODE", Me.dgvitem(i))
                Me.dgvitem(i).Columns(0).Width = 10
                Me.dgvitem(i).Columns(0).Visible = False
                Me.dgvitem(i).Columns(1).Width = 50
                Me.dgvitem(i).Columns(1).Visible = False
                Me.dgvitem(i).Columns(2).Width = 290
                Me.dgvitem(i).Columns(3).Width = 200
                Me.dgvitem(i).Columns(3).ReadOnly = False
                Me.dgvitem(i).Columns(0).ReadOnly = True
                Me.dgvitem(i).Columns(1).ReadOnly = True

            Next
        ElseIf qty > 0 Then
            MDIpos.mnuadd.Enabled = False
            MDIpos.mnumodify.Enabled = True
            For i = 0 To 5
                populate_datagrid(" select  c.GROUPCODE,c.ITEMCODE,ITEMNAME ,isnull(t.totalqty,0) as TOTALQTY ,0 as ADDQUANTITY from (select * from MITEM where GROUPCODE='" & a(i) & "' ) as c  left join (select groupcode,itemcode, totalqty= ISNULL(SUM(itemqty),0) from MINVENTORY where  entrydate='" & writesqldate & "' and groupcode='" & a(i) & "' group by itemcode,groupcode) t on t.itemcode = c.itemcode and t.groupcode = c.GROUPCODE ORDER BY C.GROUPCODE ", Me.dgvitem(i))
               [COLOR="Green"] Me.dgvitem(i).Columns(0).Width = 110
                Me.dgvitem(i).Columns(0).Visible = False
                Me.dgvitem(i).Columns(1).Width = 150
                Me.dgvitem(i).Columns(1).Visible = False
                Me.dgvitem(i).Columns(2).Width = 267
                Me.dgvitem(i).Columns(3).Width = 110
                Me.dgvitem(i).Columns(4).Width = 110

                Me.dgvitem(i).Columns(3).ReadOnly = True
                Me.dgvitem(i).Columns(2).ReadOnly = True
                Me.dgvitem(i).Columns(0).ReadOnly = True
                Me.dgvitem(i).Columns(1).ReadOnly = True[/COLOR]

            Next
        End If
    End Sub

Edited 3 Years Ago by Dani: Formatting fixed

Sorry! I'm not sure but what I think that you are executing inner loop one more time.

For j = 0 To 5
                For i = 0 To frm.dgvitem(j).RowCount - 2
                    If frm.dgvitem(j).Item(4, i).Value <> 0 Then
                        cmd = New SqlClient.SqlCommand("INSERT INTO minventory(groupcode,itemcode,itemqty,entrydate,locationcode,addt) VALUES('" & frm.dgvitem(j).Item(0, i).Value & "','" & frm.dgvitem(j).Item(1, i).Value & "'," & frm.dgvitem(j).Item(4, i).Value & ",'" & writesqldate & "'," & location_code & ",'" & writesqltime & "'  )", con)
                        cmd.ExecuteNonQuery()

                    End If
                Next i
            Next j

Can you attach your project here?

This article has been dead for over six months. Start a new discussion instead.