none
insert datatable and datagridview data to sql

    Question

  • I need your help on this, I need to insert a datagridview columns and 2 datatable column into sql. the code below inserted the datagridview column row by row in order which is what I want, but the datatable column only insert the first row and copy it on all rows. any idea on how to fix this ? appreciated

    _Command = New SqlCommand("insert_student", _Connection)
        _Command.CommandType = CommandType.StoredProcedure
        _Connection.Open()
        _Command.Parameters.Add(New SqlParameter("@_name", SqlDbType.NVarChar))
        _Command.Parameters.Add(New SqlParameter("@_m_barrImg", SqlDbType.VarBinary))
    _Command.Parameters.Add(New SqlParameter("@_CurrentImageName", SqlDbType.NChar))
        _Command.Parameters.Add(New SqlParameter("@_CurrentImageName1", SqlDbType.NChar))
    For x = 0 To DataGridView1.RowCount - 1
            _Command.Parameters("@_name").Value = DataGridView1.Rows(x).Cells("name4").Value
         _Command.Parameters("@_m_barrImg").Value = DataGridView1.Rows(x).Cells("CompanyStatusImage").Value
     Dim dt As New DataTable()
        For i As Integer = 0 To dt.DefaultView.Count - 1
            _Command.Parameters("@_CurrentImageName").Value = dt.Rows(i)("CurrentImageName").ToString
            _Command.Parameters("@_CurrentImageName1").Value = dt.Rows(i)("CurrentImageName1").ToString
     Next
            _Command.ExecuteNonQuery()
        Next


    gdeal++


    • Edited by gdeal1 Friday, July 05, 2013 1:34 AM
    Friday, July 05, 2013 1:12 AM

Answers

  • Hello,

    Unless I am really missing something the DataTable below has no rows as you created it then immediately use it expecting data.

    Dim dt As New DataTable()
    For i As Integer = 0 To dt.DefaultView.Count - 1
        _Command.Parameters("@_CurrentImageName").Value = dt.Rows(i)("CurrentImageName").ToString
        _Command.Parameters("@_CurrentImageName1").Value = dt.Rows(i)("CurrentImageName1").ToString
    Next
    _Command.ExecuteNonQuery()


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Monday, July 08, 2013 1:02 PM

All replies

  • Hi gdeal1,

    Welcome to MSDN forum!

    I suggest that you can insert a datagridview column and two datatable columns into another datatable, and then insert data into sql.

    Best regards,

    Minna

    Monday, July 08, 2013 10:36 AM
  • Hello,

    Unless I am really missing something the DataTable below has no rows as you created it then immediately use it expecting data.

    Dim dt As New DataTable()
    For i As Integer = 0 To dt.DefaultView.Count - 1
        _Command.Parameters("@_CurrentImageName").Value = dt.Rows(i)("CurrentImageName").ToString
        _Command.Parameters("@_CurrentImageName1").Value = dt.Rows(i)("CurrentImageName1").ToString
    Next
    _Command.ExecuteNonQuery()


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Monday, July 08, 2013 1:02 PM