none
How to store format data from datatable to sql 2008 RRS feed

  • Question

  •    If dt.Rows.Count > 0 Then

                Using con As New SqlConnection(cnn)
                    Using sqlBulkCopy As New SqlBulkCopy(con)
                        'Set the database table name
                        sqlBulkCopy.DestinationTableName = "dbo.Nd1AttnTemp"

                        '[OPTIONAL]: Map the DataTable columns with that of the database table
                        sqlBulkCopy.ColumnMappings.Add("Empcode", String.Format("empcode", "00000"))
                        sqlBulkCopy.ColumnMappings.Add("AttendanceDt", "AttnDt")
                        sqlBulkCopy.ColumnMappings.Add("PunchTime", "Punchtime")


                        con.Open()
                        sqlBulkCopy.WriteToServer(dt)
                        con.Close()
                    End Using
                End Using
            End If

    The below coding is not working , What I do? Help me pls.

    sqlBulkCopy.ColumnMappings.Add("Empcode", String.Format("empcode", "00000"))

    Wednesday, August 29, 2018 10:12 AM

All replies

  • Hi,

    The code seems to be correct.
    Did you get any errors when executing this code?

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 30, 2018 2:15 AM
  • Dear Alex

     No, I am not get any error. But data are stored in 2 or 3 digits only not 5 digits. leading '0' not coming

    Note

    My grid data are 5 digits

    Gird Data

    Sql Data

    Regards

    Anburose

    Thursday, August 30, 2018 4:55 AM
  • Hello,

    A database is designed to store raw data not formatted data. So in your case store the Empcode (thinking as a number) then to display in a DataGridView use the formatting capabilities of the DataGridView. In the following example I mocked up data as if coming from a database.

    Dim dt As New DataTable
    
    dt.Columns.Add(New DataColumn With
    {
        .ColumnName = "Identifier",
        .DataType = GetType(Integer),
        .AutoIncrement = True,
        .AutoIncrementSeed = 1
    })
    
    dt.Columns.Add(New DataColumn With {.ColumnName = "empcode", .DataType = GetType(Integer)})
    
    dt.Rows.Add(New Object() {Nothing, 990})
    dt.Rows.Add(New Object() {Nothing, 140})
    dt.Rows.Add(New Object() {Nothing, 390})
    dt.Rows.Add(New Object() {Nothing, 158})
    dt.Rows.Add(New Object() {Nothing, 4000})
    
    DataGridView1.DataSource = dt

    To format empcode

    Dim dt As New DataTable
    
    dt.Columns.Add(New DataColumn With
    {
        .ColumnName = "Identifier",
        .DataType = GetType(Integer),
        .AutoIncrement = True,
        .AutoIncrementSeed = 1
    })
    
    dt.Columns.Add(New DataColumn With {.ColumnName = "empcode", .DataType = GetType(Integer)})
    
    dt.Rows.Add(New Object() {Nothing, 990})
    dt.Rows.Add(New Object() {Nothing, 140})
    dt.Rows.Add(New Object() {Nothing, 390})
    dt.Rows.Add(New Object() {Nothing, 158})
    dt.Rows.Add(New Object() {Nothing, 4000})
    
    DataGridView1.DataSource = dt
    DataGridView1.Columns("empcode").DefaultCellStyle.Format = "D5"


    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. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, August 30, 2018 9:58 AM
    Moderator