3 Replies - 920 Views - Last Post: 08 August 2011 - 05:08 AM Rate Topic: -----

#1 Skada  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-August 11

Problem with my for loop logic in datagrid view with checkbox column

Posted 07 August 2011 - 07:53 PM

Can anyone teach me how to fix my logic. It's lacking of loop and I dont know how to fix it. The main problem is that not all the selected records are inserting/ed in the database. only the first checked checkbox inside my datagridview. I'll attached a picture for the viewers to easily understand my problem.

So,First,Here's my code:

    



            For i As Integer = 0 To DataGridView1.Rows.Count - 1


                If CBool(DataGridView1.Rows(i).Cells(0).Value) = True Then
                    command.Parameters.AddWithValue("@CONSO_PARENT_STOCKHOLD_CODE", DataGridView1.Rows(i).Cells(2).Value)
                    ParentDone = True
                End If
               [b]If CBool(DataGridView1.Rows(i).Cells(1).Value) = True Then
                    command.Parameters.AddWithValue("@CONSO_CHILD_STOCKHOLD_CODE", DataGridView1.Rows(i).Cells(2).Value)
                    ChildDone = True
                End If[/b]

                If ParentDone AndAlso ChildDone Then Exit For

            Next i

            If ParentDone AndAlso ChildDone Then
                sql_connection.Open()
                command.ExecuteNonQuery()
                sql_connection.Close()
                command.Parameters.Clear()
            Else
                MsgBox("failed")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        itemlist.Clear()
    End Sub

Attached File(s)


This post has been edited by Dogstopper: 07 August 2011 - 07:57 PM
Reason for edit:: Please dont use all caps in titles.


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with my for loop logic in datagrid view with checkbox column

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1055
  • View blog
  • Posts: 4,087
  • Joined: 02-July 08

Re: Problem with my for loop logic in datagrid view with checkbox column

Posted 07 August 2011 - 08:15 PM

Maybe?

Open the connection,
Start the loop,
Check the pair, if true fill parameters and execute command,
Continue loop til end,
Close connection.
Was This Post Helpful? 0
  • +
  • -

#3 Skada  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-August 11

Re: Problem with my for loop logic in datagrid view with checkbox column

Posted 07 August 2011 - 09:54 PM

View Posthawkvalley1, on 07 August 2011 - 08:15 PM, said:

Maybe?

Open the connection,
Start the loop,
Check the pair, if true fill parameters and execute command,
Continue loop til end,
Close connection.



Hi Sir! Thank you for replying on my post.

Please enlighten me. Im new to Vb.net.

Will you please apply your suggestion to my codes?


Looking for your reply.
Was This Post Helpful? 0
  • +
  • -

#4 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: Problem with my for loop logic in datagrid view with checkbox column

Posted 08 August 2011 - 05:08 AM

Another way,see my poor comments inline
I tested this code on dummy database "Tester" with one table "Stock" only
Public Class Form1
    Private SqlConn As SqlConnection
    Private SqlDa As SqlDataAdapter = New SqlDataAdapter
    Private SqlDs As DataSet = New DataSet
    Private SqlDt As DataTable = New DataTable
    Private SqlBld As SqlCommandBuilder
    Private UpdCmd As SqlCommand
    Private SqlBind As BindingSource
    'Load table to grid
    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        LoadData()
    End Sub

    Private Sub LoadData()
        'change table name to suit
        If SqlDs.Tables("Stock") IsNot Nothing Then SqlDs.Tables("Stock").Clear()
        Dim SqlSb As New SqlConnectionStringBuilder()
        SqlSb.DataSource = ".\SQLEXPRESS" ' <--change to your suit
        SqlSb.InitialCatalog = "Tester" ' <--change to your suit
        SqlSb.IntegratedSecurity = True ' <--change to your suit
        Dim strConn = SqlSb.ConnectionString
        Dim SqlConn As SqlConnection = New SqlConnection(strConn)
        Dim strQuery As String
        SqlConn.Open()
        'select the required content from the table
        Try
            strQuery = "SELECT * FROM Stock" ' <--change to your suit
            '1' if you use instead "SELECT id,a,b,c...  FROM Stock" it will fail
            '2' if your table haven't have a primary key column it will fail as well
            SqlDa = New SqlDataAdapter(strQuery, SqlConn)

            SqlDt = New DataTable("Stock") ' <--change to your suit
            'Fill the dataset
            SqlDa.Fill(SqlDs, "Stock") ' <--change to your suit
            ' call command builder
            SqlBld = New SqlCommandBuilder(SqlDa)

            'populate a datagridview (I'm not preferring it)
            'DataGridView1.DataSource = SqlDs.Tables("Stock")
            ' you can populate a datagridview this way as well:
            SqlBind = New BindingSource
            SqlBind.DataSource = SqlDs.Tables("Stock") ' <--change to your suit
            DataGridView1.DataSource = SqlBind
        Catch ex As Exception

            'if  an error occurred,displaythe error message and bad code line
            MsgBox(ex.Message & vbLf & ex.StackTrace)
        End Try
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        If SqlConn Is Nothing Then
            Dim SqlSb As New SqlConnectionStringBuilder()
            SqlSb.DataSource = ".\SQLEXPRESS" ' <--change to your suit
            SqlSb.InitialCatalog = "Tester" ' <--change to your suit
            SqlSb.IntegratedSecurity = True ' <--change to your suit
            Dim strConn = SqlSb.ConnectionString
            SqlConn = New SqlConnection(strConn)
            SqlConn.Open()
            Dim SqlCmd As SqlCommand = SqlConn.CreateCommand
            UpdCmd = SqlBld.GetUpdateCommand()
            If Not SqlDs.HasChanges(DataRowState.Modified) Then
                MsgBox("Nothing changed")
                Exit Sub
            End If
            'if you're use databinding on LoadData sub, add this line:
            SqlBind.EndEdit()
            SqlDa.Update(SqlDs, "Stock") ' <--change to your suit
        End If
        If SqlConn IsNot Nothing Then SqlConn.Close()
    End Sub

    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        LoadData()
    End Sub

In addition,
You might be want to use SqlTransaction object
(see MSDN or other docs)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1