Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
569 views
in Technique[技术] by (71.8m points)

c# - How do I retrieve data from a gridview and text.label and put it into a DB?

I am a newbie here and would like to seek help from you guys on the following.

I would like to retrieve data from a GridView and TextLabel to put inside my database. However, I am receiving the following error:

Error on Line(Code): sqlBulkCopy.WriteToServer(dt)
Error Message: System.InvalidOperationException: 'Mappings must be either all 
name or all ordinal based.'

The following is my Page Load Page:

Public Class UserCfmOffer
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        lblOwner.Text = Session("UserID").ToString()
        lblItemID.Text = Session("ItemID").ToString()
        lblItemName.Text = Session("ItemName").ToString()
        lblItemDesc.Text = Session("ItemDesc").ToString()
        lblItemBrand.Text = Session("ItemBrand").ToString()
        lblItemCategory.Text = Session("ItemCategory").ToString()
        lblItemValue.Text = Session("ItemValue").ToString()
        lblItemConditions.Text = Session("ItemConditions").ToString()

  ' Over here we fetch the datatable from session i.e. stored in previous page and passed here 
        Dim dtselectedRows As DataTable = TryCast(Session("dtgetselectedRecords"), DataTable)
            grvSelectedItem.DataSource = dtselectedRows
            grvSelectedItem.DataBind()

    Protected Sub btnConfirm_Click(sender As Object, e As EventArgs) Handles btnConfirm.Click
        Dim dt As New DataTable()

        dt.Columns.AddRange(New DataColumn(0) {New DataColumn("ExItem", GetType(String))})

        For Each row As GridViewRow In grvSelectedItem.Rows
            Dim MyString As String
            MyString = Nothing

            Dim ExItem As String = row.Cells(2).Text
            dt.Rows.Add(ExItem)
        Next

        If dt.Rows.Count > 0 Then
Dim consString As String = ConfigurationManager.ConnectionStrings("BarterTradeDBConnectionString").ConnectionString
            Using conn As New SqlConnection(consString)
                Using sqlBulkCopy As New SqlBulkCopy(conn)

                    'Set the database table name
                    sqlBulkCopy.DestinationTableName = "dbo.Offer"

                    sqlBulkCopy.ColumnMappings.Add(lblOwner.Text, "Offer_ToUsr")
                    sqlBulkCopy.ColumnMappings.Add(lblItemID.Text, "Offer_ForItemID")
                    sqlBulkCopy.ColumnMappings.Add(vbNull, "Offer_ForSvcID")
                    sqlBulkCopy.ColumnMappings.Add("ExItem", "Offer_ExItemID")
                    sqlBulkCopy.ColumnMappings.Add(vbNull, "Offer_ExSvcID")
                    sqlBulkCopy.ColumnMappings.Add(lblOwner.Text, "UserID")

                    conn.Open()
                    sqlBulkCopy.WriteToServer(dt)
                    conn.Close()

                End Using
            End Using

        End If
question from:https://stackoverflow.com/questions/65640762/how-do-i-retrieve-data-from-a-gridview-and-text-label-and-put-it-into-a-db

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...