程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了无法从 datagridview 保存数据大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决无法从 datagridview 保存数据?

开发过程中遇到无法从 datagridview 保存数据的问题如何解决?下面主要结合日常开发的经验,给出你关于无法从 datagridview 保存数据的解决方法建议,希望对你解决无法从 datagridview 保存数据有所启发或帮助;

发生的事情是我想将数据量保存在列 qty 中,它可以在单行上工作,但是一旦您放置了多行条目,它就会在通信中出现错误,从这里开始:

索引超出范围。保存多个grIDvIEw数据时必须为非负且小于集合的大小

.Parameters.AdDWithValue("@pID",CInt(DataGrIDVIEw1.Rows(i).Cells(1).Value.ToString))
Private Sub BtnSave_Click(sender As Object,e As EventArgs) Handles BtnSave.Click
    TxtReferenceNo.Text.Trim()
    TxtReceiveBy.Text.Trim()

    For a As Integer = 0 To DataGrIDVIEw1.Rows.Count - 1
        If DataGrIDVIEw1.Rows(a).Cells(7).Value = String.Empty Then
            MsgBox("Quantity must not be empty",MsgBoxStyle.Critical,"")
            Exit Sub
        End If
    Next

    Dim sdate As String = DateTimeReceived.Value.ToString("yyyy-MM-dd")
    Dim d1,d2 As Date
    Dim found As Boolean
    d1 = sdate
    d2 = Now.ToShortDateString

    If d1 <> d2 Then
        MsgBox("Date should should be current date","")
        Exit Sub
    End If

    For i As Integer = 0 To DataGrIDVIEw1.Rows.Count - 1
        Dim Tsql As String
        Tsql = "select * from tblstockin where refno like '" & TxtReferenceNo.Text & "'"
        '"' and pID like '" & DataGrIDVIEw1.Rows(i).Cells(1).Value.ToString &
        '"' and sdate like '" & sdate & "'"

        Con = New MysqLConnection
        With Con
            If .State = ConnectionState.Closed Then
                .ConnectionString = StrCon
                .open()
            End If
        End With

        Comm = New MysqLCommand(Tsql,Con)
        dr = Comm.ExecuteReader
        dr.Read()
        If dr.HasRows Then
            found = True
        End If
        dr.Close()
        Con.Close()

        Try
            If Not (IsNumeric(DataGrIDVIEw1.Rows(i).Cells(7).Value.ToString)) Then
                MsgBox("Quantity should be numeric","")
                Exit Sub
            End If
        Catch ex As Exception

        End Try


        If found = False Then
            Dim sql As String

            sql = "insert into tblstockin (refno,receivedby,pID,qty,sdate) 
                  values (@refno,@receivedby,@pID,@qty,@sdate) "

            'sql = "insert into tblstockin (refno,sdate) " &
            '"values ('" & TxtReferenceNo.Text & "','         '" & TxtReceiveBy.Text & "','         '" & CInt(DataGrIDVIEw1.Rows(i).Cells(1).Value.ToString) & "','         '" & CInt(DataGrIDVIEw1.Rows(i).Cells(7).Value.ToString) & "','         '" & sdate & "') "

            Con = New MysqLConnection
            With Con
                If .State = ConnectionState.Closed Then
                    .ConnectionString = StrCon
                    .open()
                End If
            End With

            Comm = New MysqLCommand(sql,Con)
            With Comm
                .Parameters.Clear()
                .CommandText = sql
                .Parameters.AdDWithValue("@refno",TxtReferenceNo.Text)
                .Parameters.AdDWithValue("@receivedby",TxtReceiveBy.Text)
                .Parameters.AdDWithValue("@pID",CInt(DataGrIDVIEw1.Rows(i).Cells(1).Value.ToString))
                .Parameters.AdDWithValue("@qty",CInt(DataGrIDVIEw1.Rows(i).Cells(7).Value.ToString))
                .Parameters.AdDWithValue("@sdate",sdate)
            End With

            If MessageBox.Show("Are you sure to save Stock","",MessageBoxbuttons.YesNo,MessageBoxIcon.Question) = DialogResult.Yes Then
                Dim result As Integer = Comm.ExecuteNonquery
                MsgBox("Stock has been saved",MsgBoxStyle.information,"")
                TxtReceiveBy.Clear()
                TxtCount.Clear()
                Con.Close()

                Dim hsql As String
                hsql = " insert into tblgenID (codeID,code) values (@codeID,@code) "

                Con = New MysqLConnection
                With Con
                    If .State = ConnectionState.Closed Then
                        .ConnectionString = StrCon
                        .open()
                    End If
                End With

                Comm = New MysqLCommand(hsql,Con)
                With Comm
                    .Parameters.Clear()
                    .CommandText = hsql
                    .Parameters.AdDWithValue("@codeID",LblD.Text)
                    .Parameters.AdDWithValue("@code",TxtReferenceNo.Text)
                End With
                Comm.ExecuteNonquery()
                Con.Close()
                TxtReferenceNo.Clear()
            End If

            'Dim fsql As String
            'fsql = "update tblproduct set qty=@qty where ID=@ID"

            Dim rsql As String
            rsql = "update tblproduct set qty = qty + " & CInt(DataGrIDVIEw1.Rows(i).Cells(7).Value.ToString) &
                " where ID like '" & DataGrIDVIEw1.Rows(i).Cells(1).Value.ToString & "'"
            'alternate code to update product quantity in product table
            'will update or add the number indicated on quantity to the existing product number in table

            Con = New MysqLConnection
            With Con
                If .State = ConnectionState.Closed Then
                    .ConnectionString = StrCon
                    .open()
                End If
            End With

            Comm = New MysqLCommand(rsql,Con)
            With Comm
                .Parameters.Clear()
                .CommandText = rsql
                '.Parameters.AdDWithValue("@qty",CInt(DataGrIDVIEw1.Rows(i).Cells(7).Value.ToString))
                '.Parameters.AdDWithValue("@ID",CInt(DataGrIDVIEw1.Rows(i).Cells(1).Value.ToString))
            End With
            Comm.ExecuteNonquery()
            DataGrIDVIEw2.Rows.Clear()
            DataGrIDVIEw1.Rows.Clear()
            CbxSearch.@R_502_5990@Text()
            TxtSearch.Clear()
            TxtReferenceNo.Clear()
            Con.Close()
        Else
            MsgBox("Reference no already exists","")
        End If
    Next

    GenerateStock()

    With Frm_Product_List
        .LoadProductList()
    End With
End Sub

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的无法从 datagridview 保存数据全部内容,希望文章能够帮你解决无法从 datagridview 保存数据所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: