0

Good Morning I have a program that looks like this.

Form Design

I have a button named Multiple Select and when I click that the button will call SaveCheckedRecords() and here is the code for it

  Public Sub SaveCheckedRecords()
        Dim failed = False
        For Each row As DataGridViewRow In DataGridView1.Rows

            If row.Cells(0).Value = True Then
                Dim i As Integer
                i = DataGridView1.CurrentRow.Index
                Label2.Text = DataGridView1.Item("ItemCode", i).Value
                Label3.Text = DataGridView1.Item("Description", i).Value
                Label4.Text = DataGridView1.Item("ReflectedQty", i).Value
                Label5.Text = DataGridView1.Item("UOM", i).Value
                Label6.Text = DataGridView1.Item("UnitPrice", i).Value
                Label7.Text = DataGridView1.Item("Total", i).Value
                Label8.Text = DataGridView1.Item("Remarks", i).Value
                standard() '<------------- Call this Sub
                insert()   '<------------- Call this Sub
                failed = True
                Exit For

            End If
        Next
        If Not failed Then
            MsgBox("Please select an item to receive")
        End If
    End Sub

Now my goal based on the code above is to transfer the data from datagridview rows to labels and call some Subs

Here is the code for the standard and insert

Private Sub standard()
        Dim con As MySqlConnection = New MySqlConnection("server=localhost;userid=root;password=admin1950;database=inventory")
        Dim cmd As MySqlCommand = New MySqlCommand("select StandardUOM,QtyPerUoM from item_master_list where ItemCode = '" & Label2.Text & "'", con)
        Dim reader As MySqlDataReader
        con.Open()
        reader = cmd.ExecuteReader
        While reader.Read
            Label9.Text = reader.GetString("StandardUOM")
            Label10.Text = reader.GetString("QtyPerUoM")
        End While
    End Sub


 Private Sub insert()
        DataGridView1.Columns.RemoveAt(0)
        Dim con1 As MySqlConnection = New MySqlConnection("datasource=localhost;database=inventory;userid=root;password=admin1950")
        Dim cmdinsert As MySqlCommand = New MySqlCommand("insert into receiving (RINo,PONo,ItemCode,Description,QtyPack,PackUoM,UnitPrice,Total,Remarks,ExpiryDate,QtyStan,StanUoM,PCS) values ('" & frm_Add_Receiving_Items.TextBox1.Text & "','" & Label1.Text & "','" & Label2.Text & "','" & Label3.Text & "','" & Label11.Text & "','" & Label5.Text & "','" & Label6.Text & "','" & Label7.Text & "','" & Label8.Text & "','" & DateTime.Now.ToString("yyyy-MM-dd") & "','" & Label12.Text & "','" & Label9.Text & "','" & Label10.Text & "')", con1)
        con1.Open()
        cmdinsert.ExecuteNonQuery()
        con1.Close()
        frm_Add_Receiving_Items.generate_rec_form()
        updateRI()
        loadlist1()
        frm_Add_Receiving_Items.Button6.Enabled = False
        Label2.Text = "Label2"
    End Sub

All I want to do is to save the value of datagridview when checkboxcolumn(0) is True whats wrong with my code?

TYSM

  • You need to provide ALL the relevant information. Firstly, there's nothing in that code that saves any data. Secondly, we'd need to know what behaviour you expect and what behaviour you're seeing that leads you to believe that it's not working. Thirdly, have you debugged your code? You don't just look at the end result and post here if it's not what you expect. You need to step through your code and determine exactly where what actually happens deviates from what you expect. – jmcilhinney Sep 22 '16 at 04:42
  • Hello sir TY for the respond I will Update my post right away – Stygrian Desolator Sep 22 '16 at 04:47
  • @jmcilhinney pls see the edited post TY – Stygrian Desolator Sep 22 '16 at 04:57
  • And where EXACTLY does reality differ from expectation? If all you can say is that you don't see the expected data in the database when all is said and done then I refer you back to my earlier comment, i.e. you need to debug your code to see WHERE it doesn't do what you expect. For instance, is it not getting into the 'For Each` loop at all? If it gets to the `ExecuteNonQuery` call then I don't see how a record can not be being inserted, unless an exception is thrown. Long story short, you need to exhaust all you can do first, then provide us with ALL the relevant information you've got. – jmcilhinney Sep 22 '16 at 05:27
  • That's my answer! lol. As far as I remember I didn't make it that complicated. You are already using a `For Loop`. What's the use of this `i = DataGridView1.CurrentRow.Index`? It will just save the current row (duh, of course?) of your grid. – Aethan Sep 22 '16 at 05:36
  • @CrushSundae remember the one i told you last time that i will the whats really running on my system well here it is. Sir Im sorry\ – Stygrian Desolator Sep 22 '16 at 05:56
  • It might be easier to debug and read this if you parameterise your queries. Won't fix the issue, but makes it easier to find the problem. – David Sep 22 '16 at 11:54

0 Answers0