FormViewUpload

Oct 15, 2010 at 7:03 PM

We are encountering a Databind error message associated with CSASPNETFormViewUpload.

There appears to be no overload for the DataAdapter.Fill() method that accepts (DataSet, DataTableName):

http://msdn.microsoft.com/en-us/library/system.data.common.dataadapter.fill(v=VS.85).aspx

 

Here is the behind code:

Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' The Page is accessed for the first time.
        If Not IsPostBack Then
            ' Enable the FormView paging option and 
            ' specify the PageButton count.
            fvPerson.AllowPaging = True
            fvPerson.PagerSettings.PageButtonCount = 15

            ' Populate the FormView control.
            BindFormView()
        End If
    End Sub
    Private Sub BindFormView()
        ' Get the connection string from Web.config. 
        ' When we use Using statement, 
        ' we don't need to explicitly dispose the object in the code, 
        ' the using statement takes care of it.
        Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("sqlapplicationConnectionString").ToString())
            ' Create a DataSet object.
            Dim dsApplication As New DataSet()

            ' Create a SELECT query.
            Dim strSelectCmd As String = "SELECT First,Last,City, State FROM Application"

            ' Create a SqlDataAdapter object
            ' SqlDataAdapter represents a set of data commands and a 
            ' database connection that are used to fill the DataSet and 
            ' update a SQL Server database. 
            Dim da As New SqlDataAdapter(strSelectCmd, conn)

            ' Open the connection
            conn.Open()

            ' Fill the DataTable named "Application" in DataSet with the rows
            ' returned by the query.
            da.Fill(dsApplication, "Application")

            ' Bind the FormView control.
            fvPerson.DataSource = dsApplication
            fvPerson.DataBind()
        End Using
    End Sub
    ' FormView.ItemInserting Event
    Protected Sub fvPerson_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles fvPerson.ItemInserting
        ' Get the connection string from Web.config. 
        ' When we use Using statement, 
        ' we don't need to explicitly dispose the object in the code, 
        ' the using statement takes care of it.
        Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("sqlapplicationConnectionString").ToString())
            ' Create a command object.
            Dim cmd As New SqlCommand()

            ' Assign the connection to the command.
            cmd.Connection = conn

            ' Set the command text
            ' SQL statement or the name of the stored procedure. 
            cmd.CommandText = "INSERT INTO Person ( First, Last, City, State ) VALUES ( @First, @Last, @City, @State )"

            ' Set the command type
            ' CommandType.Text for ordinary SQL statements; 
            ' CommandType.StoredProcedure for stored procedures.
            cmd.CommandType = CommandType.Text

            ' Get the first name and last name from the 
            ' InsertItemTemplate of the FormView control.
            Dim strFirst As String = DirectCast(fvPerson.Row.FindControl("First"), TextBox).Text
            Dim strLast As String = DirectCast(fvPerson.Row.FindControl("Last"), TextBox).Text
            Dim strCity As String = DirectCast(fvPerson.Row.FindControl("City"), TextBox).Text
            Dim strState As String = DirectCast(fvPerson.Row.FindControl("State"), TextBox).Text

            ' Append the parameters to the SqlCommand and set values.
            cmd.Parameters.Add("@First", SqlDbType.NVarChar, 50).Value = strFirst
            cmd.Parameters.Add("@Last", SqlDbType.NVarChar, 50).Value = strLast
            cmd.Parameters.Add("@City", SqlDbType.NVarChar, 50).Value = strCity
            cmd.Parameters.Add("@State", SqlDbType.NVarChar, 50).Value = strState

            ' Open the connection.
            conn.Open()

            ' Execute the command.
            cmd.ExecuteNonQuery()
        End Using

        ' Switch FormView control to the ReadOnly display mode. 
        fvPerson.ChangeMode(FormViewMode.ReadOnly)

        ' Rebind the FormView control to show data after inserting.
        BindFormView()
    End Sub

    ' FormView.ItemUpdating Event
    Protected Sub fvPerson_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdateEventArgs) Handles fvPerson.ItemUpdating
        ' Get the connection string from Web.config. 
        ' When we use Using statement, 
        ' we don't need to explicitly dispose the object in the code, 
        ' the using statement takes care of it.
        Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("sqlapplicationConnectionString").ToString())
            ' Create a command object.
            Dim cmd As New SqlCommand()

            ' Assign the connection to the command.
            cmd.Connection = conn

            ' Set the command type
            ' CommandType.Text for ordinary SQL statements; 
            ' CommandType.StoredProcedure for stored procedures.
            cmd.CommandType = CommandType.Text

            ' Get the person ID, first name and last name from the 
            ' EditItemTemplate of the FormView control.
            Dim strFirst As String = DirectCast(fvPerson.Row.FindControl("First"), Label).Text
            Dim strLast As String = DirectCast(fvPerson.Row.FindControl("Last"), TextBox).Text
            Dim strCity As String = DirectCast(fvPerson.Row.FindControl("City"), TextBox).Text
            Dim strState As String = DirectCast(fvPerson.Row.FindControl("State"), TextBox).Text

            ' Append the parameters to the SqlCommand and set values.
            cmd.Parameters.Add("@First", SqlDbType.NVarChar, 50).Value = strFirst
            cmd.Parameters.Add("@Last", SqlDbType.NVarChar, 50).Value = strLast
            cmd.Parameters.Add("@City", SqlDbType.NVarChar, 50).Value = strCity
            cmd.Parameters.Add("@State", SqlDbType.NVarChar, 50).Value = strState

            ' Open the connection.
            conn.Open()

            ' Execute the command.
            cmd.ExecuteNonQuery()
        End Using

        ' Switch FormView control to the ReadOnly display mode. 
        fvPerson.ChangeMode(FormViewMode.ReadOnly)

        ' Rebind the FormView control to show data after updating.
        BindFormView()
    End Sub
    ' FormView.PageIndexChanging Event
    Protected Sub fvPerson_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewPageEventArgs) Handles fvPerson.PageIndexChanging
        ' Set the index of the new display page. 
        fvPerson.PageIndex = e.NewPageIndex

        ' Rebind the FormView control to show data in the new page.
        BindFormView()
    End Sub

    ' FormView.ModeChanging Event
    Protected Sub fvPerson_ModeChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewModeEventArgs) Handles fvPerson.ModeChanging
        ' Switch FormView control to the new mode
        fvPerson.ChangeMode(e.NewMode)

        ' Rebind the FormView control to show data in new mode.
        BindFormView()
    End Sub
    Public Function strLong(ByVal pstrText As String) As String

        Dim strTemp As String = Nothing
        Dim lngI As Long = 0
        strTemp = ""
        For lngI = 1 To Strings.Len(pstrText)
            If Strings.InStr(1, "1234567890", Strings.Mid(pstrText, lngI, 1)) > 0 Then
                strTemp = strTemp & Strings.Mid(pstrText, lngI, 1)
            End If
        Next
        Return Strings.Left(strTemp, 20)
    End Function
End Class

 

Oct 15, 2010 at 7:19 PM

Go to the following URL link to view the front code at http://cforedu.com