Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datagridview where i added 4 columns to 3 columns of data.
Of the added columns, 1 is filled by a checkbox selection, 2 are blank for User Entry and one is filled with a Date Selected in a DateTimePicker.

Everything displays correctly, just like I want it, but when I save this data to a New/Different table, it only saves the information pulled from my select statement, not the 4 added columns (leaves them as NULL).

I believe its in my counter in my save button (Button2) on the SQLBulkCopy. But cant put my finger on on it.

VB
Imports System.Data.SqlClient
Imports System.Windows.Forms
Imports System.Windows.Forms.DataGridView
Imports System.Windows.Forms.CheckBox
Imports System.Windows.Forms.DateTimePicker

Public Class Form5

    Public Sub Form5_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim CheckBox1 As New CheckBox
        Dim CheckBox2 As New CheckBox
        Dim CheckBox3 As New CheckBox
        Dim DateTimePicker1 As New DateTimePicker
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim sql As String
        Dim datepicked As Date
        datepicked = DateTimePicker1.Text
        'Checkbox Check Loop
        If CheckBox1.CheckState = False And CheckBox2.CheckState = False And CheckBox3.CheckState = False Then
            MessageBox.Show("Please select a Shift")
            Exit Sub
        End If
        If CheckBox1.Checked = True And CheckBox2.Checked = True Then
            MessageBox.Show("Please select a One Shift Only")
            Exit Sub
        End If
        If CheckBox2.Checked = True And CheckBox3.Checked = True Then
            MessageBox.Show("Please select a One Shift Only")
            Exit Sub
        End If
        If CheckBox1.Checked = True And CheckBox3.Checked = True Then
            MessageBox.Show("Please select a One Shift Only")
            Exit Sub
        End If

        'Selection of Shift 
        If CheckBox1.Checked = True Then
            sql = "SELECT MACHINENUMBER, STYLEWIDTH, SPACES FROM Loomset WHERE RunFlag1 = 1"
        ElseIf CheckBox2.Checked = True Then
            sql = "SELECT MACHINENUMBER, STYLEWIDTH, SPACES FROM Loomset WHERE RunFlag2 = 1"
        ElseIf CheckBox3.Checked = True Then
            sql = "SELECT MACHINENUMBER, STYLEWIDTH, SPACES FROM Loomset WHERE RunFlag3 = 1"
        End If

        'SQL Data connection
        Dim sqlCon1 As New SqlConnection("Data Source=SERVER1\DEV01;database=Production;uid=sa;pwd=passwordhere")
        Dim daSWC1 As New SqlDataAdapter(sql, sqlCon1)
        Dim SQLcmdBuilder1 As New SqlCommandBuilder(daSWC1)
        Dim ds1 As New DataSet

        'Adds the columns Picks, Runtime and Date.
        Dim AddCol1 As New DataGridViewTextBoxColumn
        Dim AddCol2 As New DataGridViewTextBoxColumn
        Dim AddCol3 As New DataGridViewTextBoxColumn
        Dim AddCol4 As New DataGridViewTextBoxColumn

        AddCol1.DataPropertyName = "Shift"
        AddCol1.HeaderText = "Shift"
        AddCol1.Name = "Shift"
        AddCol2.DataPropertyName = "Picks"
        AddCol2.HeaderText = "PICKS"
        AddCol2.Name = "PICKS"
        AddCol3.DataPropertyName = "RunTime"
        AddCol3.HeaderText = "RunTime"
        AddCol3.Name = "RunTime"
        AddCol4.DataPropertyName = "Date"
        AddCol4.HeaderText = "Date"
        AddCol4.Name = "Date"
        AddCol4.DefaultCellStyle.Format = "MM/dd/yyyy"

        daSWC1.Fill(ds1, "MACHINENUMBER")
        DataGridView1.DataSource = ds1.Tables(0)
        DataGridView1.Columns.Add(AddCol1)
        For Each dgvr As DataGridViewRow In DataGridView1.Rows
            If CheckBox1.Checked Then
                dgvr.Cells("Shift").Value = 1
            ElseIf CheckBox2.Checked Then
                dgvr.Cells("Shift").Value = 2
            ElseIf CheckBox3.Checked Then
                dgvr.Cells("Shift").Value = 3
            End If
        Next
        DataGridView1.Columns.Add(AddCol2)
        DataGridView1.Columns.Add(AddCol3)
        DataGridView1.Columns.Add(AddCol4)
        For Each dgvr As DataGridViewRow In DataGridView1.Rows
            dgvr.Cells("Date").Value = datepicked
        Next

    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Dim dt As New DataTable
        dt = DataGridView1.DataSource

        Dim objSQLCon As New SqlConnection("Data Source=SERVER1\DEV01;database=Production;uid=sa;pwd=passwordhere")
        Dim TableName As String = "ProdEnter_SAVE"
        Dim strErrorMessage As String = ""

        Try
            If objSQLCon.State = ConnectionState.Open Then
                objSQLCon.Close()
            End If

            objSQLCon.Open()

            Try
                'To Perform Bulk Copy for copy data from DataTable to SQL Table
                If dt.Rows.Count > 0 Then
                    Using Sqlbcp As SqlBulkCopy = New SqlBulkCopy(objSQLCon)
                        Sqlbcp.DestinationTableName = "[dbo].[" & TableName & "]"
                        Sqlbcp.WriteToServer(dt)
                    End Using
                End If

            Catch sqlEx As SqlException
                strErrorMessage = "SqlException: " & sqlEx.Message & Environment.NewLine & "Error Code: " & sqlEx.ErrorCode
            Catch ex As Exception
                strErrorMessage = ex.Message
            End Try

        Catch ex As Exception
            strErrorMessage = "Open SQL connection failed" & ex.Message
        End Try
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        Me.Close()
    End Sub

End Class
Posted

1 solution

your code is almost correct just add this to your button2_click() event
VB
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Dim dt As New DataTable
        Dim ds As New System.Data.DataSet
        ds = DatagridviewToDataset(DataGridView1) 'This is the function which return you the dataset of your datagridview
        dt = ds.Tables(0)
        'And the rest of your code
Add this Function to your program
VB
Public Function DatagridviewToDataset(ByVal dgv As DataGridView) As System.Data.DataSet
        Dim ds As New System.Data.DataSet

        'Take the data and structure from the datagridview and return it as a dataset.  You can use 
        '"Imports System.Data" declaration at the top of your project/class and remove the system.data 
        'from the various parts of this function.

        Try
            'Add a new table to the dataset
            ds.Tables.Add("Main")

            'Add the columns
            Dim col As System.Data.DataColumn

            'For each colum in the datagridveiw add a new column to your table
            For Each dgvCol As DataGridViewColumn In dgv.Columns
                col = New System.Data.DataColumn(dgvCol.Name)
                ds.Tables("Main").Columns.Add(col)
            Next

            'Add the rows from the datagridview
            Dim row As System.Data.DataRow
            Dim colcount As Integer = dgv.Columns.Count - 1

            For i As Integer = 0 To dgv.Rows.Count - 1
                row = ds.Tables("Main").Rows.Add

                For Each column As DataGridViewColumn In dgv.Columns
                    row.Item(column.Index) = dgv.Rows.Item(i).Cells(column.Index).Value
                Next
            Next
            Return ds
        Catch ex As Exception
            'Catch any potential errors and display them to the user
            MessageBox.Show("Error Converting from DataGridView" & ex.InnerException.ToString, _
            "Error Converting from DataGridView", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return Nothing
        End Try
    End Function
 
Share this answer
 
v2
Comments
moordoom 4-Feb-14 11:10am    
Ok, I place the function in a module, or as a Public Function on the form?
And what should be done about the undeclared ds variable?
Basmeh Awad 5-Feb-14 1:29am    
deaclare the variable ds as dataset..
and you can place the function in the same form..
check my updated solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900